如何获得每个循环中的单元格的行号

时间:2018-06-28 12:15:15

标签: excel vba excel-vba

每个循环都有一个这样的

Dim rCell as range
Dim Searchrange as range
Set Searchrange = wss.Range("B12:B36") 'where wss is a defined worksheet
For Each rCell in SearchRange
    'do some stuff
    'get the row number of the worksheetrow rCell is currently refering to
Next rCell

如何从wss.当前持有值的原始工作表(rCell)中获取行号?

1 个答案:

答案 0 :(得分:2)

我可以想象有2种不同的“行号”:

  1. 工作表中的绝对行号
  2. 相对于Searchrange的行数

示例:

Dim SearchRange As Range
Set SearchRange = wss.Range("B12:B36") 'where wss is a defined worksheet

Dim rCell As Range
For Each rCell in SearchRange
    'Absolute row number in the worksheet
    Debug.Print rCell.Row                          'returns 12, 13, 14, … 36

    'Row number relative to the SearchRange
    Debug.Print rCell.Row - Searchrange.Row + 1    'returns 1, 2, 3, … 25
Next rCell