How to read a filename as a string variable?

Zoticos

New Member
Joined
Jan 29, 2011
Messages
16
I’m having a problem in regards to comparing a string variable with a filename.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Here’s my sample.<o:p></o:p>
<o:p></o:p>
Textbox.text input is: “new york”<o:p></o:p>
My file name is: “C:\DATA\Location – New York.bmp”<o:p></o:p>
<o:p></o:p>
How can I read the filename as a string, and then compare the Textbox.text string with the filename?<o:p></o:p>
<o:p></o:p>
The compare needs to ignore the case and see if “new york” exist in the filename else exit sub.<o:p></o:p>
<o:p></o:p>
Thanks for any help!<o:p></o:p>
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try a variation of this:
Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] CommandButton1_Click()
   [COLOR=darkblue]Dim[/COLOR] sPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
   sPath = "C:\Data\Location - " & TextBox1.Value & ".bmp"
 
   [COLOR=darkblue]If[/COLOR] Dir(sPath) = "" [COLOR=darkblue]Then[/COLOR]
      MsgBox "File does not exist!"
   [COLOR=darkblue]Else[/COLOR]
      MsgBox "File exists!"
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
End [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
You can use InStr to see if one string is within another.
Code:
Dim strFilename As String
Dim strTxtBox As String
Dim pos As Long
    strTxtBox = "New york"
    
    strFilename = "C:\DATA\Location – New York.bmp"

    pos = InStrRev(strFilename, strTxtBox, , vbTextCompare)
    
    MsgBox pos
 
Upvote 0

Forum statistics

Threads
1,224,567
Messages
6,179,571
Members
452,927
Latest member
whitfieldcraig

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