引用 table 的 .DataBodyRange 的特定范围
Referencing specific Range of table's .DataBodyRange
假设我有以下示例table
我有一个贯穿整个 ListColumn(2)
(C / 3 列)的 for 循环,并根据谁拥有最多的苹果(例如,Michael 是 1,Robert 2 等)
但是,我只想引用 table
的特定范围
(例如,假设 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
被指定为取整个 Column/Row 或仅取整个 Column/Row 中的 1 个单元格ListObject 为 .DataBodyRange([row index], [column index])
.
因此,在选择 Robert、Michael、Laurel ListRows(3, 4, 5)
的假定示例中,预期结果将是:
有什么建议吗?
您可以使用如下方式引用列中的连续单元格:
With tbl.ListColumns(2).DataBodyRange
For Each my_range in Range(.Cells(3), .Cells(5))
' Code here.
Next my_range
End With
你是这个意思吗?
我知道我迟到了,但为了后代...
使用 .Offset
和 .Resize
方法几乎可以在 tbl.Range
或 tbl.DataBodyRange
...
中执行任何操作
仅迭代 select 行:
...
Set my_range1 = tbl.Listcolumns(2).DataBodyRange
For LoopControl = StartRow To EndRow
my_range1.Resize(1).Offset(LoopControl - 1).Value2 = 'your code here
'Alternate code
tbl.ListRows(LoopControl).Resize(, 1).Offset(, 2).Value2 = 'your code here
'Or even:
tbl.DataBodyRange.Resize(1, 1).Offset(2, LoopControl).Value2 = 'your code here
Next LoopControl
请注意,您可以使用 .ListColumns("Apples")
以防列在事后重新排列。此外,您可以 .Offset(, tbl.ListColumns("Apples").Index)
适应不断变化的 table 结构。
或将行的子集称为单个范围:
Set my_range = tbl.ListColumns(2).DataBodyRange.Offset(StartRow - 1).Resize(EndRow - StartRow + 1)
'Alternately:
Set my_Range = tbl.ListColumns(2).Range.Offset(StartRow).Resize(EndRow - StartRow + 1)
假设我有以下示例table
我有一个贯穿整个 ListColumn(2)
(C / 3 列)的 for 循环,并根据谁拥有最多的苹果(例如,Michael 是 1,Robert 2 等)
但是,我只想引用 table
的特定范围
(例如,假设 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
被指定为取整个 Column/Row 或仅取整个 Column/Row 中的 1 个单元格ListObject 为 .DataBodyRange([row index], [column index])
.
因此,在选择 Robert、Michael、Laurel ListRows(3, 4, 5)
的假定示例中,预期结果将是:
有什么建议吗?
您可以使用如下方式引用列中的连续单元格:
With tbl.ListColumns(2).DataBodyRange
For Each my_range in Range(.Cells(3), .Cells(5))
' Code here.
Next my_range
End With
你是这个意思吗?
我知道我迟到了,但为了后代...
使用 .Offset
和 .Resize
方法几乎可以在 tbl.Range
或 tbl.DataBodyRange
...
仅迭代 select 行:
...
Set my_range1 = tbl.Listcolumns(2).DataBodyRange
For LoopControl = StartRow To EndRow
my_range1.Resize(1).Offset(LoopControl - 1).Value2 = 'your code here
'Alternate code
tbl.ListRows(LoopControl).Resize(, 1).Offset(, 2).Value2 = 'your code here
'Or even:
tbl.DataBodyRange.Resize(1, 1).Offset(2, LoopControl).Value2 = 'your code here
Next LoopControl
请注意,您可以使用 .ListColumns("Apples")
以防列在事后重新排列。此外,您可以 .Offset(, tbl.ListColumns("Apples").Index)
适应不断变化的 table 结构。
或将行的子集称为单个范围:
Set my_range = tbl.ListColumns(2).DataBodyRange.Offset(StartRow - 1).Resize(EndRow - StartRow + 1)
'Alternately:
Set my_Range = tbl.ListColumns(2).Range.Offset(StartRow).Resize(EndRow - StartRow + 1)