我正在研究“从其他来源->从Microsoft Query”,下面是我在sheet1和sheet2之间进行比较的代码。它无法按预期运行,并在最后一行收到错误消息“无效路径”。
您能帮我吗?
Sub filter()
Dim filename As String
Dim defaultdirpath As String
Dim src As Workbook
Application.ScreenUpdating = False
filename = Application.GetOpenFilename
defaultdirpath = Left(filename, InStrRev(filename, "\") - 1)
Set src = Workbooks.Open(filename, True, True)
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=Excel Files;DBQ=defaultdirpath; DefaultDir=filename;DriverId=1046;MaxBufferSize=2048" _
), Array(";PageTimeout=5;")), Destination:=Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT `Sheet1$`.column1, `Sheet1$`.column2, `Sheet1$`.column3, `Sheet1$`.test, `Sheet2$`.column1, `Sheet2$`.column2, `Sheet2$`.column3, `Sheet2$`.test" & Chr(13) & "" & Chr(10) & "FROM `filename`.`" _
, _
"Sheet1$` `Sheet1$`, `filename`.`Sheet2$` `Sheet2$`" & Chr(13) & "" & Chr(10) & "WHERE `Sheet1$`.column1 = `Sheet2$`.column1" _
)
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_Excel_Files"
.Refresh BackgroundQuery:=False
End With
End Sub