I'm trying to import a lot of text files with numerical names into a separate worksheets.
The loop to create the worksheets works fine
Dim i as integer 'initial file name
Dim k as integer 'final file name
i = Cells(3, 3).Value
k = Cells(5, 3).Value
Do while i <= k
Worksheets.Add.Name = i
i = i +5
Loop
and for importing specific individual files, this line also seems to work fine (when including the .FileNames .RowNumbers. RefreshPeriod etc. commands):
With Activesheet.QueryTables.Add(Connection:="TEXT;C:\temp\load_excel\15.txt" _, Destination:=Range ("$A$1"))
I would like to replace the "TEXT;C:\temp\load_excel\15.txt" with something more that allows me to use two different variables to change the files being imported:
Dim Folder As String
Dim File As String
Dim DQ as String
DQ = """" 'double quotation marks
Folder = Cells(14, 2).Value 'cell which states C:\temp\load_excel\
File = DQ & "TEXT;" & Folder & i & ".txt" & DQ
'for i = 15 this gives "TEXT;C:\temp\load_excel\15.txt"
Is there a way to incorporate the two so I can have a loop like this?
Do while i <=k
Worksheets.Add.Name = i
Activesheet.QueryTables.Add(Connection:= File _, Destination:=Range ("$A$1"))
i = i +5
Loop
As far as I can see, this should work, but when I try and run it I get a Run-time error '1004': Application or object-defined error. If anyone could help, it would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire