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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

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