Getting a file name using DIR()

USAMax

Well-known Member
Joined
May 31, 2006
Messages
843
Office Version
  1. 365
Platform
  1. Windows
I am trying to open two csv files, the first one starts with ca_ewor and the second starts with ca_pr. If I run the following commands...

Code:
    Dim cf As String
    cf = Dir(Worksheets("Sheet1").Cells(1, 2) & "\ca_ewor*.csv")
    cf = Dir(Worksheets("Sheet1").Cells(1, 2) & "\ca_pr*.csv")

cf gives me the first file's name then cf gives me the second file's name.


If I open the first file before getting the name of the second file then I get a, "Run-time error '9': Subscript out of range"
Code:
    Dim cf As String
    cf = Dir(Worksheets("Sheet1").Cells(1, 2) & "\ca_ewor*.csv")
        Workbooks.OpenText Filename:=cf, Origin:=65001, _
            StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
            xlDoubleQuote, ConsecutiveDelimiter:=False, _
            Tab:=False, Semicolon:=False, Comma:=True, _
            Space:=False, Other:=False                      'Open the Cummins-Allison Problem Report
    cf = Dir(Worksheets("Sheet1").Cells(1, 2) & "\ca_pr*.csv")

I know I could use two different variables and I am sure there are other ways around this but can someone tell me why this is happening.

OTHER FACTS
Each file has a different number of columns and I do expect the number of columns to change if this is related.
I am using Excel 2010
 
Last edited:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Dir returns file name without path.
You need to add path (see in red) to Workbooks.OpenText Filename:=Worksheets("Sheet1").Cells(1, 2) & "\" & cf, ...
 
Upvote 0
Thank you Vladimir,

You are correct, I did forget to include the path in the open statement. I corrected this but I am still getting an error in the second statement when the file is open. cf = Dir(Worksheets("Sheet1").Cells(1, 2) & "\ca_pr*.csv")

If I close the file then I don't have a problem. Could it be that it cannot read the name of the file that is open?

Dave
 
Upvote 0
Dave

The second Dir will be referring to the newly opened workbook for the path and I've a feeling that workbook doesn't have a worksheet named 'Sheet1'.

Try putting the path in a variable at the top of the code.
Code:
Dim cf As String
Dim strPath As String

    strPath = Worksheets("Sheet1").Cells(1,2).Value

    cf = Dir(strPath & "\ca_ewor*.csv")
 
Upvote 0
BRILLIANT! Once I read that I realized the problem. It was like a kick in the head when I read it.

Thank you!
 
Upvote 0

Forum statistics

Threads
1,216,063
Messages
6,128,559
Members
449,458
Latest member
gillmit

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