Having trouble using TextToColumns with variables.
This works:
IDCol = Rows(8).Find("ID", LookAt:=xlWhole).Column
Columns(IDCol).Select
Selection.TextToColumns Destination:=Range("J1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
This doesn't though, and since I don't always know where the column is going to be. The issue seems to be setting the Destination.
IDCol = Rows(8).Find("ID", LookAt:=xlWhole).Column
Columns(IDCol).Select
Selection.TextToColumns Destination:=Range(Cells(1, IDCol)), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Thanks if anyone can help.
Try this:
Sub test()
Dim id_col As Long
id_col = Rows(8).Find("ID", LookAt:=xlWhole).Column
Dim target_col As Range
If id_col <> vbNullString Then
Set target_col = Columns(id_col)
With target_col
.TextToColumns , DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
End With
End If
End Sub
Related
ActiveCell.EntireColumn.Select
Selection.TextToColumns Destination:=Range("G1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 4), TrailingMinusNumbers:=True
Selection.NumberFormat = "dd-mmm-yy"
I found the solution , Thanks for support , Modified code should be
ActiveCell.EntireColumn.Select
Dim myRange As Range
Set myRange = Selection
myRange.TextToColumns Destination:=myRange, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 4), TrailingMinusNumbers:=True
Selection.NumberFormat = "dd-mmm-yy"
I have recorded a macro that will convert a column of dates in UK format to text by using Text to Columns when recording it, but the text it returns is in US format. Text to Columns does the job when its outside of a macro so not sure why it wouldn't work when used in a macro.
Is there a way to have the text in UK format and not have it converted to US format? I believe the below is the relevant part:
Selection.TextToColumns Destination:=Range("E1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 2), TrailingMinusNumbers:=True
Columns("F:F").Select
Selection.TextToColumns Destination:=Range("F1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 2), TrailingMinusNumbers:=True
Columns("A:P").Select
Selection.Copy
Windows("Xero Upload Template V2.xlsx").Activate
Range("A4").Select
ActiveSheet.Paste```
If I understand correctly then your data is in column 5 and 6 which you need to convert. Please try below code.
Dim c As Long
For c = 5 To 6
Columns(c).TextToColumns DataType:=xlDelimited, Tab:=False, Semicolon:=False,
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 5)
Columns(c).NumberFormat = "dd-mm-yyyy"
Next c
I'm trying to write a code to do the following:
Using text-to-columns,the data should get divided in different columns.
The data in Cells A1-A8 is like this:
This data should appear in different columns.
Like this?
Sub Macro()
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:=":", FieldInfo:=Array(Array(1, 1), Array(2, 1)),
End Sub
You can build the FieldInfo parameter dynamically by building out the string. In the example below, there are 10 resultant columns. The For loop builds the sFieldInfo variable, and specifies all the columns in Text format.
sComma = ""
sFieldInfo = ""
For x = 1 To 10
sFieldInfo = sFieldInfo & sComma & " Array(" & x & ", xlTextFormat)"
sComma = ","
Next x
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.TextToColumns Destination:=Range("J5"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar:="/", _
TrailingMinusNumbers:=True, _
FieldInfo:=Array(sFieldInfo)
I am facing issue after copy and paste from export file from SAP it ask to convert to number option which is like error without converting to number formulas do not work. also cell value is number only.
I tried to make macro but it works on single column only. how to make it work on multiple column together and make it faster also as it stuck exel for long time.
code for converting to number
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, 1), TrailingMinusNumbers:=True
Columns("F:F").Select
Selection.TextToColumns Destination:=Range("F1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Columns("G:G").Select
Selection.TextToColumns Destination:=Range("G1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
How shorter code and combine for multiple columns in single line.
Use an xlFixedWidth in your Range.TextToColumns method.
Dim c As Long, vCOLs As Variant
vCOLs = Array(1, 6, 7) 'columns A, F and G
With Worksheets("Sheet1")
For c = LBound(vCOLs) To UBound(vCOLs)
With .Column(c)
.TextToColumns Destination:=.Cells(1), DataType:=xlFixedWidth, _
FieldInfo:=Array(0, 1), TrailingMinusNumbers:=True
End With
Next c
End With
The variant array allow you to quickly specify the columns to be processed. Even with a large (~250K) number of rows, this should be fairly quick to cycle through.
I have code for delimited columns for one sheet. How do I make this code for more than sheets with looping?
Sub Macro1()
Selection.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="_", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
Selection.NumberFormat = "dd-mm-yyyy;#"
End Sub
Sub t()
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Sheets
'do what you want, for example,
Range("A1:B10").TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="_", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True
ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
Selection.NumberFormat = "dd-mm-yyyy;#"
Next
End Sub
However, you should not use Selection. You should use a range instead, for e.g. Range("A1:B10")
Try it like this
Sub forEachWs()
Dim x As Long
Dim ws As Sheets
For x = 1 To 12'<--to as how many is required
ThisWorkbook.Sheets(x).Activate
Call Macro1 '<---the name of your macro
Range("A2").Select
End Sub