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

Paul21

New Member
Joined
Mar 17, 2018
Messages
14
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

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,297
Office Version
  1. 2010
Platform
  1. Windows
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
 
Solution

Paul21

New Member
Joined
Mar 17, 2018
Messages
14
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!!!
 

diddi

Well-known Member
Joined
May 20, 2004
Messages
3,297
Office Version
  1. 2010
Platform
  1. Windows
thanks for that. you can tick "mark as solution" if it has sorted the problem
 

Watch MrExcel Video

Forum statistics

Threads
1,129,568
Messages
5,637,094
Members
416,957
Latest member
Brovashift

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
Top