在Excel 2007 VBA中,行数很麻烦

我这个macros已经很完美了,但是最近它错过了一次3次,但是除了那3次之外还能正常工作。 我用Countif来计算那个excel内容,还是错过了一个数字。 我看不出有什么不同的价值,为什么它错过了那一个。 这是代码。

Dim DP1_total As Integer Range("H1").Sort Key1:=Range("H2"), Order1:=xlDescending, Header:=xlYes FinalRowH = Range("H1048576").End(xlUp).Row DP1_total = 0 For i = 2 To FinalRowH If Range("H" & i).Value = "DP1" Then DP1_total = DP1_total + 1 End If Next i If DP1_total > 0 Then MsgBox (DP1_total) End If 

我没有收到下面的代码错误。 我摆脱了你的FinalRowHvariables,并简单地设置循环结束等于.end(xlup)。 另一个改进是创build一个变体来遍历每个单元格并find值。

 Dim DP1_total As Integer Dim FinalRowH As Range Dim i As Integer Range("H1").Sort Key1:=Range("H2"), Order1:=xlDescending, Header:=xlYes DP1_total = 0 For i = 2 To Range("$H$1048567").End(xlUp).Row If ThisWorkbook.Sheets(1).Range("H" & i).Value = "DP1" Then DP1_total = DP1_total + 1 End If Next i If DP1_total > 0 Then MsgBox (DP1_total) End If