Find the letters in a string

julie_nickb

New Member
Joined
Sep 13, 2018
Messages
46
Office Version
  1. 365
Hello,
In VBA, I would like to split a string variable in two, at the point where the first time "_" is used.

For example, if the string is "ABC_12345_asd", I would like to split it into "ABC" and "_12345_asd".
I know how to do this is Excel using the "FIND" and "LEFT" functions, but having problems in VBA.
Thanks for your help.










Tt
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
You will need to use the Split function. In your example above, you would get the desire result with the following:
VBA Code:
TestString = "ABC_12345_asd"
Results = Split(TestString, "_", 2)

The resulting two parts are now stored in the Results variable as Results(0) and Results(1). To help you understand how it works, I prepared a short subroutine below so you could see/test the process. When using the Split function, it removes the delimiting character ( the undersscore ) so, as below, you need to just put it back in.

VBA Code:
Sub SplitString()

Dim TestString As String, Results As Variant

TestString = "ABC_12345_asd"
Results = Split(TestString, "_", 2)
Debug.Print Results(0)          '  Output:    ABC
Debug.Print Results(1)          '  Output:    12345_asd

Results(1) = "_" & Results(1)   '  Need to put the underscore back before the second part

Debug.Print Results(1)          '  Output:    _1234_asd

End Sub

Hope that helps.
 
Upvote 0
Solution
Here's a UDF (VBA - User Defined Function) you can use like a worksheet function (see example below) or in a VBA subroutine (e.g. the sub below the udf).
Book1
ABC
1ABC_12345_asdABC_12345_asd
2ABC12345asdABC12345asd 
Sheet1
Cell Formulas
RangeFormula
B1:C2B1=StrangeSplit(A1,"_")
Press CTRL+SHIFT+ENTER to enter array formulas.


VBA Code:
Function StrangeSplit(S As String, Ch As String) As Variant()
Dim Spl(1)
If InStr(S, Ch) = 0 Then
    Spl(0) = S
    Spl(1) = ""
    StrangeSplit = Spl
    Exit Function
End If
Spl(0) = Left(S, InStr(S, Ch) - 1)
Spl(1) = Mid(S, InStr(S, Ch), Len(S))
StrangeSplit = Spl
End Function
Sub Testit()
Dim X As Variant
X = StrangeSplit(Range("A1").Value, "_")
MsgBox X(0) & vbNewLine & X(1)
End Sub
 
Upvote 0
Try this code
VBA Code:
Sub SplitText()
Dim T As Integer
Dim Str As String, Rsl1 As String, Rsl2 As String
Str = "ABC_564_tryre"

For T = 1 To Len(Str)
    If Mid(Str, T, 1) = "_" Then
    Rsl1 = Left(Str, T - 1)
    Rsl2 = Right(Str, Len(Str) - T + 1)
    Exit For
    End If
Next T

End Sub
 
Upvote 0
You will need to use the Split function. In your example above, you would get the desire result with the following:
VBA Code:
TestString = "ABC_12345_asd"
Results = Split(TestString, "_", 2)

The resulting two parts are now stored in the Results variable as Results(0) and Results(1). To help you understand how it works, I prepared a short subroutine below so you could see/test the process. When using the Split function, it removes the delimiting character ( the undersscore ) so, as below, you need to just put it back in.

VBA Code:
Sub SplitString()

Dim TestString As String, Results As Variant

TestString = "ABC_12345_asd"
Results = Split(TestString, "_", 2)
Debug.Print Results(0)          '  Output:    ABC
Debug.Print Results(1)          '  Output:    12345_asd

Results(1) = "_" & Results(1)   '  Need to put the underscore back before the second part

Debug.Print Results(1)          '  Output:    _1234_asd

End Sub

Hope that helps.
Thank you, it's brilliant. I will investigate what other VBA functions exist.
 
Upvote 0

Forum statistics

Threads
1,215,785
Messages
6,126,890
Members
449,347
Latest member
Macro_learner

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