I am trying to run the Text to Column function. I recorded a macro and it appears to work for the first attempt in the cell I recorded the macro for. I need this macro to run for any cells I select so I changed the range to "ActiveCell" but this seems to break the code.
Recorded Macro:
Selection.TextToColumns Destination:=Range("X32"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 2), Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True
End Sub
Selection.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 2), Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True
works for me when I select a cell whose text have semicolons and it gets split into many columns
In order for the TextToColumns to work, I hope you selected a single column as the source for this funtion to work, i.e selecting cells "E5:E10" will work.
Selecting multiple columns as an Input will result with a run-time error.
Documentation link MSDN TextToColumns
Code
Sub TxtToCol()
Dim DestRng As Range
' setting the destination range using a variable
Set DestRng = Range("X32")
With Selection
.TextToColumns Destination:=DestRng, _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, _
Semicolon:=True, _
Comma:=False, _
Space:=False, _
Other:=False, _
FieldInfo:=Array(Array(1, 2), Array(2, 1), Array(3, 1)), _
TrailingMinusNumbers:=True
End With
End Sub
Related
I have code that opens a .txt file saved off of the desktop. It works great unless the user has OneDrive, then I receive an error. Is there a way to make the code dynamic to recognise if it is a OneDrive environment or not?
Sub OPENER()
'
' new_open Macro
'
'
user = Environ("Username")
ChDir "C:\Users\" & user & "\Desktop"
Workbooks.OpenText Filename:="C:\Users\" & user & "\Desktop\next.txt", Origin:=437 _
, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=True, _
Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array( _
3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1)), _
TrailingMinusNumbers:=True
End Sub
Hi I am trying to create a code which converts column A to columns with a | delimiter and want column A to be in Text format.
My code:
Sub TEST_Text_to_Columns()
ActiveSheet.Range("A:A").Select
Selection.TextToColumns _
Destination:=Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=True, _
OtherChar:="|"
FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
ActiveSheet.Columns("A:GZ").AutoFit
End Sub
It doesn't seem to like this at the FieldInfo part.
Please help
You are missing the , _ after OtherChar line:
Sub TEST_Text_to_Columns()
ActiveSheet.Range("A:A").Select
Selection.TextToColumns _
Destination:=Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=True, _
OtherChar:="|", _
FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
ActiveSheet.Columns("A:GZ").AutoFit
End Sub
However, to also fix the rest of the code:
Select and selection are unnecessary, slow things down and can cause errors. There are articles in this forum as to how and why to avoid them.
To format a column as text, xlTextFormat returns a 2, so your FieldInfo should be a bit different:
Sub TEST_Text_to_Columns()
ActiveSheet.Range("A:A").TextToColumns _
Destination:=Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=True, _
OtherChar:="|", _
FieldInfo _
:=Array(1, 2), TrailingMinusNumbers:=True
ActiveSheet.Columns("A:GZ").AutoFit
End Sub
So basically I have multiple columns of text that I want to change to numbers.
Now the issue i face is ive got the code but i dont understand how to run a loop on it to choose the next third column.
This is my code:
Sub Texscolumn()
Range("AI2:AI96").Select
Selection.TextToColumns Destination:=Range("AI2"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 9), Array(3, 9), Array(4, 9), Array(5, 9), Array(6, 9), Array(7, 9), Array(8, 9)), TrailingMinusNumbers:= _
True
End Sub
So the next column I want is suppose AJ2:AJ96.
I cant seem to figure out how to make a for loop with changing columns like this.
At least 2 options :
Sub Texscolumn()
Dim j As Integer
With Sheets("sheet1")
For j = 35 To 36
.Range(.Cells(2, j), .Cells(96, j)).TextToColumns Destination:=.Cells(2, j), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=True, _
Semicolon:=False, _
Comma:=False, _
Space:=True, _
Other:=False, _
FieldInfo:=Array(Array(1, 1), Array(2, 9), Array(3, 9), Array(4, 9), Array(5, 9), Array(6, 9), Array(7, 9), Array(8, 9)), _
TrailingMinusNumbers:=True
Next i
End With
End Sub
or
Sub Texscolumn()
Dim Rg As Range
Dim i As Integer
Set Rg = Sheets("sheet1").Range("AI2:AI96")
With Rg
For i = 1 To 2
.TextToColumns Destination:=.Cells(1, 1), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=True, _
Semicolon:=False, _
Comma:=False, _
Space:=True, _
Other:=False, _
FieldInfo:=Array(Array(1, 1), Array(2, 9), Array(3, 9), Array(4, 9), Array(5, 9), Array(6, 9), Array(7, 9), Array(8, 9)), _
TrailingMinusNumbers:=True
Set Rg = .Offset(0, 1)
Next i
End With
End Sub
I'm trying to do Text to Columns with the OtherChar not just limited to one. Currently my code looks like this:
With Selection
.TextToColumns Destination:=[A2], _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=True, _
Semicolon:=False, _
Comma:=True, _
Space:=True, _
Other:=True, _
OtherChar:="-", _
TrailingMinusNumbers:=True
End With
But I want the OtherChar to include these characters "(, ), [, ], /, *". Is there a way to do that?
Quick and dirty code. Do not use * as delimiter as it will be treated as all in replace
Dim aDelim
Dim delim
Dim oRange As Range
aDelim = Split("(,),[,],/", ",")
Set oRange = Selection
For Each delim In aDelim
oRange.Replace What:=delim, Replacement:="-"
Debug.Print oRange.Text, delim
Next
oRange.TextToColumns Destination:=[A2], _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=True, _
Semicolon:=False, _
Comma:=True, _
Space:=True, _
Other:=True, _
OtherChar:="-", _
TrailingMinusNumbers:=True
I got another Problem with VBA. I used the TexttoColumns Sub to separate every Cell in my Column by semicolon. Now I wanted to insert Columns after that one, which should contain the separated values. It all worked pretty well in the beginning, but now it suddenly won't insert new cells, but overwrite the old ones.
Example (Wish) :
Row1 Row2 Row3
Tree;PC;House |Data1 |Data2 --> Tree|PC|House|Data1|Data2
Example(How it is):
Tree;PC;House|Data1|Data2 --> Tree|PC|House
Workbooks(Ziel).Worksheets(Zieltab).Columns(Spalte).TextToColumns Destination:=Columns(Spalte), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 2), Array(2, 2))
A Range.TextToColumns method does not insert columns. It will always overwrite data if allowed to proceed.
With Workbooks(Ziel).Worksheets(Zieltab).Columns(Spalte)
'insert two columns to the right
.Cells(1).Resize(1, 2).Offset(0, 1).EntireColumn.Insert
'split the first column into itself and the two new column
.TextToColumns Destination:=.Cells(1), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, _
FieldInfo:=Array(Array(1, 2), Array(2, 2), Array(3, 2))
End With