Text-to-Columns Delimiter Based on Cell Contents

lneidorf

Board Regular
Joined
May 20, 2004
Messages
97
Office Version
  1. 365
Platform
  1. Windows
Hi there.

I frequently review DAT files. These DAT files use a variety of characters as delimiters. I'm trying to devise a macro that will perform a text-to-column operation, using the content of a specified cell as the delimiter.

Here's my code, which uses þ (ASCII 0254) as a delimiter:

Code:
With Sheet1
    Range("A1", ActiveCell.End(xlDown)).Select
    
    Do
    Selection.TextToColumns Destination:=ActiveCell.Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlSingleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar:="þ", FieldInfo _
    :=Array(1, 1), TrailingMinusNumbers:=True
    ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
    Loop Until IsEmpty(ActiveCell.Columns("A:A"))
    
    Dim i As Long
    For i = 255 To 1 Step -2
    Columns(i).Delete
    Next
    
    Range("A1", Selection.End(xlToRight)).Select
    Selection.Columns.AutoFit
    
    Range("A2").Select
    ActiveWindow.FreezePanes = True
End With

Instead of having the delimiter hard-coded, I'd like my macro to use whatever character I enter into Sheet3.Range("i5") as a delimiter. That way, I can easily change my delimiter character as necessary.

Any suggestions would be most appreciated.

Thanks!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
The key is here:

Code:
    Selection.TextToColumns Destination:=ActiveCell.Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlSingleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar:="þ", FieldInfo _
    :=Array(1, 1), TrailingMinusNumbers:=True

The "OtherChar:=" portion is followed by the character you use to split the data. So if you make a variable equal to Sheet3.Range("i5") and then use that variable after the OtherChar:= portion, it should use that as the delimiter.

Sample:

Code:
Dim strDelimiter As String

strDelimiter = Sheet3.Range("i5")

    Selection.TextToColumns Destination:=ActiveCell.Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlSingleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar:=strDelimiter, FieldInfo _
    :=Array(1, 1), TrailingMinusNumbers:=True
 
Upvote 0
One small tweek, don't forget to define the range before the text to columns code, with something like:

Code:
Range("a1").Select

where a1 is the cell you want to perform the text to columns function on.
 
Upvote 0
That did the trick. I think I just learned something to boot.

Thanks very much!
 
Upvote 0

Forum statistics

Threads
1,215,381
Messages
6,124,615
Members
449,175
Latest member
Anniewonder

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top