0

my task is:

1) Select all cells in A row which contain values (done). 2) Text to Columns - example value 2017.01.01

Sub selectAndToColumns()
    Dim LR As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Range("A1:A" & LR).SpecialCells(xlCellTypeConstants, 23).Select
'2nd step
    Selection.TextToColumns Destination:=Range(A1), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
    :=".", FieldInfo:=Array(Array(1, 2), Array(2, 1), Array(3, 1))
End Sub

1 step is ok, but how can I use my selection instead of Range(A1) in 2nd step? Can I make a variable and how?

3
  • What do you mean with my selection? Commented Jan 11, 2018 at 15:01
  • I understand your problem.... TextToColumns will work on an entire column rather than some set of cells within that column. Commented Jan 11, 2018 at 15:06
  • I agree that no need in step 1 - i need just select the whole column, but how? Commented Jan 11, 2018 at 15:26

2 Answers 2

1

I'm not too sure about your requirements, but this will perform the TextToColumns without selecting anything (You can still change the destination to wherever you want the resulting data to be placed, you can do this by specifying a range or even using a variable where your range is stored):

Sub selectAndToColumns()
    Dim LR As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    Range("A1:A" & LR).SpecialCells(xlCellTypeConstants, 23).TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
    :=".", FieldInfo:=Array(Array(1, 2), Array(2, 1), Array(3, 1))
End Sub

UPDATE

If you want to replace your destination with a variable instead of Range("A1") then something like below will work:

Sub selectAndToColumns()
    Dim DestinationRange As String
    Dim LR As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    DestinationRange = "D1"
    MsgBox DestinationRange
    Range("A1:A" & LR).SpecialCells(xlCellTypeConstants, 23).TextToColumns Destination:=Range(DestinationRange), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
    :=".", FieldInfo:=Array(Array(1, 2), Array(2, 1), Array(3, 1))
End Sub
Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure if I have understood your question properly, but if you just want to select Column A and paste it as values, I would use:

Columns("A:A").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
     :=False, Transpose:=False
Application.CutCopyMode = False

1 Comment

Selecting working even in my macro but i needed text to column after. Thanks for your answer anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.