Help needed with Changing Directories to Import a Text File

Liz_I3

Well-known Member
Joined
Dec 30, 2002
Messages
647
Office Version
  1. 2016
Platform
  1. Windows
Please what is wrong with this code? Sometimes it works and sometimes (most of the time) it does not. What am I missing.

I want to open a text file that is store on the R Dirve in a folder called New Data. There are several files in this folder, each month a new file with a new name will be added, so I what the user to select the correct file from the New Data folder

Thanks
L.

Dim myfile
ChDrive "R"
ChDir "R:\month\December 2005\New Data\ *.txt"

myfile = Application.GetOpenFilename(filefilter:="Text Files (*.txt),.txt", MultiSelect:=True)

For x = LBound(myfile) To UBound(myfile)

Workbooks.OpenText Filename:=myfile(x), Origin:=xlWindows _
, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
, Space:=False, Other:=False, FieldInfo:=Array(1, 2)

Next
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Re: Help needed with Changing Directories to Import a Text F

Liz_I3 said:
...Sometimes it works and sometimes (most of the time) it does not. ...

What is happening when it doesn't work?
 
Upvote 0
HI Barry
Nothing, It just moves on the the next macro.

It seem to read as far as the Chdir, then does not read the Myfile, skips down to the end sub.

L
 
Upvote 0
Try
Code:
Dim myfile
Dim x As Long
ChDrive "G"
ChDir "G:\"

myfile = Application.GetOpenFilename(filefilter:="Text Files (*.txt),.txt", MultiSelect:=True)

For x = LBound(myfile) To UBound(myfile)
    Workbooks.OpenText Filename:=myfile(x), Origin:=xlWindows _
        , StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
        ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
        , Space:=False, Other:=False, FieldInfo:=Array(1, 2)
Next x

Changed Next to Next x
 
Upvote 0
Thanks Barrie
It is now working every time
Thank you
L
 
Upvote 0

Forum statistics

Threads
1,214,527
Messages
6,120,058
Members
448,940
Latest member
mdusw

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