How text to columns using "-" and "_" at the same time

Paul21

New Member
Joined
Mar 17, 2018
Messages
17
Hello there,

I want to split file name in excel using Text to Column vba using dash and underscore as delimeters.
I am using below macro specifying dash or undersocre as OtherChar. How can we have dash and underscore simultaniuosly ?

I want to split this A-B_C into A B C

Thx :)

VBA Code:
Sub text_to_col()

Application.DisplayAlerts = False

Range("A2", Range("A2").End(xlDown)).TextToColumns _
      Destination:=Range("B2"), _
      DataType:=xlDelimited, _
      TextQualifier:=xlDoubleQuote, _
      ConsecutiveDelimiter:=True, _
      Tab:=True, _
      Semicolon:=False, _
      Comma:=False, _
      Space:=True, _
      Other:=True, _
      OtherChar:="_", _
      
      
Application.DisplayAlerts = True
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
VBA Code:
Sub text_to_col()
    Application.DisplayAlerts = False
    For Each cell In Range("A2", Range("A2").End(xlDown))
        cell.Value = Replace(cell.Value, "_", "-")
    Next cell
    Range("A2", Range("A2").End(xlDown)).TextToColumns _
          Destination:=Range("B2"), _
          DataType:=xlDelimited, _
          TextQualifier:=xlDoubleQuote, _
          ConsecutiveDelimiter:=True, _
          Tab:=True, _
          Semicolon:=False, _
          Comma:=False, _
          Space:=True, _
          Other:=True, _
          OtherChar:="-"
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Solution
VBA Code:
Sub text_to_col()
    Application.DisplayAlerts = False
    For Each cell In Range("A2", Range("A2").End(xlDown))
        cell.Value = Replace(cell.Value, "_", "-")
    Next cell
    Range("A2", Range("A2").End(xlDown)).TextToColumns _
          Destination:=Range("B2"), _
          DataType:=xlDelimited, _
          TextQualifier:=xlDoubleQuote, _
          ConsecutiveDelimiter:=True, _
          Tab:=True, _
          Semicolon:=False, _
          Comma:=False, _
          Space:=True, _
          Other:=True, _
          OtherChar:="-"
    Application.DisplayAlerts = True
End Sub
Hi Didi, works like charm, moreover I do undrstand your solution. Appreciate!!!
 
Upvote 0
thanks for that. you can tick "mark as solution" if it has sorted the problem
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,858
Members
449,052
Latest member
Fuddy_Duddy

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