Excel - Automated Worksheet Switching Loop **only specific worksheets** - excel

I am working on an excel dashboard and need to display 2 specific worksheets in the workbook and switch between tabs with an interval of 30 seconds per tab and then return to the first tab and repeat.
I found a macro that is similar to what I need, Excel - Automated Worksheet Switching Loop, however I am trying to only show 2 specific worksheets and not all worksheets in the workbook.
Here is the code that I am using:
Sub StartSlideShow()
Application.OnTime Now + TimeValue("00:00:30"), "ShowNextSheet"
End Sub
Sub ShowNextSheet()
Dim lastIndex As Integer, nextShtIndex As Integer
lastShtIndex = Worksheets.Count
nextShtIndex = ActiveSheet.Index + 1
If nextShtIndex <= lastShtIndex Then
Worksheets(nextShtIndex).Select
StartSlideShow
Else
Worksheets(1).Select
StartSlideShow
End If
End Sub

Something like this:
Sub SlideToOne()
Application.OnTime Now + TimeValue("00:00:30"), "ShowFirstSheet"
End Sub
Sub SlideToTwo()
Application.OnTime Now + TimeValue("00:00:30"), "ShowSecondSheet"
End Sub
Sub ShowFirstSheet()
Worksheets(1).Select
SlideToTwo
End Sub
Sub ShowSecondSheet()
Worksheets(2).Select
SlideToOne
End Sub

Related

Getting error for macro when moving to a different sheet

Im getting a Run-time error '1004':
Select method of range class failed
This only happens when I move away from the sheet (Bet Angel) that has the macro running to another sheet. The code is below:
Sub DeleteStatus()
Worksheets("Bet Angel").Range("O6:O50").Select
Selection.ClearContents
Call Start
End Sub
Sub Start()
Application.OnTime Now + TimeValue("00:00:15"), "DeleteStatus"
End Sub
I want the macro to run even if I am moving between different sheets in the workbook.
Since you are moving to another worksheets, you need to activate or select the target worksheet first.
Sub DeleteStatus()
Sheets("Bet Angel").Activate
Worksheets("Bet Angel").Range("O6:O50").Select
Selection.ClearContents
Call Start
End Sub
Sub Start()
Application.OnTime Now + TimeValue("00:00:15"), "DeleteStatus"
End Sub
If you want to go back to your original worksheet, you can activate it at the end of DeleteStatus()
Like this
Sub DeleteStatus()
Sheets("Bet Angel").Activate
Worksheets("Bet Angel").Range("O6:O50").Select
Selection.ClearContents
Call Start
Sheets("your original worksheet").Activate
End Sub
If you don't want to use activate, then you need to dim the worksheet first.
Like this
Sub DeleteStatus()
Dim ws As Worksheet
Set ws = Sheets("Bet Angel")
ws.Range("O6:O50").ClearContents
Call Start
End Sub
Sub Start()
Application.OnTime Now + TimeValue("00:00:15"), "DeleteStatus"
End Sub

What to use instead of .EntireColumn

I'm trying to create a simple counting sheet which has 5 buttons to increment the count of each field, a 'New Round' Button to start a new round in a new column (R2 and so forth) and Clear button which clears all the counts and start over again from Round1
So far I've come up with this code:
Function rngLastRound() As Range
With Range("2:2").Cells(1, Columns.Count).End(xlToLeft)
Set rngLastRound = .EntireColumn
End With
End Function
Sub IncrementCurrentRoundOfRow(N As Long)
With rngLastRound
.Cells(N, 1).Value = Val(CStr(.Cells(N, 1).Value)) + 1
End With
End Sub
Sub IncrementCurrentRoundA()
Call IncrementCurrentRoundOfRow(3)
End Sub
Sub IncrementCurrentRoundB()
Call IncrementCurrentRoundOfRow(4)
End Sub
Sub IncrementCurrentRoundC()
Call IncrementCurrentRoundOfRow(5)
End Sub
Sub IncrementCurrentRoundD()
Call IncrementCurrentRoundOfRow(6)
End Sub
Sub IncrementCurrentRoundE()
Call IncrementCurrentRoundOfRow(7)
End Sub
Sub NewRound()
With rngLastRound.Offset(0, 1)
.Cells(2, 1).Value = "R" & (.Column - 1)
.Cells(3, 1).Resize(5, 1).Value = 0
End With
End Sub
Sub Clear()
Range("B2", rngLastRound).ClearContents
Call NewRound
End Sub
The code works fine, but it clears the entire columns so that means the totals and grand total also gets cleared. How do I prevent this from happening by not specifying .EntireColumn attribute and instead a specific range?
Thanks
A Game
Option Explicit
Function rngLastRound() As Range
With Range("2:2").Cells(Columns.Count).End(xlToLeft)
Set rngLastRound = .Resize(6)
End With
End Function
Sub IncrementCurrentRoundOfRow(N As Long)
With rngLastRound
.Cells(N).Value = Val(CStr(.Cells(N).Value)) + 1
End With
End Sub
Sub IncrementCurrentRoundA()
Call IncrementCurrentRoundOfRow(2)
End Sub
Sub IncrementCurrentRoundB()
Call IncrementCurrentRoundOfRow(3)
End Sub
Sub IncrementCurrentRoundC()
Call IncrementCurrentRoundOfRow(4)
End Sub
Sub IncrementCurrentRoundD()
Call IncrementCurrentRoundOfRow(5)
End Sub
Sub IncrementCurrentRoundE()
Call IncrementCurrentRoundOfRow(6)
End Sub
Sub NewRound()
With rngLastRound.Offset(, 1)
.Cells(1).Value = "R" & (.Column - 1)
.Cells(2).Resize(5).Value = 0
End With
End Sub
Sub Clear()
Range("B2", rngLastRound).ClearContents
Call NewRound
End Sub
Try changing the line Set rngLastRound = .EntireColumn in the rngLastRound () function to Set rngLastRound = Application.Intersect (.EntireColumn, Rows ("2: 7"))

stop refreshing all queries at once in excel using macro

[![enter image description here][1]][1]I am refreshing all queries in the workbook after some specific interval using
this macro.
Sub macro_t()
ThisWorkbook.RefreshAll
interval = Now + TimeValue("00:00:015")
Application.OnTime interval, "macro_t"
End Sub
I want macro which will stop refreshing queries.
something like
stoprefreshall
how can I do it?
edit
the way i solve the issue is, stopping refresh of each sheet one by one
and then again run macro.
I want to stop refreshing all the sheets in single step, using macro
[
What you can do is declare a boolean type variable outside of procedure. And create a macro which will set that boolean variable to false and then macro will not run after run the `StopMacro(). Try below codes.
Public Times As Boolean
Sub start()
Range("E3").Value = Format(Now(), "HH:MM:SS")
Times = True
run
End Sub
Sub run()
If Not Times Then Exit Sub
Application.OnTime Now() + TimeValue("00:00:01"), "Run"
Range("E3").Value = Range("E3").Value + TimeValue("00:00:01")
End Sub
Sub StopMacro()
Times = False
End Sub
For better visualization see the screenshot.
Lots of trial and error
and finally this worked.
Sub Macro1()
'
' Macro1 Macro
'
'
With Sheets("r1").Range("B2").ListObject.QueryTable
If .Refreshing Then .CancelRefresh
End With
With Sheets("r2").Range("B2").ListObject.QueryTable
If .Refreshing Then .CancelRefresh
End With
With Sheets("r3").Range("B2").ListObject.QueryTable
If .Refreshing Then .CancelRefresh
End With
With Sheets("s1").Range("B2").ListObject.QueryTable
If .Refreshing Then .CancelRefresh
End With
With Sheets("s2").Range("B2").ListObject.QueryTable
If .Refreshing Then .CancelRefresh
End With
With Sheets("s3").Range("B2").ListObject.QueryTable
If .Refreshing Then .CancelRefresh
End With
End Sub

VBA application.ontime is not working

I am trying to achieve recursive functionality in macro using a code similar to this -:
Dim showTime As Boolean
Sub RunClock()
Range("A1", "A1").Value = Now
If showTime = True Then
Application.OnTime Now + TimeValue("00:00:01"), "RunClock"
End If
End Sub
Sub StartClock()
showTime = True
RunClock
End Sub
Sub StopClock()
showTime = False
End Sub
When I execute StartClock it shows time once then gives error - "Cannot run the macro 'Book1.RunClock'. The macro may not be available in this workbook or all macros may be disabled.
Try to prefix sheet name along with macro name. Excel might me be looking in whole work book with name like Sheet1.RunClock, so try as below (assuming the macro is in Sheet1)
Application.OnTime Now + TimeValue("00:00:01"), "!Sheet1.RunClock"

How can i change excel sheets with 5sec time gap one after another using VB code by macro?

I have more than 10 excel sheets in a work book prepared for a seminar .I have to make a slide show for these sheets ,so that each sheet should be automatically changed one after the other.Can u please help me solve this task ?
This might help you get started. It will show each sheet in a workbook every 5 seconds, starting at the first sheet in the workbook and ending with the last.
Sub StartSlideShow()
Application.OnTime Now + TimeValue("00:00:05"), "ShowNextSheet"
End Sub
Sub ShowNextSheet()
Dim lastIndex As Integer, nextShtIndex As Integer
lastShtIndex = Worksheets.Count
nextShtIndex = ActiveSheet.Index + 1
If nextShtIndex <= lastShtIndex Then
Worksheets(nextShtIndex).Select
StartSlideShow
Else
MsgBox "End of slide show"
End If
End Sub

Resources