show data refreshed time in excel - excel

I have an excel which has sql queries associated with it. I open the excel , click on Data->Refresh All and send across to the users.
When I click on refresh All , I want the refreshed date to appear in a cell on that sheet. Can you give some hint on this.

Add another SQL Query somewhere else in your workbook
SELECT GETDATE()
Make sure you refer to the same database your original query is referring to.
when you refresh the spread sheet the date will be updated as well.
Select Data ribbon
From Other Sources
From Microsoft Query
Choose DataBase (same as one used for the original Query)
"Query Wizard - Choose Columns" Click Cancel
"Do you want to continue editing the data in Query" Click Yes
"Add Tables" Click Close
Click on "SQL Button" and add the query there "SELECT GETDATE()"
Click OK
Click on Return data to Microsoft Excel

Rather than using the Ribbon to perform the Refresh, try a little macro like:
Sub Macro1()
ActiveWorkbook.RefreshAll
With Range("A1")
.Value = Now()
.NumberFormat = "mm/dd/yyyy hh:mm:ss"
End With
End Sub
Pick whatever cell you like.

after searching various Excel related sites (mrexcel.com, exceltown.com and answers.microsoft.com) i've decided that the best solution (for me) does NOT need Macro or another query:
add as a last column in your existing query whatever function provides the current date and time [for Ingres it's date('now')]
hide the column from the resulting table
in the cell that you desire to show the last date & time that your table was refreshed enter the formula max(j:j). substitute j with the column that you've hidden.
simple :)

This was the easiest way for me to get a refresh date. Add a =Date.Time.LocalNow() column. Hide Column. https://exceloffthegrid.com/power-query-get-refresh-date-time/

Related

PivotTable Not Refreshing With Data

I have an Excel document with 4 Worksheets.
On Sheet1 I set my start & end dates for my data.
On Sheet2 is the data itself (provided by an external SQL database).
On Sheet3 & Sheet4 are PivotTables that use the second sheets data as their source.
I have created a VBA macro button on Sheet1 to refresh all.
I'll change my date range on Sheet1 then click the button but it only refreshes the data on Sheet2 and not the PivotTables on Sheet3 & Sheet4. I have to click the button a second time to refresh the PivotTables with the new data.
Here is the VBA code:
Sub Button2_Click()
Application.ScreenUpdating = False
ActiveWorkbook.RefreshAll
Application.ScreenUpdating = True
End Sub
Even manually using the Refresh All button has the same outcome.
It seems when you do a refresh all it is trying to refresh everything in parallel, probably to save time. However, I think since you have pivot tables which are dependent on data which needs to refresh from the DB, you have to ensure that your external data is refreshed prior to attempting to refresh the pivot tables.
I was able to reproduce the issue and doing the below resolved it for me. Just have to replace "Query1" with the name of your query.
ActiveSheet.ListObjects("Query1").QueryTable.Refresh BackgroundQuery:=False
For Each pc In ActiveWorkbook.PivotCaches
pc.Refresh
Next
Edit:
To get the query name, you can go to the "Data" tab on your ribbon you should see a button that says "Queries & Connections" clicking on that will expand a pane on the right side of the excel screen. In that pane clicking on the Queries tab on the top will give you a list of your queries that are set up
Screenshot of my QueryTable name

Running a web query with parameter from a cell within Excel

I am trying to do a GET request when I change a value in a cell.
The link here:
https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=(PARAMETER SHOULD BE HERE)&hoursBeforeNow=1
So what's in this cell, should change this query.
Is this possible, and if so, does anyone have a good solution for this?
Been trying to google all night but can't seem to find anyone that has done the same thing.
Adapting from this answer, this is a two-step process.
Firstly, create a named range for the cell that stores the value you will pass to the query. Details on how to do so are here. I used "StationName" as the name of the range but you can use any name you like.
Secondly, we need to incorporate the named range into the source for the query. To do so:
select any cell in the data table from the current query
click on the Query tab in the ribbon
click on Edit
look for the Query Settings panel at the right of the screen - if it's not there, go to View then click on Query Settings
the first entry in the Query Settings panel should be Source - click on that entry to select it
make sure that the formula bar is displayed by going to View then checking Formula Bar
the entry in the formula bar is too long to display so click on the V symbol at the end of the formula bar to expand it
edit the formula bar text to the text shown below - make sure to change the name of the Excel range shown at Name="StationName" to whichever name you have used
= Xml.Tables(Web.Contents("https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=" & Excel.CurrentWorkbook(){[Name="StationName"]}[Content]{0}[Column1] & "&hoursBeforeNow=1"))
This picture shows how things should now look:
Now go to Home and click on Close & Load.
You may get a warning about privacy settings - to fix this, I chose to give both the Excel file and the aviationweather.gov site the "public" level of privacy.
You can access the privacy settings by selecting a cell in the data table, going to Query and choosing Edit, going to Home and choosing Data Source Settings, selecting Global Permissions and then using Edit Permissions on the relevant entries.
To make the query automatically refresh when the cell value changes, we need to use some VBA. We will need to know which sheet the cell is on, the name of the named range which refers to that cell and the name of the query we want to refresh. The first two should be straightforward and the name of the query is just "Query - " followed by the name shown in the Queries & Connections panel.
Open up the VBA Editor with ALT-F11, double-click on the relevant sheet object in the Project Explorer to open up the related code module and paste in the following code (but change the name of the named range and the name of the query to match your names):
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, ThisWorkbook.Names("StationName").RefersToRange) _
Is Nothing And Target.Cells.Count = 1 Then
Application.EnableEvents = False
ThisWorkbook.Connections("Query - Get station from aviationweather dot gov").Refresh
Application.EnableEvents = True
End If
End Sub
Now changing the cell value should cause the query to refresh automatically
Here's some simple code to list the name of every connection in the workbook. In the VBA editor, use Insert > Module to create a new module and then paste in the following code:
Option Explicit
Sub listConnections()
Dim c As WorkbookConnection
For Each c In ThisWorkbook.Connections
MsgBox c.Name
Next c
End Sub
Each connection name will pop up in a message box like this:
and you can just copy the name you need when it pops up. You can copy the message box contents just by hitting CTRL-C but you'll need to edit out some extra stuff when you paste. Copying the message box shown above results in the following text being placed on the clipboard:
---------------------------
Microsoft Excel
---------------------------
Query - Get station from aviationweather dot gov
---------------------------
OK
---------------------------
I get daily Exchange Rate data from cbr.ru in this fashion.
screenshot of the Connection
http://www.cbr.ru/eng/currency_base/daily/?UniDbQuery.Posted=True&UniDbQuery.To=["Ticker"]
Where i use the date as a variable from an excel cell, in such a way that it refreshes automatically once the cell value changes.
(named the parameter as Ticker)
screenshot of the Parameters
Hope this uber simple method helps.

Macro button to copy link in pivot table based on Slicer

I'm starting with a Pivot Table that has all my data. One of the data fields is a URL string that I ultimately want to send my users to.
To make searching through the pivot table as easy as possible, I've got a couple slicer windows to let the users click their search criteria. What I'd like to do, is based upon their search criteria (once narrowed down to one result), is have a Macro Button that automatically launches their browser (lets say internet explorer) with the link found in the pivot table.
I can figure out the launching of the browser (or just a copy text), but is there a way that I can program the Macro Button's action based upon the selections of the Slicers that are associated with my pivot table?
Any help would be much appreciated!
If I understood you correctly, you want to launch your code when only one link appears in PivotTable (after you apply slicers to it).
You can achieve this with Worksheet_PivotTableChangeSync event and helper cell.
lets assume you have your pivot table in column G, starting from cell G2. Enter this this formula =COUNTA(G3:G20) in cell G1(adjust range to your needs).
then in your worksheet module (not regular module) enter this code.
Private Sub Worksheet_PivotTableChangeSync(ByVal Target As PivotTable)
Dim a As Long
a = Range("G1").Value
If a = 1 Then
'your code for link in cell G3
Else
End If
End Sub
Now when you slice your PivotTable to only one selection, COUNTA formula will evaluate to 1 and trigger Worksheet_PivotTableChangeSync event.

Excel powerpivot, show when the data was updated?

I have an Excel sheet with powerpivot tables and charts. I want to show when the data was updated.
I use VBA code and a button to update the data, and I use the function now() in a cell which is updated when the code is running.
I have two problems.
When I open the Excel file the date is updated even if I do not update the data on start. (I do not want the data to update on start)
When the update fails the date is still updated.
My VBA code look like this:
Range("table name").Select
ActiveSheet.PivotTables("PivotTable2").PivotCache.refresh
Range("date cell").Select
How to avoid update when the data is not updated?
Instead of solve it as user3819867 suggested, I solved it by adding the information from the database, which is a SQL server database so I added a view with one colum and one row (with a getdate()) and send it along with the other tables so I get one extra table named "date".
How about its event?
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Range("date cell").Select
End Sub

how to run a macro while clicking on a value in slicer in excel vba

I have a Slicer in my excel with the following 2 values
Add Customer
Add Vendor
I want to open 2 different forms when click on those value. Can anyone please help me with this.
Yes, you can call different forms on slicer clicks, check the below code. Hope it suits your requirement and if not you can use to change as per your requirement.
On Writing your code in Worksheet_PivotTableUpdate event, on each slicer click this event will get called.
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
If ActiveWorkbook.SlicerCaches("Slicer_Region").SlicerItems("East").Selected = True Then
UserForm1.Show
ElseIf ActiveWorkbook.SlicerCaches("Slicer_Region").SlicerItems("West").Selected = True Then
UserForm2.Show
End If
End Sub
Where,
Slicer_Region = "Name to use in formulas"
You can get this name by right click on slicer and click slicer settings. and find the name to use in formula.
East or West = "Names of slicers"
For which you want to call different forms.
The solution works BUT THE CODE MUST BE in the code module of the worksheet with the pivot table
It does not work on main slicer, however if you select any particular item it works. In that case it is better to set the macro on the slicer itselft. You just need to click on slicer and it will be run automatically.

Resources