I need to run a macro after some data in a table/pivot table is refreshed. My table is reading data from a database, so I want the macro to run after the refresh. How would I do this using VBA. I've tried the following.
Private Sub Worksheet_PivotTableUpdate(ByVal target As PivotTable)
Application.Run "overrideManagers" 'Is this right?
End Sub
Sub overrideManagers() 'Macro to be run upon refresh of data
MsgBox "Hello"
End Sub
I don't get what was expected. I just get the refresh, but without the MsgBox. Even if I move the MsgBox to this.
Private Sub Worksheet_PivotTableUpdate(ByVal target As PivotTable)
MsgBox "Hello"
End Sub
I get the same results. This leads me to think that my original method for catching the update is wrong. Any ideas?
This may be a little lengthy answer. I'll try to explain clearly.
Below are the steps I followed
Created a new excel workbook with a simple table and data ( Sheet1)
Created a pivot table for that data ( Pivot table in another sheet - Sheet2)
3. Added the following code in Macro editor under Sheet2
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
MsgBox "update"
End Sub
4. Selected Sheet2, right click pivot table and refresh. Message box displayed
Hope this helps.
Related
I'm trying to run a macro every, time the selection of a dropdown menu in a cell changes. I've tried to follow this solution: automatically execute an Excel macro on a cell change
by entering the following code:
Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then Call Testsub
End Sub
Public Sub Testsub()
MsgBox "Testmessage", , "Testbox"
End Sub
Cell A1 contains the Dropdown menu that should trigger the macro "Testsub". However, nothing happens when I change the selection. I'd be very grateful for any idea, what might cause the problem.
The comment of user11138753 nailed it. I edited the code at the wrong place.
Thank you very much!
This has got to be ridiculously easy, but I just cannot figure it out searching the web.
I have an Excel macro that performs various data entry/manipulation tasks. There is a point in the macro where I want the user to have the option of using data in column A or column B for calculations. All I want is to call a userform with two command buttons which pass their "true" or "false" value to the main macro and then perform "if" statements based on this information.
The trouble is I cannot get the userform to "tell" the macro anything. I'm sure this is a major noob question and I'm missing the method, but I cannot get this to work. Thank you!
Edit, I've attached the userform code below by request:
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub OptionButton1_Click()
End Sub
Private Sub OptionButton2_Click()
End Sub
Private Sub UserForm_Initialize()
End Sub
you could avoid userform for that and use a simple message box:
If MsgBox("use data in column A or B [Yes=A, NO=B]", vbYesNo) = vbYes Then
' code for using data in column A
Else
' code for using data in column B
End If
I'm collecting measurements in an Excel workbook.
All the measurements are displayed in separate sheets.
The average and max of every sheets' measurement is displayed in the home sheet (with the name "1")
I added a line of code to jump from the home sheet to a certain sheet by entering the sheet name in cell J23 and clicking on a button.
Now i'd like to add a physical key/button in order to go back to the home sheet.
for example with ctrl+b
unfortunately my code does not work yet.
I've pasted my code below, hope someone has a solution.
Thanks in advance
Sub Macro1()
i = Sheets("1").Range("J23").Value
Worksheets(i).Select
End Sub
Sub Home()
Application.OnKey "^b", Worksheets("1").Select
End Sub
You need to pass Application.OnKey the name of a procedure, not a VBA command as you are trying to do.
Something along these lines:
Sub Home()
Sheets(1).Select
End Sub
Sub SetUp()
Application.OnKey "^b", "Home"
End Sub
The above subs are in a standard code module. When I run SetUp, I am afterwards able to get back to Sheet1 from any other sheet by entering Ctrl+b.
I am attempting to prompt a macro to run on a data refresh. I have the macro that needs to be run build, but I am having an issue with the new values not being used since the macros embedded in the sheet are called using ActiveX ComboBoxs.
I am finding several instances where people refer to AfterRefresh and BeforeRefresh, but I think I am misunderstanding how this would take effect and call a macro.
I currently am running ComboBoxs so I have multiple instances of
Private Sub ComboBox22_Change()
'do stuff
End Sub.
but I need the 'do stuff' to occur upon a data refresh, including refreshes that happen automatically and upon sheet open.
I don't want to tie the refresh to a specific box because the items that are refreshed are not dependent on any one instance of data change.
Any help is greatly appreciated.
Thank you.
Maybe a worksheet change event would help in this situation.
Right Click the sheet tab, select "View Code", Select "Worksheet" then "Change."
Code will automatically kick in when a specific range of cells has been changed.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub ' this stops code error if more than one cell is changed at once
If Not Application.Intersect(Target, Me.Range("A1:C10")) Is Nothing Then ' indicates the Target range
MsgBox "You have changed " & Target.Address & " to " & Target
End If
End Sub
You could also use Worksheet_pivottableupdate event to run the macro. You set it up in a similar way to davesexcel answer above.
The connection in question may not be a pivot table but you can use a small and fast pivot table as a trigger.
Set the pivot table to update at the same time as your connection (e.g. set to self refresh every 5 minutes or on workbook open).
I have a problem with a piece of VBA code:
Sub Macro3()
Sheets("Output").Select
Rows("5:160").Select
Selection.Rows.AutoFit
End Sub
What - from my point of view it should do - is applying Autosize to Rows 5:160 within the Sheet "Output". I added the code to the sheet "Output" so whenever I open it it should be properly resized automatically. However, nothing happens. But if I manually select the rows and press CRTL+C+H+O+A to auto size the rows it works properly.
Does anyone has a guess where the mistake lies? Would be a huge help!
Thanks upfront!
whenever I open it it should be properly resized automatically.
For this you have to place the code in the ThisWorkbook code area and you have to use the Workbook_Open() event
See this
Private Sub Workbook_Open()
Sheets("Output").Rows("5:160").Rows.AutoFit
End Sub
SNAPSHOT