比對兩個分頁資料,回饋欄位值,來源資料(可能)有兩筆以上狀況
- 帖子
- 1018
- 主題
- 15
- 精華
- 0
- 積分
- 1058
- 點名
- 0
- 作業系統
- win7 32bit
- 軟體版本
- Office 2016 64-bit
- 閱讀權限
- 50
- 性別
- 男
- 來自
- 桃園
- 註冊時間
- 2012-5-9
- 最後登錄
- 2022-9-28
|
本帖最後由 stillfish00 於 2013-11-15 15:30 編輯
回復 1# HSIEN6001
修改一下錄製Microsoft Query的巨集,Left Join結合兩表。
Sub Test()
On Error Resume Next
With Sheets("回饋數值")
.Cells.ListObject.QueryTable.Delete
.Cells.ClearContents
With .ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=Excel Files;DBQ=" & ThisWorkbook.FullName & ";DefaultDir=" & ThisWorkbook.Path & ";DriverId=1046;MaxB" _
), Array("ufferSize=2048;PageTimeout=5;")), Destination:=.Range("$A$1")). _
QueryTable
.CommandText = Array( _
"SELECT `來源1$`.代號, `來源1$`.名稱, `來源1$`.日期, `來源1$`.漲跌, `來源1$`.收盤, `來源1$`.`成交(張)`, `來源1$`.當沖, `來源2$`.大股東名稱, `來源2$`.`異動(張)`, `來源2$`.上月持張, `來源2$`.本月持張" & Chr(13) & "" & Chr(10) & "FR" _
, _
"OM {oj `" & ThisWorkbook.FullName & "`.`來源1$` `來源1$` LEFT OUTER JOIN `" & ThisWorkbook.FullName & "`.`來源2$` `來源2$` ON `來源1$`.代號 = `來源2$`." _
, "代號}")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "表格_來自_Excel_Files_的查詢"
.Refresh BackgroundQuery:=False
End With
End With
End Sub |
|
|
|
|
|
|
|
- 帖子
- 1018
- 主題
- 15
- 精華
- 0
- 積分
- 1058
- 點名
- 0
- 作業系統
- win7 32bit
- 軟體版本
- Office 2016 64-bit
- 閱讀權限
- 50
- 性別
- 男
- 來自
- 桃園
- 註冊時間
- 2012-5-9
- 最後登錄
- 2022-9-28
|
回復 2# stillfish00
小筆誤
Sub Test()
With Sheets("回饋數值")
On Error Resume Next
.Cells.ListObject.QueryTable.Delete
.Cells.ClearContents
On Error goto 0
With .ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=Excel Files;DBQ=" & ThisWorkbook.FullName & ";DefaultDir=" & ThisWorkbook.Path & ";DriverId=1046;MaxB" _
), Array("ufferSize=2048;PageTimeout=5;")), Destination:=.Range("$A$1")). _
QueryTable
.CommandText = Array( _
"SELECT `來源1$`.代號, `來源1$`.名稱, `來源1$`.日期, `來源1$`.漲跌, `來源1$`.收盤, `來源1$`.`成交(張)`, `來源1$`.當沖, `來源2$`.大股東名稱, `來源2$`.`異動(張)`, `來源2$`.上月持張, `來源2$`.本月持張" & Chr(13) & "" & Chr(10) & "FR" _
, _
"OM {oj `" & ThisWorkbook.FullName & "`.`來源1$` `來源1$` LEFT OUTER JOIN `" & ThisWorkbook.FullName & "`.`來源2$` `來源2$` ON `來源1$`.代號 = `來源2$`." _
, "代號}")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "表格_來自_Excel_Files_的查詢"
.Refresh BackgroundQuery:=False
End With
End With
End Sub |
|
|
|
|
|
|
|
- 帖子
- 1018
- 主題
- 15
- 精華
- 0
- 積分
- 1058
- 點名
- 0
- 作業系統
- win7 32bit
- 軟體版本
- Office 2016 64-bit
- 閱讀權限
- 50
- 性別
- 男
- 來自
- 桃園
- 註冊時間
- 2012-5-9
- 最後登錄
- 2022-9-28
|
回復 4# HSIEN6001
DriverId=1046 改成 DriverId=790 看看
應該是EXCEL版本不同問題...
語法因為是錄的,我也沒有整理過,所以很難看懂, |
|
|
|
|
|
|
|