Excel Text-to-Columns using VB.net - vb.net

I have an excel sheet with variable number of entries in the column A.
Sample:
402110000027547 97517161579 IDLE 402-11-150
402110000013260 97517117011 IDLE 402-11-190
402110000033664 97517125759 IDET 402-11-21
I want to execute text-to-columns and put these values in separate columns (A, B, C & D). Any help is appreciated.

I got it working using this code:
With sheet
.Columns(1).TextToColumns( _
Destination:=.Cells(1, 1), _
DataType:=Excel.XlTextParsingType.xlDelimited, _
TextQualifier:=Excel.XlTextQualifier.xlTextQualifierDoubleQuote, _
ConsecutiveDelimiter:=False, _
TAB:=False, _
Semicolon:=False, _
Comma:=False, _
Space:=True, _
Other:=False, _
TrailingMinusNumbers:=False)
End With
Thanks to this page
http://www.siddharthrout.com/index.php/2018/06/28/excel-text-to-columns-from-vb-net/

Related

Need VBA That Works With and Without OneDrive

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

VBA Text to Columns FieldInfo Text not working

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

Run recorded Text to Column code on selected cell

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

Text to Columns delimitter

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

Text to Columns overwrites columns

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