Excel VBA计数单元格,其中包含包含符号的单元格中的值和记录编号

时间:2016-02-15 21:05:43

标签: excel-vba vba excel

我不知道如何启动此代码,我对VBA不是很擅长。基本上我有2列我感兴趣。一列(E列)包含单词Header的单元格。我想从上到下计算那些,在左边的单元格中,我需要将当前计数放在该单元格中文本的开头。

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

1 个答案:

答案 0 :(得分:1)

如果您不需要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