持续计数跨子

时间:2016-10-14 11:22:08

标签: excel-vba vba excel

我正在运行我的主要子,我想暂停每5个循环10秒。我的代码似乎有效,但我面临的问题是我的计数变量(即j,i和n)在暂停子运行后重置了它们的计数。

是否有一种有效的方法可以在运行sub?

中的sub后保持当前计数
if (completedForm.isValid()) {
    return formDataQueue.push(formJson, this.company).then(function () {
        return self.trySync().catch(function(error) {
            router.navigate('home');
        });
    });
}

2 个答案:

答案 0 :(得分:0)

尝试使用Application.Wait功能,而不是Application.OnTime

您的代码如下:

Sub DataPopulation()
Dim count As Integer
Dim n As Integer
Dim i As Integer
Dim Total As Long
Dim j As Integer
i = 6
n = 7
Total = Cells(Rows.count, "A").End(xlUp).Row

For j = 7 To Total
If j Mod 5 = 0 Then
    Application.Wait(Now + TimeValue("00:00:10")
    Exit Sub
End If
Range("B" & i & ":" & "FO" & i).Select
Selection.AutoFill Destination:=Range("B" & i & ":" & "FO"     &n    ),Type:=xlFillDefault
n = n + 1
i = i + 1
Next j

End Sub

答案 1 :(得分:0)

尝试以下方法。如果您有疑问,请询问。

Sub DataPopulation()
Static I As Integer
Dim Total As Long
Dim j As Integer

Total = Cells(Rows.count, "A").End(xlUp).Row
If I = 0 Or I >= Total Then I = 6


For j = 1 To 10
    If I >= Total Then Exit Sub
    Range("B" & I & ":" & "FO" & I).AutoFill Destination:=Range("B" & I & ":" & "FO" & I + 1), Type:=xlFillDefault
    I = I + 1
Next j
Call Application.OnTime(Now + TimeValue("00:00:10"), "DataPopulation")
End Sub