Macro to check words in cell dont use things like "/" in the file name??

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

please excuse my ignorance but i don't know what the official name for these things would be, but if you use excel a lot I'm sure you can understand what I'm trying to do

I use cell D11 as the cell that holds the document name

I have a macro that works great, that takes this name and saves the document in it current location.
this is all fine,
the problem is sometimes someone will try save the document using a character excel cant include in the file name easily and we get an error
for example , it might be named "21/23 the street"

so what I'd like is a way to check if the cell contains any problem letters ?
if it does, the ideal would be to tell the person what is being used that cant be used in a message box?? eg "You can not use / in a document name, please change this!"

anyone able to help me? I don't know which chartreus are a problem but even if you could just do it to check the propular ones like "/" then this would be a huge help

thanks

Tony
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
For what you are doing, this would probably be sufficient.
VBA Code:
Public Function IsValidFileName(FileName As String) As Boolean
    IsValidFileName = Not FileName Like "*[<>:/\|?*]*"
End Function

Sub YourCurrentSub()
    If Not IsValidFileName Then
        MsgBox "Message to user"
        Exit Sub
    End If
End Sub
 
Upvote 0
Different approach:
VBA Code:
Sub IterateCharacters_String()
    Dim sStr As String, Chars As Variant, i As Long
    Chars = Array("\", "/", ":", "*", "?", "<", ">", "|", "[", "]", """")
    With Sheets("Sheet1").Range("D11")
        For i = LBound(Chars) To UBound(Chars)
            If InStr(.Value, Chars(i)) > 0 Then
                If sStr = "" Then sStr = Chars(i) Else sStr = sStr & " " & Chars(i)
            End If
        Next i
    End With
    MsgBox ("You can not use the following characters in a document name." & Chr(10) & sStr & Chr(10) & "Please change them!")
End Sub
You can add characters to the array if necessary.
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,981
Members
448,934
Latest member
audette89

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