file naming

tony.reynolds

Board Regular
Joined
Jul 8, 2010
Messages
97
I have a userform that the user can enter a new name for the workbook so the original is kept

i obviously need to limit the user data to certain characters so "\" is not allowed and also the original file name is not allowed

if the current excel file name is XYZ that is not allowed.... and

how can I limit users from entering these characters? \ / : * ? " < > |

The following code i have allows text only but obviously dosnt stop user entering a number following a letter and the above characters. Stillit it will show you what im trying to do.

Code:
Private Sub NewFileName_Change()
If IsNumeric(NewFileName.Value) Then
MsgBox "cannot use ' \ / : * ? " < > |' characters"
NewFileName.Value = ""
End If
End Sub

Anyons help would be appreciated
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
try something like this

Aint = InStr(1, NewFileName, Chr(XX))
If Aint > 0 Then
MsgBox "The character ' " & Chr(XX) & " ' is not allowed"
End If

Where XX=ASCII code for charater of insterest
 
Upvote 0
I would do something like this to evaluate the string

Code:
Function EvaluateString(strString As String) As String
   
    Dim i As Long
    For i = 1 To Len(strString)
        Select Case Asc(Mid(strString, i, 1))
            Case 65 To 90, 97 To 122: '(65-90UpperCaseAtoZ, 97-122LowerCaseatoz)
            'Case 32, 48 To 57 (32Space,48-57ZerotoNine)
            Case Else
                EvaluateString = Chr(Asc(Mid(strString, i, 1)))
                Exit For
        End Select
    Next
End Function
Private Sub NewFileName_Change()
    Dim result As String
    result = EvaluateString(NewFileName.Value)
    If result <> "" Then
        MsgBox "cannot use " & result & " characters"
        NewFileName.Value = ""
    End If
End Sub

thanks
tigs
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,400
Members
449,448
Latest member
Andrew Slatter

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