I have written a code in VBA which fetch data from API( API data stored in the database) and showed it in an excel sheet.
below is my code:
Sub Auto_Open()
Dim sJSONString As String
Dim vJSON As Variant
Dim sState As String
Dim vElement As Variant
Dim sValue As String
Dim aData()
Dim aHeader()
' Retrieve JSON string
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "http://localhost:3030/node/express/get/apidata", False
.Send
sJSONString = .responseText
End With
' Parse JSON
JSON.Parse sJSONString, vJSON, sState
If sState = "Error" Then MsgBox "Invalid JSON string": Exit Sub
' Output the entire table to the worksheet
JSON.ToArray vJSON, aData, aHeader
With Sheets(1)
.Cells.Delete
.Cells.WrapText = False
OutputArray .Cells(3, 1), aHeader
Output2DArray .Cells(4, 1), aData
.Columns.AutoFit
End With
' for hyperlinks
' Worksheets("Sheet1").Select
' Range("L2:L1048576").Select
' ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:=I.Value, TextToDisplay:=I.Value
' Dim sh As Worksheet, I As Range, rng As Range
'Set sh = ActiveSheet
'With sh
' Set rng = .Range("L2:L" & .Cells(.Rows.Count, "L").End(xlUp).Row)
' For Each I In rng.Cells
' .Hyperlinks.Add Anchor:=Sheets("Sheet1").Range("L2:L"), Address:=I.Value, SubAddress:=I.Value, TextToDisplay:=I.Value
'Next
'End With
'auto refresh code
Application.OnTime Now + TimeValue("00:30:00"), "Auto_Open"
End Sub
Sub OutputArray(oDstRng As Range, aCells As Variant)
With oDstRng
.Parent.Select
With.Resize(1, UBound(aCells) - LBound(aCells) + 1)
.NumberFormat = "@"
.Value = aCells
End With
End With
End Sub
Sub Output2DArray(oDstRng As Range, aCells As Variant)
With oDstRng
.Parent.Select
With .Resize( _
UBound(aCells, 1) - LBound(aCells, 1) + 1, _
UBound(aCells, 2) - LBound(aCells, 2) + 1)
.NumberFormat = "@"
.Value = aCells
End With
End With
End Sub
Now I want to show " L " column data in hyperlink format whenever the user clicks on "L" column data file automatically downloaded.
I have used the below code but this code covert entire sheet data to hyperlink:
'Converts each text hyperlink selected into a working hyperlink
For Each xCell In the Selection
ActiveSheet.Hyperlinks.Add Anchor:=xCell, Address:=xCell.Formula
Next xCell
Please help me, guys. Thanks