首页 > 解决方案 > 为什么我的代码在从开发人员窗格运行时可以正确执行,但在使用按钮运行时却不能正确执行?

问题描述

我不明白为什么当我从开发人员窗格运行我的代码时,我的代码运行顺利,但是当我使用按钮运行它时,它不能正确运行。这是我的代码以及何时有效和何时无效的示例

Sub Comment()
Dim x As Integer

x = 3

Do Until Sheets(2).Cells(x, 19) = ""
If Sheets(2).Cells(x, 19) <> "N/A" Then

If Sheets(2).Cells(x, 7).Value = Sheets(2).Cells(x + 1, 3).Value And Sheets(2).Cells(x, 16).Value = Sheets(2).Cells(x + 1, 16).Value And Sheets(2).Cells(x, 2).Value = Sheets(2).Cells(x + 1, 2).Value Then
                     'copy the value of the missing label and delete the missing label row
                    'Application.ScreenUpdating = False
                    Sheets(2).Cells(x + 1, 3).Copy (Cells(x, 3))
                    Sheets(2).Cells(x + 1, 4).Copy (Cells(x, 4))
                    Sheets(2).Cells(x + 1, 5).Copy (Cells(x, 5))
                    Sheets(2).Cells(x + 1, 6).Copy (Cells(x, 6))
                    Sheets(2).Rows(x + 1).Delete Shift:=xlShiftUp
                    'calculate the delta for the frequecy and the %
                    Sheets(2).Cells(x, 11).Value = Abs(Cells(x, 5).Value - Cells(x, 9).Value)
                    Sheets(2).Cells(x, 12).Value = Abs(Cells(x, 6).Value - Cells(x, 10).Value)
                    'replace the comment to identify witch one have been change by the tool
                    If Sheets(2).Cells(x, 19) = "Orphan Label" Then Sheets(2).Cells(x, 19).Value = "Was Orphan Label"
                    If Sheets(2).Cells(x, 19) = "Orphan Label - Rate instability" Then Sheets(2).Cells(x, 19).Value = "Was Orphan Label - Rate instability"
                    Sheets(2).Rows(x).Interior.ColorIndex = 0
                    Sheets(2).Rows(x).Interior.ColorIndex = 36
                    'Application.ScreenUpdating = True
                    GoTo next_row
                End If
End If

next_row:


x = x + 1
Loop

End Sub

这是正确的(使用开发人员窗格运行时)

在此处输入图像描述

那是错误的(使用按钮运行时)

在此处输入图像描述

标签: excelvba

解决方案


推荐阅读