Check File Extension

Swat

New Member
Joined
Feb 9, 2007
Messages
36
I need to check that the person who is entering a file name into an input box is also entering the file extension. For example if its an excel spreadsheet is should be .xls and a text file would be .txt.

Could some one help as I cannot find a solution to my question.

Thanks.
 

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.
This is what I've come up with so far but it does not seem to work correctly.

If Right(MyInput1, 4) <> ".xls" Then
MsgBox "File extension incorrect or missing"
Exit Sub
End If
 
Upvote 0
This is what I've come up with so far but it does not seem to work correctly.

If Right(MyInput1, 4) <> ".xls" Then
MsgBox "File extension incorrect or missing"
Exit Sub
End If

Worked for me, what is the full code you used
 
Upvote 0
I'm getting into a bit of a pickle with the IF statements and was hoping that someone could check the code below.

At one point I had it so that if the user selected either No or Cancel on the twp message boxes then the the message "Halted By User" would appear. Since adding the additional IF statement for checking the file extension then it does not work.

I'd appreciate it if someone could tell me what I've done wrong.

Dim MyPrompt1
Dim MyInput1 As String

Sheets("Sheet1").Activate
MyPrompt1 = MsgBox("The current workbook is " & Range("B4") & vbCrLf & "Would you like to change it?", vbYesNo)

If MyPrompt1 = vbYes Then

MyInput1 = UCase(InputBox("Enter new workbook date"))

Range("C3") = MyInput1

If MyInput1 = Range("B4") Then
MsgBox "Samefile name unable to proceed"
Exit Sub
End If

If Range("D3").Value <> ".XLS" Then
MsgBox "Missing or Incorrect File Extension"
Exit Sub
Else
MsgBox "OK to continue"
End If
MsgBox "Halted By User!"
End If
 
Upvote 0
try

If UCase(Range("D3").Value) <> ".XLS" Then
MsgBox "Missing or Incorrect File Extension"
Exit Sub
Else
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,194
Members
449,072
Latest member
DW Draft

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