代码在 F8 单步执行期间有效,但在运行时不起作用



我编写了一个宏,当使用"F8"(中断模式(运行时可以完美运行,但是当正常运行时,该过程似乎跳过了以下步骤。相关问题的答案都不允许我解决这个问题。 谁能告诉我是什么让程序不提供正确的结果?

Option Explicit
Dim wbTO As Workbook
Dim wsEF As Worksheet, wsTO As Worksheet
Dim r As Range
Dim x As Integer, y As Integer, rn As Integer
Dim LastColumn As Long, LastRow As Long, o As Long, oo As Long, v As Long
Sub GenerateEFiche()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual   
Set wbTO = ActiveWorkbook
Set wsEF = Worksheets("main")
Set wsTO = wbTO.Sheets(1)
x = Application.Match("Header1", wsTO.Rows(1), False)
y = Application.Match("Category", wsTO.Rows(1), False)
o = 0
oo = 0
v = 0
Set r = wsTO.Range(wsTO.Cells(2, x), wsTO.Cells(LastRow, x))
For Each Cell In r
rn = Cell.Row
If Cells(rn, y).Value = "ALPHA" And Not Cell = "" Then
o = o + 1
ElseIf Cells(rn, y).Value = "BRAVO" And Not Cell = "" Then
oo = oo + 1
ElseIf Cells(rn, y).Value = "CHARLIE" And Not Cell = "" Then
v = v + 1
End If
Next Cell
wsEF.Range("B25").Value = o
wsEF.Range("C25").Value = oo
wsEF.Range("D25").Value = v
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.AskToUpdateLinks = True
MsgBox "macro terminated"
End Sub

好的,我发现了问题:

我已经重写了代码的"对于每个"部分

For Each Cell In r
rn = Cell.Row
If wsTO.Cells(rn, x).Value <> "" Then
If wsTO.Cells(rn, y).Value = "ALFA" Then
o = o + 1
ElseIf wsTO.Cells(rn, y).Value = "BRAVO" Then
oo = oo + 1
ElseIf wsTO.Cells(rn, y).Value = "CHARLIE" Then
v = v + 1
End If
End If
Next Cell

现在返回所需的结果。需要添加对单元格的正确引用才能使公式正常工作。

相关内容

最新更新