VBA - replace a character in a range to save

The Ruff Report

New Member
Joined
Jun 17, 2023
Messages
19
Office Version
  1. 365
Platform
  1. Windows
In the below I'm saving a file with a name based on the values in cells A1 and B1. Cell B1 though contains prohibited characters. In my case, they are "[", "]" and of course "/"

I would like to remove the prohibited characters from the file name but not alter the text in cell B1. Any help is appreciated.
------------------------------------------------------------------------------------------------------------------------------------------------

Dim X As Integer
Dim astrLinks As Variant
Dim Path As String
Dim filename As String

For X = 2 To 200

If Sheets("Suppliers").Range("A" & X).Value <> "" Then

Sheets("Suppliers").Range("A" & X).Copy
Sheets("DA Summary").Range("B1").PasteSpecial (xlPasteValues)
Application.CutCopyMode = False

Sheets("Suppliers").Range("A" & X).Copy
Sheets("Price Point Data").Range("E1").PasteSpecial (xlPasteValues)
Application.CutCopyMode = False

Sheets(Array("Invoice", "DA Summary", "Price Point Data")).Copy
Sheets("DA Summary").Select
Range("A1").Select

astrLinks = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
ActiveWorkbook.BreakLink _
Name:=astrLinks(1), _
Type:=xlLinkTypeExcelLinks

Path = "C:\Test Reports\"
filename = Range("A1") & " " & Range("B1")
ActiveWorkbook.SaveAs filename:=Path & filename & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Application.CommandBars("Workbook Links").Visible = False
Sheets("Invoice").Select
Range("A1").Select
ActiveWorkbook.Save
ActiveWorkbook.Close
Windows("DA Chopper New.xlsm").Activate

End If

Next X


End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi, perhaps adding this into your routine solves your problem. Its essentially an Array of "illegal characters" that you can add to or remove as you see fit. These are then removed from a variable in the following lines.

VBA Code:
Dim X As Integer
Dim astrLinks As Variant
Dim Path As String
Dim filename As String


For X = 2 To 200

If Sheets("Suppliers").Range("A" & X).Value <> "" Then

Sheets("Suppliers").Range("A" & X).Copy
Sheets("DA Summary").Range("B1").PasteSpecial (xlPasteValues)
Application.CutCopyMode = False

Sheets("Suppliers").Range("A" & X).Copy
Sheets("Price Point Data").Range("E1").PasteSpecial (xlPasteValues)
Application.CutCopyMode = False

Sheets(Array("Invoice", "DA Summary", "Price Point Data")).Copy
Sheets("DA Summary").Select
Range("A1").Select

astrLinks = ActiveWorkbook.LinkSources(Type:=xlLinkTypeExcelLinks)
ActiveWorkbook.BreakLink _
Name:=astrLinks(1), _
Type:=xlLinkTypeExcelLinks

'Remove special Chars from filename in B1
CellB1 = Range("B1")
IllegalArray = Array("[", "]", "<", ">", "|", "/", "*", "\", ".", "?", """")
For Y = LBound(IllegalArray) To UBound(IllegalArray)
    CellB1 = Replace(CellB1, IllegalArray(Y), "", 1)
Next Y
            

Path = "C:\Test Reports\"
filename = Range("A1") & " " & CellB1 'note variablename change here from your original cell Ref
ActiveWorkbook.SaveAs filename:=Path & filename & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Application.CommandBars("Workbook Links").Visible = False
Sheets("Invoice").Select
Range("A1").Select
ActiveWorkbook.Save
ActiveWorkbook.Close
Windows("DA Chopper New.xlsm").Activate

End If

Next X
 
Upvote 1
Solution
If it is always the same three illegal characters then another option is something like this.

VBA Code:
    filename = Range("A1") & " " & Replace(Replace(Replace(Range("B1").Value, "[", ""), "]", ""), "/", "")
 
Upvote 1
Thank you. That is helpful. I was playing around with replace and was wondering how to approach for multiple replaces. I didn't know it was nested like that. Makes sense.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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