Excel VBA (need help)

来源: 2008-03-31 08:07:46 [旧帖] [给我悄悄话] 本文已被阅读:

Hi everyone,

I am trying to write a small VBA proccedure to import dozens of text files into a worksheet.

The problem is I can't ask the QueryTables.Add function to read a variable for the file name, like the following code, variable Filename is not recognized inside quatation marks or without quatation marks:

Sub ImportData()

Dim FileName as string
Filename = E:\test.txt

With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;Filename" _
, Destination:=Range("$A$1"))
.TextFileTabDelimiter = True
.Refresh BackgroundQuery:=False
End With
End Sub


Thanks, please help!