VBA: Save Files based on cell value. Apply Trim function when saving

TheHack22

Board Regular
Joined
Feb 3, 2021
Messages
121
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hi All,

I have this code below: It works fine, except for the cell value in A2 (Downloaded Files) contains leading and trailing spaces. So the SaveAs will fail if I don't manually resolve all the leading and Trailing spaces in all the files in A2.

Is there a way to incorporate the TRIM Function into my code?


Sub FileNameAsCellContent()
Dim FileName As String
Dim Path As String
Application.DisplayAlerts = False
Path = "C:\Users\C-TLG\Box\Amp_Page_Views\"
FileName = Range("A2").Value & ".xlsx"
ActiveWorkbook.SaveAs Path & FileName, xlOpenXMLWorkbook
Application.DisplayAlerts = True
ActiveWorkbook.Close
End Sub

Imran
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Spaces are allowed in filenames though.
But you could try to trim your range.

VBA Code:
Application.Trim(Range("A2").Value)
 
Upvote 0
Spaces are allowed in filenames though.
But you could try to trim your range.

VBA Code:
Application.Trim(Range("A2").Value)
@JEC

Thanks very much for your help and prompt response. How do I incorporate this?

Sub FileNameAsCellContent()
Dim FileName As String
Dim Path As String
Application.DisplayAlerts = False
Path = "C:\Users\C-TLG\Box\Amp_Page_Views\"
FileName = Application.Trim(Range("A2").Value) & ".xlsx"
ActiveWorkbook.SaveAs Path & FileName, xlOpenXMLWorkbook
Application.DisplayAlerts = True
ActiveWorkbook.Close
End Sub

Is this correct?

Imran
 
Upvote 0
Yes, try to run it now
 
Upvote 0
Yes, try to run it now
I'm getting this error.



1631728930424.png
 
Upvote 0
You have characters in range("A2") which are not allowed in filenames
 
Upvote 0
Here is the file name as shown by the Locals window. This looks correct

1631729499248.png


Also here is an example of one of the Excel WorkBook cell A2. I only noticed a "-". No other character seems as not allowed.

1631729702439.png
 

Attachments

  • 1631729667096.png
    1631729667096.png
    24.2 KB · Views: 8
Upvote 0
I do not know if this is what is causing your issue, but you should NEVER use reserved words as names of variables, procedures, or functions. It can cause errors and unexpected results. Reserved words are words that are already used by VBA for things like functions, properties, and methods.

BOTH "Plan" and "Filename" are reserved words.
Try naming them something else and see if that makes any difference.
 
Upvote 0
Are you sure your Path exists?
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,211
Members
448,554
Latest member
Gleisner2

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