Reference cell values in Json.Document() - EXCEL Data sources - excel

I used the From Web function in EXCEL to load a a JSON document from the Web.
If I then Show Query, EXCEL has generated this call:
Json.Document(Web.Contents("http://blah.com/stuff"))
I'd like to edit that so rather than hard coding "http://blah.com/stuff" I'd like to pull the address from a cell in a WorkSheet.
Can anyone help me with what to put here?
Json.Document(Web.Contents(<<>>))

Give the spreadsheet cell with your URL a Name (using Name Manager) eg MyUrl
Insert a line above your Json.Document call (don't forget the comma at the end)
JsonURL = Excel.CurrentWorkbook(){[Name="MyUrl"]}[Content]{0}[Column1],
Then amend the next line to:
Json.Document(Web.Contents(JsonURL))

Related

Excel File doesn't show Hyperlink in protected view (Gembox and Kendo)

I have a problem with some excel files. I'have stored three excel files in the azure cloud storage. I've implemented a download option from my website. I'am opening the file via Gembox on the server to insert one link per row with the help of the hyperlink option of Gembox.
When opening the Excel-File for the first time, instead of showing the text of the hyperlink, excel shows the error value '#NAME?'. However, there is also an security warning, so it opens in an protected view.
#NAME? error
When I click the button to edit the worksheet, the text shows appropriate.
Texts shows approriate
Any ideas on how the text can be shown appropriate from the start?
Is that a HYPERLINK formula? That would explain this behavior.
To avoid this try using the ExcelCell.Hyperlink property. You can find an example of its usage on this Hyperlink example.
var workbook = new ExcelFile();
var worksheet = workbook.Worksheets.Add("Hyperlinks");
var hyperlinkStyle = workbook.Styles[BuiltInCellStyleName.Hyperlink];
var cell = worksheet.Cells["B1"];
cell.Value = "Link to GemBox homepage";
cell.Style = hyperlinkStyle;
cell.Hyperlink.Location = "https://www.gemboxsoftware.com";
cell.Hyperlink.IsExternal = true;
Or you can continue to use the HYPERLINK formula and to resolve this you would need to execute the ExcelFile.Calculate method before saving the ExcelFile.
Note, the latest version of GemBox.Spreadsheet has support for recalculating HYPERLINK function.

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.

Searching for DATA (VBA+Web)

I'm trying to create a macro that fetch for specific DATA in URL from Excel sheet and when the DATA is found , it copy the values and past it into the Excel File andmove on to the next DATA
I'm a little bit new with SCRAPPING WEB via EXCEL VBA so Anyone can light me and helps me to carry on ?
I made a lot of searches here in stack over flow but I didn't understand too much
I will explain below with Image what I want to Do and I will show My code which is nothing :(
First URL to use to access to MP :http://XXXX-XXXXX.eu.airbus.XXXX:XXXXX/XXXX/consultation/preSearchMP.do?clearBackList=true&CMH_NO_STORING_fromMenu=true
then Take a value from EXCEL FILE and put it in that Path to make search and tape enter
The last Picture IT will loop for MOD and Copy Value and Paste it into Excel File
Can Anyone guide me please with something
this is my first Code
Dim str As String: str = ThisWorkbook.Sheets("Feuil1").Range("A1").Value
Dim myURL as String
' The & symbol concatenates strings. The _ symbol is for line continuation.
myURL =
"http://Confidential.eu.airbus.Confidential:Confidential/Confidential/" _
& "consultation/preViewMP.do?" & str
If you have your URL then the next stage is to send it. To send it you need something to act as a connector, like Internet Explorer, or a background connector without the browser like XMLHttpServer objects - THIS is a good guide to hopefully get you started.

VBA Excel: How to edit links using user cell inputs?

I'm trying to make a macro that, among other things, updates external file links in several cells, where the file location currently in the cells and that which I want to change it to are specified by the user in another tab.
I've tried to do this via find/replace; if I try to do this with the text specified in the code:
Range("b3").Formula = Replace(Range("B3").Formula, "\\folder\file", "\\folder\newfile")
Then it'll replace this text and the links update correctly. If I change the locations with inputs, say oldlocation and newlocation:
oldlocation= "\\folder\file"
newlocation= "\\folder\newfile"
Range("b3").Formula = Replace(Range("B3").Formula, oldlocation, newlocation)
This also works ok. But if I change the definitions of the locations (e.g. B3: "\folder\file"):
oldlocation= Range("b3").text
newlocation= Range("b4").text
It no longer works - passes over the line in the code with no change or error. I've made a quick check and Range("b3").text & "\folder\file" both seem to be text strings; after that I'm stumped. I've tried a few different find/replace formats I've found, but all with the same result. What am I missing?
The problem is when you define the string to be replaced as
oldlocation= Range("b3").text
the oldlocation variable will have the value of the cell (the data you see in the cell) not the formula of the cell that contains the current reference, so the replace function does not find the string to be replaced.
You have to extract the location that you want to replace from the Range("b3").formula string and work with that.

Display Last Saved Date on worksheet

Does anyone know how to display the Last Saved Date of an Excel Spreadsheet on one of the worksheets?
I have found ways to do it using macros, but the spreadsheet is populated by an add-in called Jet Essentials, and this does not like macros so a solution here must not use one.
thought I would update on this.
Found out that adding to the VB Module behind the spreadsheet does not actually register as a Macro.
So here is the solution:
Press ALT + F11
Click Insert > Module
Paste the following into the window:
Code
Function LastSavedTimeStamp() As Date
LastSavedTimeStamp = ActiveWorkbook.BuiltinDocumentProperties("Last Save Time")
End Function
Save the module, close the editor and return to the worksheet.
Click in the Cell where the date is to be displayed and enter the following formula:
Code
=LastSavedTimeStamp()
This might be an alternative solution. Paste the following code into the new module:
Public Function ModDate()
ModDate =
Format(FileDateTime(ThisWorkbook.FullName), "m/d/yy h:n ampm")
End Function
Before saving your module, make sure to save your Excel file as Excel Macro-Enabled Workbook.
Paste the following code into the cell where you want to display the last modification time:
=ModDate()
I'd also like to recommend an alternative to Excel allowing you to add creation and last modification time easily. Feel free to check on RowShare and this article I wrote: https://www.rowshare.com/blog/en/2018/01/10/Displaying-Last-Modification-Time-in-Excel
May be this time stamp fit you better
Code
Function LastInputTimeStamp() As Date
LastInputTimeStamp = Now()
End Function
and each time you input data in defined cell (in my example below it is cell C36) you'll get a new constant time stamp.
As an example in Excel file may use this
=IF(C36>0,LastInputTimeStamp(),"")
You can also simple add the following into the Header or Footer of the Worksheet
Last Saved: &[Date] &[Time]
There is no built in function with this capability. The close would be to save the file in a folder named for the current date and use the =INFO("directory") function.

Resources