[英]QueryTables.Add Method (Excel)
strFile = Application.GetOpenFilename 'Workbooks.Open strFile strFile = Application.GetOpenFilename'Workbooks.Open strFile
With ActiveSheet.QueryTables.Add( _
Connection:="strFile", _
Destination:=Range("$A$1"))
'I have tried with : With ActiveSheet.QueryTables.Add( _ Connection:="TEXT;strFile", _ Destination:=Range("$A$1")) '我尝试过:使用ActiveSheet.QueryTables.Add(_ Connection:=“ TEXT; strFile”,_ Destination:= Range(“ $ A $ 1”))
This is throwing up an error saying "Run-time error '1004'; So you have any clue why it is showing me error ?? 这抛出了一个错误,提示“运行时错误'1004';因此,您有任何线索为什么会向我显示错误?
After some quick testing, I think I have found the issue. 经过快速测试,我认为我已经找到了问题。 You need to be sure you are not passing in the string "strFile" into the connection, and you need to concatenate the TEXT to the connection string.
您需要确保没有将字符串“ strFile”传递到连接中,并且需要将TEXT连接到连接字符串。 This is demonstrated here:
这在这里演示:
strFile = Application.GetOpenFilename 'Workbooks.Open strFile
strFile = "TEXT;" & strFile 'Add the TEXT; to beginning of connection string
With ActiveSheet.QueryTables.Add(Connection:= _
strFile, Destination _
:=Range("$A$1"))
... 'Remainder of code
End With
For good measure, you may want to put the With ActiveSheet.QueryTables.Add ...
code inside an IF statement that checks to make sure a file was selected. 为了达到良好的效果,您可能需要将
With ActiveSheet.QueryTables.Add ...
代码放在IF语句中,该语句检查以确保已选择文件。
For example: 例如:
Dim strFile As String
strFile = Application.GetOpenFilename 'Workbooks.Open strFile
strFile = "TEXT;" & strFile
If strFile <> False Then
With ActiveSheet.QueryTables.Add(Connection:= _
strFile, Destination _
:=Range("$A$1"))
... 'Remainder of code
End With
Else
MsgBox "No file was selected!"
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.