Editing and taking a part of a string

eawachte

New Member
Joined
Jan 14, 2011
Messages
29
I have a string it is LF_LCA_079_010813_Curved_1_4_Short it reads into vba from a file and is give the variable name LFDesc and dim LFDesc as string

Now i would like to pull LCA_079 from that string and create a new string with this text. I can do it in excel using the MID and Find functions, but i really want to do it in vba to clean the sheet up.

I'm having a hard time figuring out the instr function for vba could you help me with the code PLEASE!!

Thank you in advance

Gene
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
newStr = mid(LFDesc,instr(1,LFDesc,"LCA",vbTextCompare),7)
 
Upvote 0
newStr = mid(LFDesc,instr(1,LFDesc,"LCA",vbTextCompare),7)

Thank you for the response!

I apologize i was thinking through my question while i grabbed lunch and remembered I had one more condition i needed to add to my post.

With LF_LCA_079_010813_Curved_1_4_Short I still want to start where you started with the last post and get "LCA", but this could be any length, how would I fine the (2nd) "_" after 079.

I assuming the "7" in your code is the amount of characters?

Thank you!
 
Upvote 0
yes the 7 is the number of characters to extract.

Finding the second "_" can be a bit tricky and a long formula

I hope somebody can suggest something better than this:

Code:
Function getSecondUnder(sourceString) As String
Dim stStart As Integer
Dim stEnd As Integer
Dim newStr As String


stStart = 1
stEnd = Len(sourceString)
If (InStr(1, sourceString, "LCA", vbTextCompare)) Then
    stStart = InStr(1, sourceString, "LCA", vbTextCompare)
    stEnd = InStr(stStart + 4, sourceString, "_")
    stEnd = InStr(stEnd + 1, sourceString, "_")
    stEnd = stEnd - stStart
End If
    
getSecondUnder = Mid(sourceString, stStart, stEnd)
End Function
 
Upvote 0
I have a string it is LF_LCA_079_010813_Curved_1_4_Short it reads into vba from a file and is give the variable name LFDesc and dim LFDesc as string

Now i would like to pull LCA_079 from that string and create a new string with this text. I can do it in excel using the MID and Find functions, but i really want to do it in vba to clean the sheet up.

I'm having a hard time figuring out the instr function for vba could you help me with the code PLEASE!!
Does this code do what you want...
Code:
Function Get2ndAnd3rd(SourceString As String) As String
  Dim Parts() As String
  Parts = Split(SourceString, "_")
  Get2ndAnd3rd = Parts(1) & "_" & Parts(2)
End Function
 
Upvote 0
ok looking at my own code I see it can be done in 1 long formula with a few substitutions:

newStr= Mid(LFDesc, InStr(1, LFDesc, "LCA", vbTextCompare), InStr(InStr(InStr(1, LFDesc, "LCA", vbTextCompare) + 4, LFDesc, "_") + 1, LFDesc, "_") - InStr(1, LFDesc, "LCA", vbTextCompare))

but it isn't pretty.
 
Upvote 0
I thought about splitting but I was not sure if the LCA would always be the beginning of a part.

I think they would want parts 1-4
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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