你可以试试这个。宏是通过 3 张纸的无限循环(可以设置为任何数字),基于计时器设置(此处为2 秒)。您可以在每个工作表加载时通过代码插入一个 msg./validation 框,以询问用户是否要继续改组或停止当前选择(这不是那么用户友好)。或者,您可以创建一个新工作表(即 MacroKeys)并设置洗牌的验证值(是/否)(检查下面的代码)。最后一步是在每个工作表上添加 2 个按钮以创建一个所谓的 UI 来调用宏 [Shuffle] 和 [Stop]。无论如何,您应该使用[Application.OnTime alertTime, "macro name"]函数
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
Sub Stop_Shuffling()
Dim MacroKeys As Worksheet: Set MacroKeys = Sheets("MacroKeys")
'edit !!!
MacroKeys.Range("A1").Value = "No"
End Sub
笔记!您必须添加一个新工作表并将其命名为 "MacroKeys"。你可以隐藏它并让它保持原样。笔记!要添加分配有宏的按钮:按 Alt + N、+SH 并选择一个形状。然后,右键单击形状 > 分配宏(并选择相应的宏)。可选的!使用 Alt + W、+F、+R 阻止顶行并将按钮高度 = 保持在第 1 行的高度。笔记!如果您不想隐藏工作表,只需用 '注释掉 .Visible = false/true并将Select替换为Activate。你会想出来的!希望能帮助到你!