As the title suggests, I want a specific macro (PopulateCalculatorWithWeekChosen) to run whenever a user changes the value in the 'Summary' worksheet, cell H10.
I found the below question and answer and tried it but it doesn't seem to trigger it. Did I understand correctly that I put the code below...
Trigger event when select from dropdown
Sub PopulateCalculatorWithWeekChosen()
If Target.Address = "$H$10" Then
With Application
.EnableEvents = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
[the rest of the code in the PopulateCalculatorWithWeekChosen macro]
With Application
.EnableEvents = True
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End If
End Sub
...in the right place?
It doesn't seem to do anything. Thanks in advance for any help.
The problem is you've defined a macro and the question/answer you reference is using the Worksheet_Change event. In your VBA code, you should have Worksheet object which has a Change event. An event is just a function that is triggered when a specific thing happens.
In this case, the function Worksheet_Change triggers whenever the worksheet is modified. You should have something like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$H$10" Then
With Application
.EnableEvents = False
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
With Application
.EnableEvents = True
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With
End If
End Sub
When you modify the worksheet, Excel will automatically call this function and tell you which cells were modified by providing that information in the Target parameter.
Related
Dear community members,
We have disable the calculations in the workbook through below macro:
Private Sub Workbook_Open()
ActiveSheet.EnableCalculation = False
If Application.Calculation <> xlCalculationManual Then
Application.Calculation = xlManual
Else
End If
End Sub
But still when the workbook opens ,it calculates the formulas and takes some time to open. I ideally want to open the workbook without any initial calculations.
I tried the below code but it is not giving satisfactory results.
is there a ideal way to disable all the calculations??
Private Sub Workbook_Open()
ActiveSheet.EnableCalculation = False
Application.EnableEvents = False
Application.EnableEvents = True
If Application.Calculation <> xlCalculationManual Then
Application.Calculation = xlManual
Else
End If
End Sub
Would really appreciate you help on this.
This is the first time I couldn't find an answer to my problem on StackExchange. You guys are quite thorough. Anyway, we recently updated to Office 365/Excel 2016 from 2007, and now my VBA scripts won't run, except overnight. I researched and learned that I was a horrible person for using Select/Activate. I have seen the error of my ways, but now even simple code still doesn't want to run on a large sheet. My code, which clears the formatting from the current worksheet to make populating it with data faster:
Sub ClearFormattingAndValidation()
Dim referenceCell As Range
Dim rngToFormat As Range
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.EnableEvents = False
End With
Set referenceCell = Cells.Find("Some specific text", after:=ActiveCell, LookAt:=xlWhole)
Set rngToFormat = Range(referenceCell.Offset(2, 0), ActiveCell.SpecialCells(xlLastCell))
' rngToFormat.Select
With rngToFormat
.Validation.Delete 'near-instantaneous
.FormatConditions.Delete 'took 7-15 minutes on timed runs
End With
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
End With
End Sub
When I uncomment rngToFormat.Select, I get a total of 76,302 cells, to give you an idea of the size of the spreadsheet. There is a LOT of validation and conditional formatting. In 2007, even using Select/Selection, it ran in seconds. Now, I have no idea how long it takes. I gave up after 5 minutes. It does run successfully on a smaller version of the worksheet.
I would like to avoid removing the validation and conditional formatting if at all possible, but it IS a (time-consuming and costly) possibility for about half of it if that is the only way to speed it up.
Is there anything else that I can do to make the code run faster, or is there something that I'm doing wrong?
Edit: Code changed to reflect comments/suggestions as of 2/21. Similar results.
Your code runs instantly with Excel 2013.
Though the code can be written as below...
Sub ClearFormattingAndValidation()
Dim referenceCell As Range
Dim rngToFormat As Range
With Application
.Calculation = xlCalculationManual
.EnableEvents = False
.ScreenUpdating = False
End With
Set referenceCell = Cells.Find("Some specific text", after:=ActiveCell, LookAt:=xlWhole)
If Not referenceCell Is Nothing Then
Set rngToFormat = Range(referenceCell.Offset(2, 0), ActiveCell.SpecialCells(xlLastCell))
With rngToFormat
.FormatConditions.Delete
.Validation.Delete
End With
End If
With Application
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
With ActiveSheet
.EnableFormatConditionsCalculation = False
End With
This disables the conditional formatting that is causing the slowdown. It should be reenabled after the code is run.
thanks again, #sktneer. Your help pushed me in the right direction.
I'm using the following code (reduced to relevant parts for simplicity):
Sub deleteCategory()
Application.ScreenUpdating = False
Sheets("Recurring Expenses").Activate
'A bunch of stuff is checked and done on the now activated sheet
Sheets("Input").Activate 'This is the sheet the sub is called from via a button
Application.ScreenUpdating = True
End Sub
Although Application.ScreenUpdating is turned off, every time you click the button and the macro runs (otherwise bug-free) you can clearly see the Sheet "Recurring Expenses" briefly flash for a moment.
Any idea what causes this or how it could be fixed?
Try using With statement:
Sub deleteCategory()
With Excel.Application
.ScreenUpdating = False
.EnableEvents = False
End With
With Workbooks("your_workbook_name").Sheets("Recurring Expenses")
' do stuff like
' .copy
' .cells.select
End With
'A bunch of stuff is checked and done on the now activated sheet
With Workbooks("your_workbook_name").Sheets("Input") 'This is the sheet the sub is called from via a button
' Do stuff
End With
With Excel.Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Hope this help you.
I'm trying to create a VBA script that fires when a value within a cell range (in this case E7:E10) is selected (from a dropdown). However, Excel does not seem to give any indication of the macro firing, and I feel it's due to to the header lines. Here is the header line code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("E7:E10")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
Is there a way to be able to tell if it's running or not?
Is there a way to be able to tell if it's running or not?
Put a breakpoint or a MsgBox call inside the procedure:
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.Address
End If
If no message box pops up, then the event didn't fire. Check the value of Application.EnableEvents and also ensure macros are enabled.
Note that the Change event doesn't fire as a result of recalculation.
Try the below code
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "dropdown cell location" Then
With Application
.Calculation = xlCalculationManual
.EnableEvents = False
.ScreenUpdating = False
End With
End If
'your macro
If Target.Address = "dropdown cell location" Then
With Application
.Calculation =xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
End With
End If
End Sub
I am copy and pasting data from workbookA to workbookB every 1 minute interval using the application.ontime method. However while I'm running this macro I am working on something different entirely on workbookC. Each time the macro runs, workbookB pops up which interferes with what I'm doing on workbookC. It can get frustrating... is there a way around this?
My code has the structure:
sub dataextract()
ThisWorkbook.Activate 'ThisWorkbook is workbookA
'copy the data
If workbookB Is Nothing Then
Set workbookB= Workbooks.Open("C:\Users\Ken\Desktop\Df.xlsx")
Else
Set workbookB= Workbooks("Df.xlsx")
End If
workbookB.Activate
'paste the data here
workbookB.Save
timer 'timer is the sub that contains the application.ontime loop
end sub
Any suggestions?
sub dataextract()
dim xl as application
dim workbookB as workbook
'ThisWorkbook.Activate 'ThisWorkbook is workbookA, unnecessary to activate workbook when you copy
'copy the data
If workbookB Is Nothing Then
set xl = new application 'create another excel application
xl.visible = false 'set the new excel application invisible
Set workbookB= xl.Workbooks.Open("C:\Users\Ken\Desktop\Df.xlsx")
Else
Set workbookB= xl.Workbooks("Df.xlsx")
End If
'workbookB.Activate 'unnecessary to activate workbook when you paste
'paste the data here
workbookB.Save
timer 'timer is the sub that contains the application.ontime loop
end sub
You have two options to stop the flashing, using the below function, turn off screen updating (the function includes a few other things too, but don't forget to turn it back on).
Sub SetEvents(ByVal State As Boolean)
With Excel.Application
.ScreenUpdating = State
.EnableEvents = State
If State = True Then .Calculation = xlCalculationAutomatic Else .Calculation = xlCalculationManual
End With
End Sub
Or stop using the activeworkbook/select range method. If you define your references properly, ie.
Workbooks("Mybook.xlsx").Sheets("Sheet1").Range("A1").copy
The flashing will stop as the workbooks aren't in fact changing.
If you want to include the first option, as per your comment, add the function outside the sub and call it, as below:
sub dataextract()
setEvents False
ThisWorkbook.Activate 'ThisWorkbook is workbookA
'copy the data
If workbookB Is Nothing Then
Set workbookB= Workbooks.Open("C:\Users\Ken\Desktop\Df.xlsx")
Else
Set workbookB= Workbooks("Df.xlsx")
End If
workbookB.Activate
'paste the data here
workbookB.Save
timer 'timer is the sub that contains the application.ontime loop
setEvents True
end sub
Sub SetEvents(ByVal State As Boolean)
With Excel.Application
.ScreenUpdating = State
.EnableEvents = State
If State = True Then .Calculation = xlCalculationAutomatic Else .Calculation = xlCalculationManual
End With
End Sub