Hi all
I am having trouble with combining the .textotocolumn function with if statements. The posts I have found all have fixed/specified ranges such as destination cell A1 and range A:A.
My data insn't always imported the same way. Both the columns and the format varies which is why I would like to make a vba code that searches through the dataset.
If a cell contains "Start date" or "End date" i would like to use the text to column function changing to the date format on the entire column. Thus my search variables would be A "Start date", B "End date"
I have tried recording a macro to see how the function looks like and it seems it needs both the entire column selected and the first cell of the selected range as destination.
It looks something like this:
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 4), TrailingMinusNumbers:=True
ActiveWindow.SmallScroll Down:=12
Below I have tried to make a loop that searches for a text string but I can't get to dynamically set both a range pointing at the cell contain the textstring and then select the entire column. I know it is me not doing
it right, so I would really appreciate some help.
Sub FormatText()
Dim MyRange As Range
Set MyRange = ActiveCell.Offset(1, 0)
For i = ActiveSheet.Columns.Count To 1 Step -1
If InStr(1, Cells(1, i), "some text") Then Columns(i).EntireColumn.Select
Selection.TextToColumns Destination:=Range(MyRange), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 4), TrailingMinusNumbers:=True
Best regards
Kasper
I am having trouble with combining the .textotocolumn function with if statements. The posts I have found all have fixed/specified ranges such as destination cell A1 and range A:A.
My data insn't always imported the same way. Both the columns and the format varies which is why I would like to make a vba code that searches through the dataset.
If a cell contains "Start date" or "End date" i would like to use the text to column function changing to the date format on the entire column. Thus my search variables would be A "Start date", B "End date"
I have tried recording a macro to see how the function looks like and it seems it needs both the entire column selected and the first cell of the selected range as destination.
It looks something like this:
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 4), TrailingMinusNumbers:=True
ActiveWindow.SmallScroll Down:=12
Below I have tried to make a loop that searches for a text string but I can't get to dynamically set both a range pointing at the cell contain the textstring and then select the entire column. I know it is me not doing
it right, so I would really appreciate some help.
Sub FormatText()
Dim MyRange As Range
Set MyRange = ActiveCell.Offset(1, 0)
For i = ActiveSheet.Columns.Count To 1 Step -1
If InStr(1, Cells(1, i), "some text") Then Columns(i).EntireColumn.Select
Selection.TextToColumns Destination:=Range(MyRange), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 4), TrailingMinusNumbers:=True
Best regards
Kasper