How to give general path while comparing data between two sheets in excel?

时间:2016-04-04 17:13:50

标签: excel vba

When I compare data using Microsoft Query in Microsoft Excel to compare sheets it takes c:\user\mgow\desktop\Excel.xlsm path to compare. But when I use this Excel.xlsm file on others system I see the error (odbc excel driver login failed). Can anyone help to overcome this issue.

Note: I don't want to use specific path C:\Users\mgow\Desktop\Excel.xlsm Can I use common path which everyone can use ?

Sub Macro()

Application.CutCopyMode = False
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
    "ODBC;DSN=Excel Files;DBQ=C:\Users\mgow\Desktop\Excel.xlsm;DefaultDir=C:\Users\mgow\Desktop\" _
    ), Array("RAM;DriverId=1046;MaxBufferSize=2048;PageTimeout=5;")), Destination _
    :=Range("$B$1")).QueryTable
    .CommandType = 0
    .CommandText = Array( _
    "SELECT `Result$`.REQUIREMENTS, `Result$`.`AUDIT DATA`, `Result$`.`AUDIT DATA1`, `Result$`.`AUDIT DATA2`, `Result$`.`AUDIT DATA3`, `Result$`.`AUDIT DATA4`, `Result$`.`AUDIT DATA5`, `Result$`.`AUDIT DAT" _
    , _
    "A6`, `Result$`.`AUDIT DATA7`, `Result$`.`AUDIT DATA8`, `Result$`.`AUDIT DATA9`, `Result$`.`AUDIT DATA10`, `Result$`.`AUDIT DATA11`, `Result$`.`AUDIT DATA12`, `Result$`.`AUDIT DATA13`, `Result$`.`AUDIT" _
    , _
    " DATA14`, `Result$`.`AUDIT DATA15`, `Result$`.`AUDIT DATA16`, `Result$`.`AUDIT DATA17`, `Result$`.`AUDIT DATA18`, `Result$`.`AUDIT DATA19`, `Result$`.`AUDIT DATA20`, `Result$`.`AUDIT DATA21`" & Chr(13) & "" & Chr(10) & "FROM `Co" _
    , _
    "mpare$` `Compare$`, `Result$` `Result$`" & Chr(13) & "" & Chr(10) & "WHERE `Result$`.REQUIREMENTS = `Compare$`.REQUIREMENTS" _
    )
    .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

1 个答案:

答案 0 :(得分:0)

如果要引用存储工作簿的位置/路径,可以使用

ThisWorkbook.Path