I am trying to write a vba only to refresh the local pivot tables. Since the pivot tables are getting data from a power query which links to a password locked excel file. In the past few days, I managed to write a vba to open the external excel file to refresh my query.
After refresh, my subroutine will close the excel file again, so other people of my team could access it without my holding up the file. Then, I would like to refresh all the pivot tables linked to the query. Since it links to password locked file, I don't want to refresh that query again. In one of your RefreshQueries suggestion stated such a code:
Sub RefreshQueries()
Dim ws As Worksheet
Dim qt As QueryTable
For Each ws In ThisWorkbook.Worksheets
For Each qt In ws.QueryTables
qt.Refresh
Next qt
Next ws
End Sub
You mentioned that I could write a similar subroutine to be called for updating Pivot Tables separately. Please advise how the other subroutine should be written.
Thanks!
You could use the following subroutine. It iterates over all the worksheets in the Workbook and then refreshes every pivotTable in the worksheet:
Sub RefreshPivotTables()
Dim ws As Worksheet
Dim pt As PivotTable
For Each ws In ThisWorkbook.Worksheets
For Each pt In ws.PivotTables
pt.PivotCache.Refresh
Next pt
Next ws
End Sub
Related
I have an Excel file that comprises a sheet "DATA", which contains data extracted using a Power Query, and three other sheets containing pivot tables that are all dependent on these extracted data in "DATA".
I wantto create a VBA that (1) automatically refreshes/reruns the Power Query specified above when the Excel file is opened and (2) subsequently updates the three pivot tables to mirror the updated data.
I have made several attempts and looked for similar questions on stackoverflow, but without succeeding:
While it was possible for me to refresh the data by refreshing the connection, the Pivot tables were not updated accordingly.
One VBA code that, among others, did not yield the desired results (i.e. subsequently updating the Pivot table) is:
Sub test()
Dim ws As Worksheet
Dim pt As PivotTable
'ActiveWorkbook.RefreshAll 'make sure the refresh in bg property is false for all connections
ActiveWorkbook.Connections("Name of Query").Refresh
For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
pt.RefreshTable
Next pt
Next ws
End Sub
Thank you!
You must be sure that "background refresh" is set to false:
Sub Refresh_All_Data_Connections()
Dim objConnection, bBackground
For Each objConnection In ThisWorkbook.Connections
'Get current background-refresh value
bBackground = objConnection.OLEDBConnection.BackgroundQuery
'Temporarily disable background-refresh
objConnection.OLEDBConnection.BackgroundQuery = False
'Refresh this connection
objConnection.refresh
'Set background-refresh value back to original value
objConnection.OLEDBConnection.BackgroundQuery = bBackground
Next
ThisWorkbook.RefreshAll
End Sub
Update: added refreshall to refresh all pivot tables.
Rereading your question if you just want to refresh a specific query (assuming sh is set):
sh.ListObjects("Name of Table").QueryTable.refresh BackgroundQuery:=False
ThisWorkbook.RefreshAll
I have three tables which pull data from SQL database.
When I click refresh all from the data menu, the connections refresh but only sum of the tables are refreshed.
When I right-click>refresh the tables themselves, they reflect the correct information (telling me that the connection was refreshed indeed).
I tried ActiveBook.RefreshAll and the following code too:
Dim wks As Worksheet
Dim qt As QueryTable
For Each wks In Worksheets
For Each qt In wks.QueryTables
qt.Refresh BackgroundQuery:=False
Next qt
Next wks
Set qt = Nothing
How can I refresh the table using VBA code?
You should access the connections through Workbook. Example,
Sub Test()
Dim con As WorkbookConnection
For Each con In ThisWorkbook.Connections
' Your Conditions, for example, check connection name etc.
' If con.Name = "MyConnection" Then
con.Refresh
' Else
' Do your thing
' End if
Next
End Sub
I have a small program VBA which is fact a userform which allow me to display all the existing worksheet of one open workbook on which I am working. Via this userform I can select another sheet and by clicking the sheet via this userform, it reorients me to the desired worksheet.
Now I tried to modifiy a bit of part of this program in order to do it the same but with all my open workbooks. It means if I have several workbook open, I would like that my userform allows me to display all the existing open workbook and by selecting the desired workbook via the userform, it reorients me to this workbook (it means that the selected workbook in the userform is activated and selected). The problem is when I run the code, I have an error message 424 VBA Run-time error '424' Object Required Error…
PS:really sorry for the format of my Code but I do not manage to put it in the right format..
Thanks in advance for your help
Xavi
Here please find the original code which works for userform related to worksheet (this one works):
Sub UserForm_Initialize()
Dim n As Long
Dim msg As String
Dim i As Long
Dim s As String
Dim sht As Worksheet
Do
n = n + 1
Me.ListBox1.AddItem Sheets(n).Name
Loop Until n = Worksheets.Count
End Sub
Here please find the modified code for userform related to workbook (this one does not works: run time error 424):
Sub UserForm_Initialize()
Dim n As Long
Dim msg As String
Dim i As Long
Dim s As String
Dim Wb As Workbook
Do
n = n + 1
Me.ListBox1.AddItem Workbooks(n).Name
Loop Until n = Worksbooks.Count
End Sub
If there is no reason for the actual number to be parsed in your code, then why not just loop the sheets directly?
Sub UserForm_Initialize()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
Me.ListBox1.AddItem ws.Name
Next ws
End Sub
May I propose a simple for loop ?
Dim i As Long
For i = 1 To Application.Workbooks.Count
Debug.Print Application.Workbooks(i).Name
Next
Then, if you have different instances of Excel (different Application objects then the one your Userform came from), this become a little bit more complex. (This is probably not the case if you are working in Excel 2010 or newer). But, if this is the case, it requires a couple of Win32 API calls and some insights on the "windows" of Excel. I've found my answers here in the past : Can VBA Reach Across Instances of Excel?
Situation
Trying to copy a range of cells that include formulas from worksheet called "Sheet1" to the rest of other worksheets I found that I could do it performing "Fill Across Worksheets". It worked fine, so my next step was to record a Macro for it to be more efficient and worked just fine too.
The Problem
The problem is that when I include a new worksheet and run the Macro, the Macro does not consider the new worksheet so this last worksheet doesn't get updated.
I am including below the code created by the macro. In it I can see that it's including only the worksheets I have now in the workbook, so this is where I need help.
( My excel is in Spanish so when you read Ctrl+Mayus+Q, Mayus means Shiftkey )
Help
What I need is a way to modify this Macro so when it runs it will check and update all worksheets. Or, maybe it's because a Macro can't do this I may need a VBA code ? If this VBA is the way to resolve it, can you help me here with this ?
I appreciate all help
Thank you
Javier
Sub Macro2()
'
' Macro2 Macro
'
' Acceso directo: Ctrl+Mayús+Q
'
Range("A5:D12").Select
Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")).Select
Sheets("Sheet1").Activate
ActiveWindow.SelectedSheets.FillAcrossSheets Range:=Selection,
Type:=xlAll
Sheets("Sheet1").Select
End Sub
This is a work around, concerning that you want all the worksheets to have the value of the first worksheet in range A5:D12:
Sub TestMe()
Dim ws As Worksheet
Dim selAddress As String
selAddress = "A5:D12"
For Each ws In Worksheets
'ws.Range(selAddress).Value2 = Worksheets(1).Range(selAddress).Value2
ws.Range(selAddress).Formula = Worksheets(1).Range(selAddress).Formula
Next ws
End Sub
See How to avoid using Select in Excel VBA.
Very novice user here, just getting my feet wet. Please excuse this if it's an idiotic question - I'm quite sure what I'm doing yet.
I'm creating a userform with VBA and Excel. I've been successful in learning how to pull information from a pivot table so far, but I've run into a hitch.
When run my userform from another sheet it can't find the pivot table. Here's the code I'm using.
Set PT = ActiveSheet.PivotTables(1)
Works perfectly when I'm one that sheet, but I envision a scenario where I have multiple pivots on different sheets and will want to call info and cross-reference the data.
I thought maybe Set PT = ActiveWorkbook.PivotTables(1) would work, but of course it doesn't. Obviously I don't yet quite understand how to use a pivot table variable.
Does anyone know how I might go about doing this? Thanks so much.
If you open UserForm, workbook with UserForm open will be the active one. If I understand correctly, you want to refer to 1st object of PivotTables collection on a different sheet. To do so, define path of the file with pivot table, example below:
Dim path as string
Dim wbk as workbook
Dim PT As PivotTable
path= "C:\folder1\folder2\yourfile.extension"
set wbk = workbooks.open(path)
Set PT = wbk.worksheets("Sheet1").PivotTables(1)
'do your actions, save the file if you want to keep changes
wbk.close
set wbk = nothing
Alternatively work on all Pivot Tables in your worksheet, instead of:
Set PT = wbk.worksheets("Sheet1").PivotTables(1)
You can use a loop through collection, this example refreshes tables:
For Each PT In wbk.worksheets("Sheet1").PivotTables
PT.RefreshTable
Next PT