VBA to extract string between 2 special characters (where spec. characters appear twice)

Sah213

New Member
Joined
Feb 25, 2014
Messages
10
I'm looking for some VBA to extract the text: 1620011017 from the below string;

Inv_1620056384_1620011017_1034258

There could be any number of characters, so I think I would need to define it based on the fact it's the string between the 2 underscores.
I think starting from the right of the string would be easiest as starting from the left it's the 2nd string between the _ characters. Starting from the right, it's the first string that meets that criteria.

If anyone could help with their expertise, I'd really appreciate it!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
But by your rules (return the string between two underscores), 1620056384 is also between two underscores. So you have two values that meet your criteria.
So how do you know which one to return?
 
Upvote 0
Hi Joe,

Yes completely agree, so I either need;

a) a way to select the 2nd string that meets this criteria if substringing from the left, or b) if there's a way to substring from the right - then it's the first criteria that would be returned?
 
Upvote 0
A VBA demonstration as a beginner starter :​
VBA Code:
Sub Demo1()
    V = Split("Inv_1620056384_1620011017_1034258", "_")
    If UBound(V) > 1 Then MsgBox V(2)
End Sub
 
Upvote 0
Solution
Will there always be exactly 3 underscores?
Or might there be less/more?
 
Upvote 0
Post #4 variation : If UBound(V) > 0 Then MsgBox V(UBound(V) - 1) …​
 
Upvote 0
A VBA demonstration as a beginner starter :
Why not just give them a dynamic solution instead of a hard-coded example that only works for the one example?
 
Upvote 0
Oh you did not well read the initial post ! :rolleyes:
And as with the If codeline it works « dynamicly » obviously whatever the « string » …​
 
Upvote 0
Yes - there will indeed always be 3.
Here is a formulaic solution that should work on any value in cell A1, provided that the strings in between the underscores do not get too long:
Excel Formula:
=TRIM(LEFT(RIGHT(SUBSTITUTE(A1,"_",REPT(" ",100)),200),100))
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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