我有一系列范围如下:
Dim all_pivots_amounts As New Collection
all_pivots_amounts.Add (corporate_amounts)
all_pivots_amounts.Add (wealth_amounts)
all_pivots_amounts.Add (institutional_amounts)
all_pivots_amounts.Add (premium_amounts)
all_pivots_amounts.Add (one_bank_one_bank_amounts)
all_pivots_amounts.Add (one_bank_entrepreneurs_amounts)
Debug.Print corporate_amounts.Address
Debug.Print all_pivots_amounts(1).Address ' this line gives object required
error
我不知道为什么第二个Debug.print
行给出了“对象必需的错误”(我也尝试过.Items
方法)。我需要使用类似的东西,因为我想在循环中打印地址而不是专门调用每个范围。
完整代码:
Sub BA_view_new()
Application.EnableEvents = False
Call SetWorkbooks
Call update_pivot_data_sources
Dim corporate_table As PivotTable
Dim wealth_table As PivotTable
Dim institutional_table As PivotTable
Dim premium_table As PivotTable
Dim one_bank_one_bank_table As PivotTable
Dim one_bank_entrepreneurs As PivotTable
Dim corporate_amounts As Range
Dim wealth_amounts As Range
Dim institutional_amounts As Range
Dim premium_amounts As Range
Dim one_bank_one_bank_amounts As Range
Dim one_bank_entrepreneurs_amounts As Range
'get pivots
Set corporate_table = BA_view_pivots_sheet.PivotTables("Corporate & Investment Banking")
Set wealth_table = BA_view_pivots_sheet.PivotTables("Wealth Management & Private Clients")
Set institutional_table = BA_view_pivots_sheet.PivotTables("Institutional Clients")
Set premium_table = BA_view_pivots_sheet.PivotTables("Premium Clients CH")
Set one_bank_one_bank_table = BA_view_pivots_sheet.PivotTables("One Bank Switzerland->One Bank Switzerland")
Set one_bank_entrepreneurs = BA_view_pivots_sheet.PivotTables("One Bank Switzerland->Bank For Entrepreneurs")
'get pivots' amounts
Set corporate_amounts = corporate_table.DataBodyRange
Set wealth_amounts = wealth_table.DataBodyRange
Set institutional_amounts = institutional_table.DataBodyRange
Set premium_amounts = premium_table.DataBodyRange
Set one_bank_one_bank_amounts = one_bank_one_bank_table.DataBodyRange
Set one_bank_entrepreneurs_amounts = one_bank_entrepreneurs.DataBodyRange
'create collection
Dim all_pivots_amounts As New Collection
all_pivots_amounts.Add (corporate_amounts)
all_pivots_amounts.Add (wealth_amounts)
all_pivots_amounts.Add (institutional_amounts)
all_pivots_amounts.Add (premium_amounts)
all_pivots_amounts.Add (one_bank_one_bank_amounts)
all_pivots_amounts.Add (one_bank_entrepreneurs_amounts)
Debug.Print corporate_amounts.Address
Debug.Print all_pivots_amounts(1).Address ' this line gives object required
error
答案 0 :(得分:2)
您需要限定添加为范围或对象的范围。否则,VBA会考虑它们的变体。并且Variants
没有.Address
属性。因此,需要一个对象:
Sub TestMe()
Dim all_pivots_amounts As New Collection
Dim someRange As Range
Dim someOtherRange As Range
Set someRange = Range("A1:A10")
Set someOtherRange = Range("A66")
all_pivots_amounts.Add someRange
all_pivots_amounts.Add someOtherRange
Debug.Print all_pivots_amounts(1).Address
Debug.Print all_pivots_amounts.Item(1).Address
Debug.Print all_pivots_amounts(2).Address
Debug.Print all_pivots_amounts.Item(2).Address
End Sub
执行此操作后,您可以同时使用.Item(index)
和(1)
。
范围变量不带括号。如果使用括号,则强制VBA获取参数ByVal
。要获得Range()
ByVal意味着,VBA正在将其值添加到集合中,因此它不再是对象,而是Variant
。如果您传递这样的范围,这是您在监视窗口中看到的一个简单示例:
Sub TestMe()
Dim newColl As New Collection
Dim someRange As Range: Set someRange = Range("A1")
Dim someOtherRange As Range: Set someOtherRange = Range("A66")
Range("A1") = 55
newColl.Add (someRange)
newColl.Add someOtherRange
End Sub
Item1
为Double
,Item2
为Object/Range
:
以下是关于此的两个参考:
答案 1 :(得分:1)
您错误地使用了命名范围。与下面的代码比较,看看rror发生的位置:
ORDER BY cASE WHEN num_mojud = 0 THEN num_mojud END ASC,CASE WHEN num_mojud > 0 THEN num_mojud END ASC
or
ORDER BY IF (num_mojud = 0, 9999999, num_mojud )