Sub Macro1()
Dim fs, f As Object
Dim strPath As String
strPath = "Filename_" + Format(Date, "yyyymmdd")
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile(strPath, 1, 0)
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;strPath", Destination:= _
Range("A1"))
End Sub
'-----我不确定我在VBA上面缺少什么,但显然,当我在宏设置中手动指向文件名时它会起作用--------- --------------------
Sub Macro1()
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Desktop\filename_20140101.txt", Destination:= _
Range("A1"))
End Sub
'感谢!!!!
答案 0 :(得分:0)
看起来您实际上没有将strPath
传递给连接,而且您缺少文件扩展名(.txt),您需要它:
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & strPath, Destination:= _
Range("A1"))
将strPath
设置为完整路径,包括扩展名:
strPath = "C:\Desktop\Filename_" + Format(Date, "yyyymmdd") & ".txt"