Clearing Excel AutoFilter using VBA - excel

I am using this VBA code to refresh my entire workbook at specific time intervals. (thanks to this thread)
As you can see, it is currently set to refresh every 60 minutes.
Public RunWhen As Double
Public Const cRunIntervalMinutes = 60
Public Const cRunWhat = "Workbook_RefreshAll"
Sub StartTimer()
RunWhen = Now + TimeSerial(0, cRunIntervalMinutes, 0)
Application.OnTime earliesttime:=RunWhen, procedure:=cRunWhat, _
schedule:=True
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime earliesttime:=RunWhen, _
procedure:=cRunWhat, schedule:=False
End Sub
Sub Workbook_RefreshAll()
Application.CalculateFullRebuild
ActiveWorkbook.RefreshAll
Call StartTimer
End Sub
The code works great, however there’s one thing I wish it could also do:
Any time the sheet is opened, and/or
Any time this VBA code executes the sheet refresh process
I would like the AutoFilter to be "Cleared" (not disabled or turned off) as in the screen grab below:
AutoFilter Clear
Thanks in advance for any assistance.
Cheers

This should do the trick. You may of course do it for one Sheet only, if you wish.
Sub Workbook_RefreshAll()
Dim sh As Worksheet
Application.CalculateFullRebuild
ActiveWorkbook.RefreshAll
For Each sh In ActiveWorkbook.Worksheets
If Not (sh.AutoFilter Is Nothing) Then sh.AutoFilter.ShowAllData
Next sh
Call StartTimer
End Sub

Related

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

Workbook, which contains a clock, automatically reopens

I've got three Subs in Module: runClock, startClock and stopClock.
runClock is showing a clock that is updated every 5 seconds.
startClock is used to start the clock and is called from Sub Workbook_Open.
stopClock - is used to stop the clock and is called from Sub Workbook_BeforeClose.
However, when ever I close the workbook, it reopens automatically and the clock continues to run. I've checked the value of ClockOn and it's set to FALSE after the Workbook_BeforeClose is executed. I think that that issue has to do with Application.OnTime, but don't know how to fix it.
This Code is in Module:
Global ClockOn As Boolean
Sub runClock()
With Range("A1")
.Value = Now()
.NumberFormat = "hh:mm:ss"
If ClockOn = True Then
Application.OnTime Now + TimeSerial(0, 0, 5), "runClock"
End If
End With
End Sub
Sub startClock()
ClockOn = True
runClock
End Sub
Sub stopClock()
ClockOn = False
End Sub
This Code is in ThisWorkbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
stopClock
ActiveWorkbook.Save
End Sub
Private Sub Workbook_Open()
startClock
End Sub
This seems to be happening because the times resulting from Application.OnTime are not the same. The initial call time should be saved as a variable and then referenced in the subs. See this solution.
https://www.ozgrid.com/forum/forum/help-forums/excel-general/131658-timer-reopens-workbook-when-closed
Found a solution in http://www.cpearson.com/excel/OnTime.aspx
I've changed my sub stopClock so now it includes the following code:
RunWhen = Now + timeserial(0, 0, 5)
Application.OnTime earliestTime:=RunWhen, Procedure:="startClock", Schedule:=False
This stops the clock from running and the workbook from reopening.
I don't know what caused it to reopen under the previous code, but at least now it's fine.

Excel Randbetween - Refresh without Shift-F9

I'm looking to have an excel worksheet automatically calculate a rand between function without having to hit shift f9 or calculate the workbook/worksheet.
I'm looking to do this in order to create some animated conditional formatting, but the rand between function won't update automatically even when auto calculate is turned on.
Place the following code in a standard module:
Public RunWhen As Double
Public Const cRunIntervalSeconds = 1
Public Const cRunWhat = "The_Sub"
Sub StartTimer()
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
Application.OnTime earliesttime:=RunWhen, procedure:=cRunWhat, _
schedule:=True
End Sub
Sub The_Sub()
Application.CalculateFullRebuild
StartTimer
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime earliesttime:=RunWhen, _
procedure:=cRunWhat, schedule:=False
End Sub
This implements 1 second auto-re-calculation:
to begin, run StartTimer
to stop, run StopTimer
Adapted from Chip Pearson
So I ended up doing a VB script to automatically do something to the spreadsheet using this example:
VBA Macro On Timer style to run code every set number of seconds, i.e. 120 seconds
This basically made all of the formulas update without the user doing anything (entering data or calculating).

create a countdown timer in excel 2010 using VBA (excel)

I'm using Excel 2010, and want to create a countdown timer in excel. The code will be started and stopped by using buttons with macros attached to them. The problem occurs when the "start" button is pushed. The code above is utilizing Cell "B1" as the input spot, because I was just trying to get it to work, but every time I tried, it would always say:
"Cannot run the macro . The macro may not be available in this workbook or all macros may be disabled".
Yes I enabled all macros before putting this here. I want to be able to take user input, and to make that the time that the timer starts at instead of just using cell "B1".
'Macro for Starting the timer (attached to the start button)
Sub startTimer()
Application.OnTime Now + TimeValue("00:00:01"), "nextTick"
End Sub
'Macro for next second
Sub nextTick()
Sheet1.Range("B1").Value = Sheet1.Range("B1").Value - TimeValue("00:00:01")
startTimer
End Sub
'Macro for stopping the timer (attached to the end button)
Sub stopTimer()
Application.OnTime Now - TimeValue("00:00:01"), "nextTick", , False
End Sub
I modified your code very slightly and put it in a standard module:
Public Future As Double
Sub StartTimer()
Future = Now + TimeSerial(0, 0, 1)
Application.OnTime earliestTime:=Future, procedure:="nextTime", _
schedule:=True
End Sub
Sub nextTime()
Sheet1.Range("B1").Value = Sheet1.Range("B1").Value - TimeValue("00:00:01")
StartTimer
End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime earliestTime:=Future, _
procedure:="nextTime", schedule:=False
End Sub
Both StartTimer() and StopTimer() run just fine.

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"

Resources