如果命名范圍“InventoryList”等于傳遞給 Sub 的命名范圍,我正在嘗試在 Excel 中調整命名范圍的大小。我似乎無法為此獲得正確的語法。“IF”陳述句是我收到錯誤的地方。希望有人可以幫助我。
提前致謝。
Sub Resize_Inventory_Range(InventoryList As Excel.Range)
Dim xlApp As Excel.Application
Dim WB As Excel.Workbook
Dim WS As Excel.Worksheet
Dim FullInventory As Excel.Range
xlApp = GetObject(, Constants.ExcelApp)
WB = xlApp.Workbooks("Product")
WS = WB.Sheets("Inventory")
FullInventory = WS.Range("Full_Inventory")
If InventoryList.Name.Name = "Hardware_Inventory" Then
FullInventory.Resize(FullInventory.Rows.Count 1, 4).Name = FullInventory.Name.Name
End If
End Sub
uj5u.com熱心網友回復:
您可以通過比較它們的地址來檢查這兩個范圍是否相同。例如:
MsgBox InventoryList.Address = FullInventory.Address
uj5u.com熱心網友回復:
什么name.name回傳取決于羯羊名字上workbook-或作業表級別的定義。如果作業表級別名稱將回傳例如“Table1!Hardware_Inventory”。
所以你的支票應該是:
If InventoryList.Name.Name like "*Hardware_Inventory" Then
(如果可能有一個名稱“Old_Hardware_Inventory”,你將不得不建立一個更復雜的檢查......)
為了安全起見 - 如果提供的范圍沒有名稱 - 您應該改用此函式。當沒有名稱時,檢查名稱總是會回傳錯誤。
Public Function tryGetName(c As Range) As String
'if range has no name this will catch the error
'an empty string is returned
On Error Resume Next
tryGetName = c.Name.Name
On Error GoTo 0
End Function
這是我認為還on error resume next可以的少數情況之一。
您的支票將如下所示:
If tryGetName(InventoryList) like "*Hardware_Inventory" Then
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/331397.html
下一篇:函式VB.NET到C#的轉換
