简体   繁体   English

带数组下标的VLOOKUP超出范围

[英]VLOOKUP with Array Subscript out of range

I'm trying to do what I think should be a simple formula but it's proving difficult. 我正在尝试做我认为应该是一个简单公式的操作,但是事实证明这很困难。 I'm attempting to set a cell equal to a formula via VBA. 我试图通过VBA将单元格设置为等于公式。 Here is the code: 这是代码:

Dim pivotws_month As Worksheet
Dim departmentArray() As Variant
Dim i As Long
Dim x As Long
Dim lrow As Long

lrow = pivotws_month.Cells(Rows.Count, 2).End(xlUp).Row
ReDim departmentArray(1 to lrow)
departmentArray = pivotws_month.Range("B4:B" & lrow)

i = 4  
For x = 1 to UBound(departmentArray)
   pivotws_month.Cells(i,4).Value = pivotws_month.Cells(i,3) / Application.WorksheetFunction.VLookup(departmentArray(x), pivotws_month.Range("G4:H" & lrow), 2, False)
i = i + 1
Next x

I've debugged my variables and my lrow is correct (297) and the # of items in my array is correct (294). 我已经调试了变量,并且贷款正确(297),数组中的项目数正确(294)。 I keep getting a subscript out of range error inside of my For loop and I'm not sure why. 我不断在For循环中收到下标超出范围的错误,我不确定为什么。 Please help, I've been looking around for answer for awhile now. 请帮助,我一直在寻找答案。

You have created a 2D array; 您已经创建了2D数组; supply the rank in all references. 提供所有参考文献中的排名。

...

'this Redim is completely unnecessary
'ReDim departmentArray(1 to lrow)

...

with pivotws_month
    i = 4  
    lrow = .Cells(Rows.Count, 2).End(xlUp).Row
    'create a 2-D array of many 'rows' and one 'column
    departmentArray = .Range("B4:B" & lrow).value
    For x = LBound(departmentArray, 1) to UBound(departmentArray, 1)
       .Cells(i,4).Value = .Cells(i,3) / Application.VLookup(departmentArray(x, 1), .Range("G4:H" & lrow), 2, False)
        i = i + 1
    Next x
end with

Loading a variant array from a worksheets range always results in a 2-D array. 从工作表范围加载变体数组始终会导致二维数组。 It doesn't matter whether you are loading from a single row or a single columns. 从单行还是单列加载都没有关系。 It also discards any Option Base 0 declaration or a ReDim command to a 1-D array prior to the bulk loading code line. 它还会在批量加载代码行之前将任何Option Base 0声明或ReDim命令丢弃到一维数组。

You can test the dimensions of your array after the bulk loading event with the following. 您可以在批量加载事件之后使用以下方法测试阵列的尺寸。

debug.print lbound(departmentArray, 1) & ":" & ubound(departmentArray, 1)
debug.print lbound(departmentArray, 2) & ":" & ubound(departmentArray, 2)

It may help to think of the first rank (eg ubound(departmentArray, 1) ) as the 'rows' of the array and the second rank (eg ubound(departmentArray, 2) ) as the 'columns' of the array. 将第一个等级(例如ubound(departmentArray, 1) )视为数组的“行”,将第二个等级(例如ubound(departmentArray, 2) )视为数组的“列”可能会有所帮助。

There is no error control on that VLOOKUP. 该VLOOKUP没有错误控制。 A simple no-match #N/A will throw your code into conniptions. 一个简单的不匹配#N / A将使您的代码陷入混乱。 The result of an Application.Vlookup can be thrown into a variant and that variant can be tested with IsError. 可以将Application.Vlookup的结果放入一个变量中,并且可以使用IsError测试该变量。 A further test against the variant equaling zero can be used to avoid #DIV/0! 可以使用针对零变量的进一步测试来避免#DIV / 0! results on the division operation in your code. 代码中除法运算的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM