quick naming question

Mr_Ragweed2

Board Regular
Joined
Nov 11, 2022
Messages
145
Office Version
  1. 365
Platform
  1. Windows
Sorry for a dumb question here but how do i take the .xlsx out of a filename/string? The filename/string is dynamic in length.
I have some .xlsx files i am saving as .PDF files and for some reason (prolly because i coded it that way) it is saving them as a "file name.xlsx.pdf" . The "file name.xlsx" is a String. So i just want to edit that string and lop off the .xlsx. So basically lop off the last 5 characters of a string. I'm no good at the LEN/UBound/LBound/LEFT/MID/Right thing if it has something to do with that. haha
This is as far as i have gotten on that bit.
VBA Code:
fileName = File.Name
        
    newName =
To add background i used filename as a String in a pathway to save to another location. I now want to switch the string in that save as pathway to newName. Then i kill the original filename.
VBA Code:
If fileName Like "*SWO*.xlsx" Then
    
        ActiveWorkbook.SaveAs fileName:= _
            "https://abcd.sharepoint.com/teams/mycompany/Shared%20Documents/General/Crystal,%20Mary,%20Anna/Propane%20Service%20Work%20Orders/" _
            & newName & ".PDF"   'newName used to be filename but that's when the issues arose
            ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF
        Kill "C:\Users\" & MyName & "\OneDrive - MyCompany\Desktop\Propane Forms\" & fileName
I can put the whole code here if needed. It's not super long and is a loop.

Thanks in advance.
 

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.
I'm an idiot. Would be as simple as this?
VBA Code:
newName = Left(fileName, Len(fileName) - 5)
 
Upvote 0
Just a bit more generic (caters for the likes of .xls as well as .xlsx)
VBA Code:
Left(Filename, InStrRev(Filename, ".", -1, vbTextCompare) - 1)
 
Last edited:
Upvote 0
So if i understand it correctly, what you did was tell it to lop off everything from right to left until it found a "." and then lop off the "." ?
 
Upvote 0
The InStrRev is finding the position of the first period working from the right, the last -1 is making the position 1 character less (the first -1 just tells it to start it's search at the first character [it could be omitted as it is the default]).

Then it is just Left(FileName, found position)

Edit: if you want to shorten it then you can use
VBA Code:
Left(Filename, InStrRev(Filename, ".", , 1) - 1)
 
Last edited:
Upvote 0
Solution
That's cool. Thank you for taking the time to teach and explain it to me!
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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