How to trim a filename in a macro

dktenor

New Member
Joined
Jul 9, 2003
Messages
33
I have a macro that requests a filename and puts it in a variable "infile". I need to have the ".xls" stripped from the filename and the result put into another variable - call it "insheet".

For example:

infile = "2003-06.xls"
insheet = "2003-06"

Thanks,
doug
 
Then how about --

insheet = Mid(infile, InStrRev(infile, "\") + 1, Len(infile) - InStrRev(infile, ".") + 1)
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Is it possible that I don't have the instrrev function? I'm using Excel 97 (ugh! but blame my forward-looking company)

doug
 
Upvote 0
Quite possible. Take a look at this snippet and see if it will run on '97.

Code:
Sub xx()
Dim infile As String, insheet As String, i As Integer, icount As Integer
infile = "O:\LGD Database\Extract Reformater\2003-02-obligor.xls"
icount = (Len(infile) - Len(Application.WorksheetFunction.Substitute(infile, "\", "")))
For i = 1 To icount
    infile = Mid(infile, Application.WorksheetFunction.Find("\", infile) + 1, 255)
Next i
insheet = Left(infile, Len(infile) - 4)
MsgBox insheet
End Sub
 
Upvote 0
Thanks,
In the meantime - here is what I came up with - and it works.

begin = 1
TESTSTRING = infile
test = Len(infile)
Do Until test = 0
TESTSTRING = Mid(infile, begin, Len(infile) - begin + 1)
test = InStr(TESTSTRING, "\")
begin = begin + test
Loop
insheet = Left(TESTSTRING, Len(TESTSTRING) - 4)
 
Upvote 0
In addition to the other detours you've received for the InstrRev function, you could use the code below. It provides the StrReverse and InstrRev functions in XL97 and when the workbook with the code is run on XL2000 or later, the code is automatically skipped.
Code:
#If VBA6 = 0 Then
Function StrReverse(s As String)
    Dim i As Long
    For i = Len(s) To 1 Step -1
        StrReverse = StrReverse & Mid(s, i, 1)
        Next i
    End Function
Function InstrRev(StringCheck As String, StringMatch As String, _
    Optional Start As Long = 1, Optional Compare As Long = 0)
    InstrRev = Len(StringCheck) _
        - (InStr(Start, StrReverse(StringCheck), StrReverse(StringMatch), Compare) _
        + Len(StringMatch) - 1) + 1
    End Function
    #End If
dktenor said:
Is it possible that I don't have the instrrev function? I'm using Excel 97 (ugh! but blame my forward-looking company)

doug
 
Upvote 0

Forum statistics

Threads
1,217,035
Messages
6,134,121
Members
449,861
Latest member
DMJHohl

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