Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting String With LastIndexOf() in VBA

Tags:

excel

vba

How to split A string By LastIndexOf() in VBA

I have a string which can have these kind of values

Dim input As String
   input =  "customName_D3"
   input =  "Custom_Name_D3"
   input =  "my_Custom_Name_D3"

like it can have many "_" in it but after last"_" it contains cell name I want to split this string to get cell name and other remaning part as two different strings something like

cellName = D3
remainingString = my_custom_Name
like image 949
Addy Avatar asked Dec 11 '14 08:12

Addy


People also ask

How do you split a string in VBA?

The VBA Split Function is used is to split a string of text into an array. The text is split based on a given delimiter – e.g. a comma, space, colon etc. You can see that each item separated by the colon sign. We call the colon sign the delimiter.

How do I split a word in Excel VBA?

Step 2: Declare three variables. Step 3: Now, for the defined variable, My Text assigns the word “My Name is Excel VBA.” Step 4: Now, apply the VBA Split String function for the “My Result” variable. Step 5: Expression is our text value.


1 Answers

Dim strInput As String
Dim strName As String
Dim strCellAddress As String
Dim iSplit As Integer

strInput = "my_Custom_Name_D3"

iSplit = InStrRev(strInput, "_")

strName = Left(strInput, iSplit - 1)
strCellAddress = Mid(strInput, iSplit + 1)

MsgBox "Name: " & vbTab & strName & vbCrLf & "Cell: " & vbTab & strCellAddress
like image 172
Jean-François Corbett Avatar answered Oct 05 '22 13:10

Jean-François Corbett