Extract data

Excel2006

Active Member
Joined
Dec 19, 2006
Messages
455
Hi

Please could some tell me using vba code how to extract

1758

form the following info.

c:\pot\1750 GASplane\qq1758.xls

I have tried using find and mid commands but always got an error.

I need to do this for several dirs and filenames.

Thx.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

DonkeyOte

MrExcel MVP
Joined
Sep 6, 2002
Messages
9,124
can you provide some more info in terms of the other searches you need to do -- ie will the string you want always be 4 characters in length and always precede the .xls ?
 
Upvote 0

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Is the number always in the same position?

Code:
Sub Test()
    Const Str As String = "c:\pot\1750 GASplane\qq1758.xls"
    Dim TempArr As Variant
    TempArr = Split(Str, "\")
    MsgBox Mid(TempArr(UBound(TempArr)), 3, 4)
End Sub
 
Upvote 0

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
Also
Code:
Sub test()
Dim myFilePath As String
myFilePath = "c:\pot\1750 GASplane\qq1758.xls"
With CreateObject("VBScript.RegExp")
    .Pattern = "\d+"
    If .test(myFilePath) Then MsgBox .execute(myFilePath)(0)
End With
End sub
 
Upvote 0

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
OOps
Thanks Andrew
Code:
Sub test()
Dim myFilePath As String
myFilePath = "c:\pot\1750 GASplane\qq1758.xls"
With CreateObject("VBScript.RegExp")
    .Pattern = ".*(\d+)\.xls"
    If .test(myFilePath) Then MsgBox .replace(myFilePath, "$1")
End With
End sub
 
Upvote 0

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
Jindon, that returns 8.
Is that right ?....
I don't know why...
How about
Code:
Sub test()
Dim myFilePath As String, m As Object
myFilePath = "c:\pot\1750 GASplane\qq1758.xls"
With CreateObject("VBScript.RegExp")
    .Pattern = "d+"
    .Global = True
    Set m = .execute(myFilePath)
    If m.Count > 0 Then MsgBox m.item(m.count-1)
End With
End sub
 
Upvote 0

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
Andrew,
Thanks very much for testing the code.
I need to go now, so I will post tomorrow.
 
Upvote 0

Forum statistics

Threads
1,191,308
Messages
5,985,904
Members
439,986
Latest member
DaveTee

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