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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
59,036
Office Version
  1. 365
Platform
  1. Windows
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?
 

Sah213

New Member
Joined
Feb 25, 2014
Messages
10
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?
 

Marc L

Well-known Member
Joined
Apr 5, 2021
Messages
1,241
Office Version
  1. 2010
Platform
  1. Windows
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
 
Solution

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
59,036
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Will there always be exactly 3 underscores?
Or might there be less/more?
 

Marc L

Well-known Member
Joined
Apr 5, 2021
Messages
1,241
Office Version
  1. 2010
Platform
  1. Windows
Post #4 variation : If UBound(V) > 0 Then MsgBox V(UBound(V) - 1) …​
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
59,036
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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?
 

Marc L

Well-known Member
Joined
Apr 5, 2021
Messages
1,241
Office Version
  1. 2010
Platform
  1. Windows
Oh you did not well read the initial post ! :rolleyes:
And as with the If codeline it works « dynamicly » obviously whatever the « string » …​
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
59,036
Office Version
  1. 365
Platform
  1. Windows
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))
 

Forum statistics

Threads
1,141,496
Messages
5,706,682
Members
421,464
Latest member
apicsofcake

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
Top