假设我有以下示例表
我有一个贯穿整个ListColumn(2)
(C / 3 列)的 for 循环,并根据谁拥有最多的苹果(例如 Michael 是 1,Robert 2 等)对“用户”进行排名
但是,假设我只想引用表格的特定范围
(例如,假设Range("C7:C9") <=> ListRows(3,4,5)
)
我该怎么做?
我有以下代码:
Private Sub CommandButton1_Click()
Dim tbl As ListObject: Set tbl = Sheets("Sheet1").ListObjects("Table1")
Dim my_range As Range
For Each my_range In tbl.ListColumns(2).DataBodyRange
' ^ this runs through entire column instead of the specified range I want!
my_range.Offset(0, 1) = WorksheetFunction.Rank(my_range, tbl.ListColumns(2).DataBodyRange)
' ^ again, isntead of entire DataBodyRange we should be rather referencing the specific Range
Next my_range
End Sub
基本上,我需要以某种方式将.DataBodyRange
自身限制在特定范围内,问题是,.DataBodyRange
指定为将整个列/行或整个 ListObject 中的仅 1 个单元格作为.DataBodyRange([row index], [column index])
.
因此,在选择 Robert,Michael,Laurel 的假设示例中,ListRows(3, 4, 5)
预期结果将是:
任何建议如何做到这一点?