“范围”是一种接口types,不能用作expression式

我一直在尝试一切,我完全不知道为什么这不起作用。 我看过无数以前的问题和答案,没有任何作品。 我有一个对话框,用户select一些文件,然后可以导入到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”范围“是一种接口types,不能用作expression式”

你的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")) 

请参阅https://msdn.microsoft.com/en-us/library/office/ff837764.aspx

尝试从$ Range("$A$1")删除$

我认为问题是它不知道“范围”来自哪里,试试这个:

 With openWorksheet QueryTables.Add(Connection:=My.Settings.files, Destination:=Range("$A$1")) End