trim characters before and after "_"

ymeyaw

New Member
Joined
Apr 24, 2006
Messages
29
Dear experts,

I had excel column with following format :

123_ABC_DATA1_1
234_BCD_DATA2_44

and i wish to trim the data so that i will have:
DATA1
DATA2

and i do not want to use import files with "_" . How should i do that? This is the last part of my data processing using VBA and i really hope some one can help me on this. Thanks for your help...
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Are we safe in presuming that there are consistently two underscores fore and one aft the part we want left?
 
Upvote 0
Sub Macro1()
'
' This will just delimit based on the char "_"
'
Columns("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(Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 2)), _
TrailingMinusNumbers:=True
End Sub
 
Upvote 0
Are we safe in presuming that there are consistently two underscores fore and one aft the part we want left?
Hi GTO,

Yes, There will be always a 2 underscore before and one underscore after which i only need the "DATA" part only.
 
Upvote 0
I imagine you already have your range referenced, so just a demo to try.

Rich (BB code):
Sub exa()
Dim REX As Object
Dim cell As Range
    
    Set REX = CreateObject("VBScript.RegExp")
    With REX
        .Global = False
        .Pattern = "([^_]*_)([^_]*_)([^_]+)(_.*)"
    
        For Each cell In Range("A2:A5")
            If .Test(cell.Text) Then cell.Offset(, 2).Value = .Execute(cell.Text)(0).SubMatches(2)
        Next
    End With
End Sub

Before/After:
Excel Workbook
ABC
1
2123_ABC_DATA1_1DATA1
3234_BCD_DATA2_44DATA2
412321_A_Data3_45Data3
5BASCD_ADS_Data4_0Data4
Sheet1
Excel 2003
Hope that helps,

Mark
 
Upvote 0

Forum statistics

Threads
1,224,593
Messages
6,179,791
Members
452,942
Latest member
VijayNewtoExcel

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