Replace illegal characters when naming file with cell value

2Took

Board Regular
Joined
Jun 13, 2022
Messages
203
Office Version
  1. 365
Platform
  1. Windows
Cell A2 contains the following:

ABC123 List : 8/5/2022 2:53:31 PM

My code below encounters following error:
2022-08-05_15h08_11.png


How to Replace illegal characters when naming file with cell value?

VBA Code:
Sub MySaveTabName()
With CreateObject("Scripting.FileSystemObject")
ActiveSheet.Name = "ABC - " & Format(Now(), "MM.dd.yy")
MyName = ActiveSheet.Range("A2").Value
End With
Const csPath As String = "C:\Test\"
ActiveWorkbook.SaveAs Filename:=csPath & MyName & ".xlsx"
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
"/" are not legal characters that can be used in file names.
So you will need to either change what is contained in cell A2, or "clean it up" first in your VBA code.

Why not just use the same formula you are using for the sheet name, i.e.
VBA Code:
MyName = "ABC - " & Format(Now(), "MM.dd.yy")
 
Upvote 0
"/" are not legal characters that can be used in file names.
So you will need to either change what is contained in cell A2, or "clean it up" first in your VBA code.

Why not just use the same formula you are using for the sheet name, i.e.
VBA Code:
MyName = "ABC - " & Format(Now(), "MM.dd.yy")
Did cross my mind, but would be interesting to see a VBA solution to replace the illegal characters and in the file name itself, not on the worksheet. Perhaps even if there's a mapping collection of some kind already in Excel that identifies all the illegals and maps them individually to suitable legal counterparts...
 
Upvote 0
but would be interesting to see a VBA solution to replace the illegal characters and in the file name itself, not on the worksheet
You could use the REPLACE function to replace all the backslashes with something else, like dashes, i.e.
VBA Code:
    Dim fname As String
    fname = Replace(Range("A2"), "/", "-")
    MsgBox fname

Perhaps even if there's a mapping collection of some kind already in Excel that identifies all the illegals and maps them individually to suitable legal counterparts...
People have done stuff like this before. If you are interested in seeing the code that people have come up, just do some Google searches.
Here is one to get your started, but there are tons more you can find.
 
Upvote 0

Forum statistics

Threads
1,214,879
Messages
6,122,065
Members
449,064
Latest member
scottdog129

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