VBA'停止'对宏观结果的影响

时间:2015-10-09 05:59:38

标签: vba excel-vba excel-2013 excel

我正在尝试比较一列中每对细胞的值。如果它们不同,我想用它做点什么。

如果我在'If'语句中使用关键字'Stop'运行一个宏并单击F5几次就可以了,但是当我从代码中删除“Stop”时,我得到了完全不同的结果。在第二个版本中立即窗口不打印某些单元格的任何值,可能会导致问题。任何想法为什么?

按要求编辑:(代码前的一些额外行)

Dim LastRow As Long
Dim Element As Excel.Range

Worksheets("Dane").Activate
LastRow = Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Application.Calculation = xlCalculationManual
Application.Calculate

Edit2:复制操作

Range("F" & Element.Row).Copy Destination:=Worksheets("histogram").Range("C1048576").End(xlUp).Offset(1, 0)
Range("F" & Element.Row + 1).Copy Destination:=Worksheets("histogram").Range("B1048576").End(xlUp).Offset(1, 0)
Range("Y" & Element.Row + 1).Copy
Worksheets("histogram").Range("D1048576").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone

1块代码:

Worksheets("Dane").Activate
For Each Element In Worksheets("Dane").Range("Y2:Y" & LastRow)
    If Range("Y" & Element.Row + 1).Value <> Element.Value Then
        Debug.Print Range("Y" & Element.Row + 1).Address, Range("Y" & Element.Row + 1).Value, "!=", Element.Value, Element.Address
        Stop '<-- the only difference
        'some code that copy some cells
    End If
Next

1立即窗口(正确答案):

$Y$21         True          !=            False         $Y$20
$Y$86         False         !=            True          $Y$85
(...)
$Y$1835       True          !=            False         $Y$1834
$Y$3198       False         !=            True          $Y$3197

2秒版本(没有Stop语句):

Worksheets("Dane").Activate
For Each Element In Worksheets("Dane").Range("Y2:Y" & LastRow)
    If Range("Y" & Element.Row + 1).Value <> Element.Value Then
        Debug.Print Range("Y" & Element.Row + 1).Address, Range("Y" & Element.Row + 1).Value, "!=", Element.Value, Element.Address
        'some code that copy some cells
    End If
Next

2立即窗口(错误答案):

(...)
$Y$3194                     !=            True          $Y$3193
$Y$3195                     !=            True          $Y$3194
$Y$3196                     !=            True          $Y$3195
$Y$3197                     !=            True          $Y$3196
$Y$3198                     !=            True          $Y$3197

0 个答案:

没有答案
相关问题