Question : removing invalid characters in vba

I have some code wich is returning an error, newsheetname is a variable which sometimes has characters such as / or \.  I cant then use that variable to name a sheet and so I was simply going to replace all invalid characters with an -

But where am I going wrong and getting

variable uses an automation type not supported in Visual Basic

on the goodnewsheetname line

Code Snippet:
1:
2:
3:
4:
Sheets.Add After:=Sheets(Sheets.Count)
    Myarray = Array(":", "/", "\", "?", "*", "[", "]")
    goodnewsheetname = Replace(Newsheetname, Myarray, "-")
    ActiveSheet.Name = goodnewsheetname

Answer : removing invalid characters in vba

you could use a function like this:

1:
2:
3:
4:
5:
6:
7:
8:
9:
Public Function GetValidSheetName(ByVal strOrig As String) As String
   Dim varChars
   Dim varItem
   varChars = Array(":", "\", "/", "?", "*", "[", "]")
   For Each varItem In varChars
      strOrig = Replace$(strOrig, varItem, "")
   Next varItem
   GetValidSheetName = left$(strOrig, 31)
End Function
Random Solutions  
 
programming4us programming4us