Variable Directory

bjurney

Active Member
Joined
Aug 24, 2009
Messages
320
I am trying to get this code (or a code like it, or any code for that matter!) to find a certain file based on the user input. The end result will import data from that certain file but for test purposed, a message box is good enough to tell me that it works. The format of the file will always be along the lines of My File 2011-02-28. Depending on the date the user puts in, it will need to find that file. Every time I run the follwing code though I get an Object Required error. Im at a loss where I should go from here.

Code:
Sub import()
result = InputBox("What is the date of the file you are importing? Date Must be in MM/DD/YYYY format", _
    "Enter Date", Date)
    mydate = Format(result, "MM/DD/YYYY")
    mydir = ("C:\My File " & Year(mydate) & "-" & Month(mydate) & "-" & Day(mydate).xls)
If Dir(mydir) <> "" Then
MsgBox ("Exists")
End If
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Is this a typo?:)
Code:
Day(mydate).xls
 
Upvote 0
Im not sure how to format the .xls. I need it to to specify that it is a .xls file though.
 
Upvote 0
Isn't it the date you want to format, rather than the filename itself?
Code:
Option Explicit

Sub MyImport()
Dim result
Dim strFileName
Dim strDir As String
 
    strDir = "C:\"
 
    result = InputBox("What is the date of the file you are importing? Date Must be in MM/DD/YYYY format", _
                      "Enter Date", Date)

    If IsDate(result) Then
 
        strFileName = "My File " & Format(DateValue(result), "yy-mm-dd") & ".xls"

        If Dir(strDir & strFileName) <> "" Then
            MsgBox "Exists"
        Else
            MsgBox "Doesn't exist"
        End If

    Else
        MsgBox "Please enter a valid date."
    End If
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,558
Members
452,928
Latest member
101blockchains

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