Excel VBA对包含值的单元格和包含符号的单元格中的记录编号进行计数



我不知道如何启动这段代码,而且我不太擅长VBA。基本上我有 2 列我感兴趣。一列(E 列(包含带有单词"标题"的单元格。我想从上到下计算这些,在左侧的单元格 1 行中,我需要将当前计数放在该单元格文本的开头。

ROW | column D  | Column E 
1   |   AHU     | Header
2   |  random   | random
3   |   FCU     | Header 

如果你不需要VBA,你可以使用=COUNTIF($E$2:E2,"Header")

如果要遍历行,可以使用如下所示的计数器:

Dim ct as Long
Dim i as Long
Dim iLastRow as Long
iLastRow = Range("E2", Range("E" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible).Count
For i = 2 to iLastRow
    If Range("E" & i).value="Header" then
         ct = ct + 1
         Range("D" & i).value = ct & "-" & Range("D" & i).value
    End If
Next i

最新更新