为什么不循环?

时间:2018-07-11 18:02:16

标签: excel vba excel-vba

自动滚动工作正常,但是我试图使它循环返回,但是一旦返回A1,它就会停止并且宏似乎已完成。我做错了什么?

 Sub ReRunMacro()
 Dim xMin As String
 Dim lastRow As Long, i As Long
 Dim ws As Worksheet
 Dim validSheets() As Variant

 Set ws = ActiveSheet

 validSheets = Array("CNC Machining Cell 2", "CNC Grinding Cell", "CNC Turning Cell 1 & 3", "CNC Turning Cell 2")

 If UBound(Filter(validSheets, ws.Name)) = -1 Then 
 Exit Sub 
 End If

 lastRow = Range("A" & Rows.Count).End(xlUp).Row

 For i = 1 To 14 Step 2


       Cells(i, 1).Select
       ActiveWindow.SmallScroll down:=1
       Application.Wait (Now + TimeValue("0:00:02"))
       If i = lastRow - 2 Or i = lastRow - 1 Then
       i = 0
       Cells(1, 1).Select
    End If
  Next i
  Range("A1").Activate

  End Sub

2 个答案:

答案 0 :(得分:0)

以下示例将永远滚动。但是我建议使用DoEvents和一个停止开关,否则它将永远运行并且不会停止。

Option Explicit

Public StopScroll As Boolean 'global switch to stop scrolling

Public Sub ScrollForever()
    Dim ws As Worksheet
    Set ws = ActiveSheet

    Dim LastRow As Long
    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    Dim iRow As Long
    iRow = 1

    StopScroll = False 'initialize scroll

    Do
        DoEvents 'make excel responsive
        If StopScroll Then Exit Sub 'possibility to stop the scroll again

        ActiveWindow.ScrollRow = iRow
        Application.Wait (Now + TimeValue("0:00:01"))

        iRow = iRow + 1 'count 1 row up (change it to 2 for step 2)
        iRow = IIf(iRow > LastRow, 1, iRow) 'if last row is reached reset to 1
    Loop While iRow <= LastRow 'this is never true because of the line above and therefore runs forever
End Sub

您可以使用以下示例。按钮停止滚动:

Public Sub StopIt()
    StopScroll = True
End Sub

答案 1 :(得分:0)

所以我对上述代码所做的只是添加了一个do ...循环,现在重复执行:

    parquetMapDriver = MapDriver.newMapDriver(parquetMapper);
    conf = parquetMapDriver.getConfiguration();
    conf.setStrings("io.serializations", conf.get("io.serializations"),
    MutationSerialization.class.getName(),
    KeyValueSerialization.class.getName(),
    WritableSerialization.class.getName(),
    ResultSerialization.class.getName());