我一直在尝试一切,我完全不知道为什么这不起作用。我看过无数先前的问题和答案,没有任何作用。我有一个对话框,用户可以选择一些文件然后导入Excel。设置导入范围时会出现问题。
Dim files As New OpenFileDialog
files.Multiselect = True
files.InitialDirectory = My.Settings.path
If (files.ShowDialog() = DialogResult.OK) Then
My.Settings.files = files.InitialDirectory
My.Settings.Save()
MsgBox("Files to be imported: " & files.Multiselect, MessageBoxIcon.Information)
If String.IsNullOrEmpty(My.Settings.path) Then
MsgBox("Warning! No files were imported!", MessageBoxIcon.Exclamation)
End If
End If
Dim openExcel As New Microsoft.Office.Interop.Excel.Application
Dim openWorkbook As New Microsoft.Office.Interop.Excel.Workbook
Dim openWorksheet As New Microsoft.Office.Interop.Excel.Worksheet
openExcel = CreateObject("Excel.Application")
openExcel.Visible = True
openExcel.UserControl = True
openWorkbook = openExcel.Workbooks.Add
openWorksheet = openWorkbook.ActiveSheet
With openWorksheet.QueryTables.Add(Connection:=My.Settings.files, Destination:=Range("$A$1"))
End With
它一直给我错误:“BC30111'Range'是一种接口类型,不能用作表达式”
答案 0 :(得分:1)
您的Range
返回的是与QueryTable不同的工作表。
试试这样:
With openWorksheet.QueryTables.Add(Connection:=My.Settings.files, Destination:=openWorksheet.Range("$A$1"))
或者这个:
With openWorksheet
With .QueryTables.Add(Connection:=My.Settings.files, Destination:=.Range("$A$1"))
答案 1 :(得分:0)
请参阅https://msdn.microsoft.com/en-us/library/office/ff837764.aspx
尝试从Range("$A$1")
答案 2 :(得分:0)
我认为问题在于它并不知道" Range"来自,试试这个:
With openWorksheet
QueryTables.Add(Connection:=My.Settings.files, Destination:=Range("$A$1"))
End