我在这里想要实现的是我在三张表中运行了三个不同的仪表板,我每隔1分钟就会切换一次。我坚持使用下面的代码。任何帮助,将不胜感激。
我有三张纸在两者之间切换 1. First_sheet,2。Second_Sheet,3。Third_Sheet
Sub Swap_Sheets()
Dim Sheets As Workbook
Dim dTime As Date
dTime = Now + TimeValue("00:00:60")
Application.OnTime dTime, "Swap_Sheets"
If ActiveSheet.Name = "First_Sheet" Then
Sheets("Second_Sheet").Activate
Else
Sheets("Third_Sheet").Activate
Else
Sheets("First_Sheet").Activate
End If
If Sheets("Second_sheet").CheckBox1.Value = False Then
Application.OnTime dTime, "Swap_Sheets", , False
End If
End Sub
答案 0 :(得分:0)
这是一个很好的方法,避免多个if-s,select case和recursion:
Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Sub SwapSheets()
Dim dTime As Date
Dim i As Long: i = 1
While i <= ThisWorkbook.Worksheets.Count
If ActiveSheet.Name = Worksheets(Worksheets.Count).Name Then
Worksheets(1).Activate
Else
Worksheets(ActiveSheet.index + 1).Activate
End If
i = i + 1
Sleep (10000) '10 seconds
Wend
End Sub
这个想法是每个工作表都有一个索引,你只需要增加活动索引的索引。如果最后一张纸是activeone,请从头开始。 Sleep需要几毫秒作为参数,60秒则为60.000。
另外 - 在您的代码中,您有Dim Sheets As Workbook
,在这里您可能意味着Worksheet
(我只是猜测)。
如果您只想激活3个工作表,这可能是最简单的方法:
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Sub SwapSheets()
Dim sheetNames() As Variant
Dim i As Long
sheetNames = Array("Sheet1Name", "Sheet2Name", "Sheet3Name")
For i = LBound(sheetNames) To UBound(sheetNames)
Sheets(sheetNames(i)).Activate
Sleep (10000) '10 seconds
Next i
End Sub