SaveAs VBA Removing Special Characters

tcorcoran15

New Member
Joined
Feb 16, 2017
Messages
22
Hello,

I have the following code which names a sheet after a specified cell and then copies into a new workbook.

The next stage attempts to save the workbook after another cell value however this is where I became stuck.

Cell D2 can contain special characters so the save as isn't working correctly.

Is it possible to save as a specified cell removing the special characters? Without removing the special characters from the cell?

For example, ABC LTD T/A TOMS CAKE SHOP would become ABC LTD TA TOMS CAKE SHOP

Any help is greatly appreciated.

Code:
Set wh = Worksheets(ActiveSheet.Name)
        ActiveSheet.Name = wh.Range("Q2").Value
        
        ActiveSheet.Copy
        
        Application.DisplayAlerts = False    ' suppress overwrite warning message
        ActiveWorkbook.SaveAs Filename:="X:\Company Name\Product\Product - Live\Reports\Credit Control Files\" & Range("D2"), _
                          FileFormat:=51, _
                          CreateBackup:=False
                          
        Application.DisplayAlerts = True
        ActiveWorkbook.Close SaveChanges:=False
 
Last edited:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
This link show you how to right a function that you could use in an empty cell then refer to that cell for the name of the workbook then you could delete the cell content. https://www.extendoffice.com/documents/excel/3483-excel-remove-special-characters.html

Function RemoveSpecial(Str As String) As String
'updatebyExtendoffice 20160303
Dim xChars As String
Dim I As Long
xChars = "/"
For I = 1 To Len(xChars)
Str = Replace$(Str, Mid$(xChars, I, 1), "")
Next
RemoveSpecial = Str
End Function
 
Last edited:
Upvote 0
Thanks for your help Trevor.

I used the above function, although I wanted to stay away from using empty cells so tried the following and found it worked perfectly.

Code:
Set wh = Worksheets(ActiveSheet.Name)
        ActiveSheet.Name = wh.Range("Q2").Value

        ActiveSheet.Copy

        Application.DisplayAlerts = False    ' suppress overwrite warning message
        ActiveWorkbook.SaveAs Filename:="X:\Company Name\Product\Product - Live\Reports\Credit Control Files\" & RemovePunctuation(Range("D2")), _
                          FileFormat:=51, _
                          CreateBackup:=False

        Application.DisplayAlerts = True
        ActiveWorkbook.Close SaveChanges:=False
 
Upvote 0
Pleased to read you have a working solution. Thank you for letting me know.
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,893
Members
449,097
Latest member
dbomb1414

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