编程一个按钮来停止无限循环

时间:2017-08-20 19:30:55

标签: excel vba excel-vba

我想连续在工作簿1中显示excel表,所以我使用无限循环。 我的问题是,当我需要更新这些表中的数据时,我必须停止无限循环,我该如何使用VBA代码?例如,我想按工作簿2中的按钮来停止工作簿1中的无限循环(顺便说一句,我不能将按钮放在工作簿1中,因为工作表不断更改

或者有没有人更好地了解如何连续展示纸张?

这是代码: **所以Sheet MacroKeys只包含单元格A1 = yes中的单词作为触发器,以显示每张纸张2秒钟(sheet1,sheet2,sheet3再次返回到纸张1并且无限重复该过程

Sub Show_Sheet1()
Dim MacroKeys As Worksheet: Set MacroKeys = Sheets("MacroKeys")
Dim Sh1 As Worksheet: Set Sh1 = Sheets("Sheet1")
Dim Sh2 As Worksheet: Set Sh2 = Sheets("Sheet2")
Dim Sh3 As Worksheet: Set Sh3 = Sheets("Sheet3")

MacroKeys.Range("A1") = "Yes"
' [...]
Sh1.Visible = True
Sh1.Select
Sh2.Visible = False
Sh3.Visible = False

' [...]
If MacroKeys.Range("A1") = "Yes" Then
alertTime = Now + TimeValue("00:00:02")
Application.OnTime alertTime, "Show_Sheet2"
    End If
End Sub

Sub Show_Sheet2()
Dim MacroKeys As Worksheet: Set MacroKeys = Sheets("MacroKeys")
Dim Sh1 As Worksheet: Set Sh1 = Sheets("Sheet1")
Dim Sh2 As Worksheet: Set Sh2 = Sheets("Sheet2")
Dim Sh3 As Worksheet: Set Sh3 = Sheets("Sheet3")

    MacroKeys.Range("A1") = "Yes"
    ' [...]
    Sh2.Visible = True
    Sh2.Select
    Sh1.Visible = False
    Sh3.Visible = False

    ' [...]
    If MacroKeys.Range("A1") = "Yes" Then
    alertTime = Now + TimeValue("00:00:02")
    Application.OnTime alertTime, "Show_Sheet3"
    End If
    End Sub

Sub Show_Sheet3()
Dim MacroKeys As Worksheet: Set MacroKeys = Sheets("MacroKeys")
Dim Sh1 As Worksheet: Set Sh1 = Sheets("Sheet1")
Dim Sh2 As Worksheet: Set Sh2 = Sheets("Sheet2")
Dim Sh3 As Worksheet: Set Sh3 = Sheets("Sheet3")

MacroKeys.Range("A1") = "Yes"
' [...]
Sh3.Visible = True
Sh3.Select
Sh1.Visible = False
Sh2.Visible = False

' [...]
If MacroKeys.Range("A1") = "Yes" Then
alertTime = Now + TimeValue("00:00:02")
Application.OnTime alertTime, "Show_Sheet1"
End If
End Sub

1 个答案:

答案 0 :(得分:0)

我认为你正在为你的"连续循环使用某种Application.OnTime()函数"。设置一个布尔变量" Loop" to" True"或" False",并在您更改工作表的方法中,检查布尔值是否为真。有一个按钮将布尔值设置为false。

如果您发布了一些您已经使用过的代码,我们可以帮助您完善它。

编辑:一些代码/更多定义: 您可以使用无模式窗体来控制开始/停止。为此,请使用.Show()方法中的vbModeLess参数:

Dim oForm As UserForm1
Set oForm = New UserForm1
oForm.Show vbModeless

然后,Userform可以使用按钮来启动和停止循环:要启动它,请将公共布尔值设置为True以继续循环,然后启动循环。要停止它,请使用另一个按钮将相同的布尔值设置为false

使用更好的按钮名称,但这是简单的代码:

Private Sub CommandButton1_Click()
ContinueLoop = True
LoopingController
End Sub

Private Sub CommandButton2_Click()
ContinueLoop = False
Unload Me
End Sub

就其他代码而言:用于控制循环的变量(在标准模块中):

Public ContinueLoop As Boolean
Public Const IntervalSeconds = 5
Public Const codeToRun = "LoopingController"

运行循环的代码:

Public Sub LoopingController()
If ContinueLoop = True Then
    LoopAction
    Application.OnTime Now + TimeSerial(0, 0, IntervalSeconds), codeToRun       '//This calls itself, as long as the looping variable is 'true'
End If
End Sub

每次实际运行的代码:

Public Function LoopAction()
Dim visibleSheetIndex As Long
visibleSheetIndex = ThisWorkbook.ActiveSheet.Index

If (visibleSheetIndex = ThisWorkbook.Sheets.Count) Then
    visibleSheetIndex = 1
Else
    visibleSheetIndex = visibleSheetIndex + 1
End If

ThisWorkbook.Sheets(visibleSheetIndex).Activate
End Function

你可以通过以下方式开始:

Public Sub test()

Dim oForm As UserForm1
Set oForm = New UserForm1
oForm.Show vbModeless

End Sub

对不起 - 我在这里舀勺代码,但我喜欢挑战。您还可以设置start-up position以使其不那么突兀。

此外,您可以使用功能区上的ToggleButton执行类似操作。