我一直在為這個問題撓頭。
在我的一個子例程的開頭,我有一些代碼將定義從 B 列開始的資料表的維度。
Dim TableHeaders As Variant: TableHeaders = "Table1[#Headers]" 'Header row for the main data table
Dim MainDataTable As String: MainDataTable = "Table1" 'Should be the main table on the BDDS
Dim MainTable As ListObject: Set MainTable = WsDB.ListObjects(MainDataTable) 'Mimics synax to call on the main data table as a variable (to make things cleaner)
Dim WholeMainTable As Range
Dim last_row As Long: last_row = WsDB.Range("A:A").Rows.Count
Dim last_row_col As Integer: last_row_col = WorksheetFunction.Match(WsDB.Range(TableHeaders).End(xlToRight), WsDB.Range(TableHeaders), 0) 1 ' 1 as the table starts in Column B
Dim first_header As String: first_header = WsDB.Range(TableHeaders).End(xlToLeft).Address
Set WholeMainTable = WsDB.Range(first_header, Cells(WsDB.Cells(last_row, last_row_col).End(xlUp), last_row_col))
除了定義“first_header”外,一切都按預期作業。End(xlToRight) 給了我正確的位置,但 End(xlToLeft) 回傳 A53 而不是 B53。
這段代碼作業正常,直到我洗掉了資料表上方的一些行,但這段代碼應該能夠解釋這一點(公平地說,行號應該是......)
我仔細檢查了 A53 中沒有任何內容,我嘗試記錄代碼,選擇表頭并手動執行 ctrl ( shift) left。它按預期在 B53 處停止,但是當代碼在宏中重新運行時... A53 再次顯示。
老實說,我不知道為什么會這樣……如果有人有任何見解,將不勝感激。
謝謝
編輯:通過下面 Gove 建議的以下代碼重寫解決。
Dim MainTable As ListObject: Set MainTable = WsDB.ListObjects(MainDataTable) 'Mimics synax to call on the main data table as a variable (to make things cleaner)
Dim WholeMainTable As Range: Set WholeMainTable = MainTable.Range
uj5u.com熱心網友回復:
似乎您正在嘗試將 WholeMainTable 設定為整個表的范圍。如果我對您的代碼意圖的閱讀是正確的,那么有一種更簡單的方法:
Dim MainTable As ListObject: Set MainTable = WsDB.ListObjects(MainDataTable)
Dim WholeMainTable As Range
Set WholeMainTable = MainTable.Range
uj5u.com熱心網友回復:
End 總是在參考單元格或參考范圍的左上角單元格之后開始搜索。所以如果你告訴它往 B53 的左邊看,它會找到 A53。如果您從 C53 開始,它應該會找到 B53。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/454706.html
下一篇:OSError:[Errno8]Execformaterror:'/home/ec2-user/Desktop/chromedriver'在AWSEC2ARM風味機器中使用Chro
