Hi,
I need help with something that I should probably know but can't seem to remember how to solve. I've got a piece of code that uses a For/Next loop to calculate the value of an object 'Total' based on values in various arrays:
Dim r as integer, ArrA as variant, ArrB as variant, Total as long, WB as workbook
Set WB = ThisWorkbook
ArrA = WB.Worksheets("Table").Range("K2:K62").Value
ArrB = WB.Worksheets("Table").Range("L2:L62").Value
For R = 1 To 61 'make the UBound of R = the # of rows used to calculate ArrA & ArrB (i.e. 61)
If UserForm3.CheckBox2.Value = True Then '
Total = Total * ArrA(R, 1)
End If
If UserForm3.CheckBox5.Value = True Then
Total = Total * ArrB(R, 1)
End If
Next R
And the code works great except that I'd like to be able to change the upper bound of R and the ArrA & ArrB array sizes based on the value in a worksheet cell (rather than having to change the coding each time that cell value changes). I know I can assign an object name to the cell's value (="Max") & can then say "For R = 1 to Max", but how to I use "Max+1" to describe the upper bounds of my two arrays so that R will loop through all the array entries & doesn't stop short or over-shoot the size of each array?? And how can I do that without having to use additional loops to define my two arrays?
I've been playing arround with solutions like:
Max = WB.Worksheets("Table").Range("N8").Value +1
ArrA = WB.Worksheets("Table").Range("K2:K" & Max).Value
But nothing has worked so far. And, since it's been a nearly a year since I wrote code, I can't remember if I can actually use UBound to define an array (or if I can only use it to loop through entries in the array) or if there's a better solution that I just haven't though of yet.
Can anyone help jog my memory and suggest a quick & easy solution?
I'd be very grateful for any help you can give!
Thx.
Bookmarks