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

Imran_IsshackNY

Board Regular
Joined
Feb 3, 2021
Messages
83
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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

JEC

Well-known Member
Joined
Aug 21, 2021
Messages
571
Office Version
  1. 365
Platform
  1. Windows
Spaces are allowed in filenames though.
But you could try to trim your range.

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

Imran_IsshackNY

Board Regular
Joined
Feb 3, 2021
Messages
83
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
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
 

JEC

Well-known Member
Joined
Aug 21, 2021
Messages
571
Office Version
  1. 365
Platform
  1. Windows
Yes, try to run it now
 

Imran_IsshackNY

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

ADVERTISEMENT

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



1631728930424.png
 

JEC

Well-known Member
Joined
Aug 21, 2021
Messages
571
Office Version
  1. 365
Platform
  1. Windows
You have characters in range("A2") which are not allowed in filenames
 

Imran_IsshackNY

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

ADVERTISEMENT

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: 4

Joe4

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

JEC

Well-known Member
Joined
Aug 21, 2021
Messages
571
Office Version
  1. 365
Platform
  1. Windows
Are you sure your Path exists?
 

Forum statistics

Threads
1,148,397
Messages
5,746,460
Members
424,021
Latest member
naimathulla

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