Reputation: 21
I have a set of sheets on a workbook. I have names of the sheets in a particular column on one of the sheets. This workbook will be displayed on a monitor/dashboard. Is there a way I can loop through the sheets and show them one-by-one.
Eg, the book has 2 sheets: Sheet1 and Sheet2. When I run the macro, the Sheet1 should be visible on the screen for 5 seconds, then Sheet2 for 5 seconds. Then Sheet1 (loop).
I am able to add the time delay and loop, however just not able to change display of sheet. I have tried the methods below, but those do not work.
Application.ScreenUpdating = True
Worksheets("Sheet1").Activate
Worksheets("Sheet1").Select
update :
I have tried all the suggestions, but could not get it to work. When I run the code, the sheet display doesn't change. Eg, is my workbook has three sheets Sheet1, Sheet2 and Sheet3 and I write the code to display sheet1 for 10 sec, sheet2 for 10 secs and sheet3 for 10 sec in that order, during execution excel is stuck on the sheet from where I started the macro for 30 secs and displays the final sheet (sheet3). Not sure if this is because of some settings in the code/application.
Anyway, since this is not working, I have decided to copy the charts from Excel to a PowerPoint (by VBA) and use the ppt to display on the dashboards. I have used the code provided in the below link.
http://peltiertech.com/Excel/XL_PPT.html
Upvotes: 1
Views: 93991
Reputation: 387
You might try this. The macro is an endless loop through 3 sheets (it can be set to any number), based on timer set (here 2 sec). You can either insert a msg./validation box via code on each sheet load to ask the user if he wants to keep shuffling or stop on the current selection (this is not that userfriendly). Or, you might create a new sheet (i.e. MacroKeys) and set the validation value (Yes/No) for the shuffling (check the code below). The final step is to add 2 buttons on each worksheet to create a so called UI to call the macros [Shuffle] and [Stop]. Anyway you should use the [Application.OnTime alertTime, "macro name"] function
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
Note! You must add a new sheet and name it "MacroKeys". You can hide it and leave it be. Note! To add buttons with macro assigned to them: press Alt + N, +SH and select a shape. Then, right click on the shape > Assign Macro (and select the corresponding macros). Optional! Block the top row using Alt + W, +F, +R and keep the buttons height = to the row 1 height. Note! It you don't want to hide the sheets just comment out the .Visible = false/true with ' and replace the Select with Activate. You'll figure it out! Hope it helps!
Upvotes: 2
Reputation: 12353
Its always better to use the workbook name followed by sheet name then the range to avoid confusion. This code will work even if multiple workbooks are open.
Sub changeSh()
'Application.ScreenUpdating = True its true by default
Dim wkb As Workbook
Set wkb = ThisWorkbook
wkb.Sheets("Sheet1").Activate
Application.Wait Now + TimeSerial(0, 0, 5)
wkb.Sheets("Sheet2").Activate
End Sub
Upvotes: 2
Reputation: 1335
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub changeSh()
While (True)
Sheets(1).Activate
'Use your method to add time delay here
'Sleep 5000
Sheets(2).Activate
'Use your method to add time delay here
'Sleep 5000
Wend
End Sub
Upvotes: 1
Reputation: 871
Activating sheet is done BY Sheet.Activate method. Try this
Application.ScreenUpdating = True
Worksheets("Sheet1").Activate Worksheets("Sheet1").Select
Worksheets("Sheet1").Activate Worksheets("Sheet1").Activate
Upvotes: 0