Do you want to tell me what you are trying to do ?
Sometimes I wonder if even I know.
I have a table in a worksheet. I need to calculate the Z Scores on several of the columns, then apply a weighting factor and sum the rows.
1) Set rngTableData = ActiveSheet.ListObjects(rnTable).Range
a) Ubound applies to arrays this is not an array
I think I see. It's a range, not an array. Ranges, like arrays, have dimensions, and so, can be subscripted. Right?
And I got this to work:
Code:
Dim rngPrice As range
Set rngPrice = ActiveSheet.ListObjects(rnTable).ListColumns(3).DataBodyRange
?application.worksheetfunction.sum(rngprice)
839.98
?application.worksheetfunction.average(rngprice)
209.995
?application.worksheetfunction.stdev_s(rngprice)
31.3549576090714
So, if rngTableData & rngPrice are ranges (not arrays), does that mean that every time I access an element (cell), I am going back to the sheet? That is, the data is
not in any VBA object, so VBA has to go back to the sheet to get the actual data. Is that correct?
I think I read once that if I am going to access very many cells, it's better to load the entire range into a VBA array. This is what I tried:
Code:
Dim Price As Variant
Price = rngPrice.Value
'It's a 4x1 array
?ubound(price,1)
4
?ubound(price,2)
1
'It has the correct values
?price(1,1)
199.99
?price(2,1)
249.99
'But how do I get this to work?
?application.worksheetfunction.sum(price)
0
b) .Range uses the whole table use DataBodyRange if you want to exclude the headings
c) ListObject is the Table you still need to tell it what part of the Table you want to access.
I'm not sure how this is useful. It seems like I would still have to load it into an array to be able to use it.
See if this helps:
VBA Code:
Sub WtdRtg()
Const rnTable As String = "Tbl" 'The name of the main table
Dim TblData
TblData = Range(rnTable).Value
Dim rngTable As Range
Set rngTable = ActiveSheet.ListObjects(rnTable).Range
Debug.Print rngTable.Address, "rngTable (Range - Whole Table)"
Dim rngTableData As Range
Set rngTableData = ActiveSheet.ListObjects(rnTable).DataBodyRange
Debug.Print rngTableData.Address, "rngTableData (DataBody)"
Dim loTableData As ListObject
Set loTableData = ActiveSheet.ListObjects(rnTable)
Debug.Print loTableData.Range.Address, "loTableData.Range"
Debug.Print loTableData.DataBodyRange.Address, "loTableData.DataBodyRange"
Debug.Print loTableData.HeaderRowRange.Address, "loTableData.HeaderRowRange"
End Sub
[/QUOTE]
Yes, that works perfectly. Thank you.