Macro to Change Dates and Generate Spreadsheet on Website - excel

There is a website I routinely use to generate a spreadsheet. The only items on the website are a "Start Date" field, an "End Date" field, and a "Go" button. After I enter my date range and click "Go" it downloads a .cfm file, I click open with excel, excel warns the file has a different extension and verifies it's not corrupted and I click open and I have the data I need and from there have a macro to maniupulate as needed.
I'm looking to automate the steps:
Go to website
Change Start Date
Change End Date
Click Go
Click Open file
Agree to open different extension
The macro I've used before to get data from a website only copies and pastes data visible on a specific url and is as follows. I manipulate the url on my Input spreadsheet to manipulate the data.
Dim addWS As Worksheet
Set addWS = Sheets.Add(Before:=Sheets("Input"))
addWS.Name = "Website Data"
Dim myurl As String
myurl = Worksheets("Input").Range("G4")
With Worksheets("Website Data").QueryTables.Add(Connection:= _
"URL;" & myurl, _
Destination:=Range("A3"))
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With
Thank you.

The following code works for me. You will have to change "startDate" and "endDate" depending on how the specific website names the input boxes.
Sub test_fetch()
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
Dim i As Long
Dim Doc As Object, lastrow As Long, tblTR As Object
Set IE = CreateObject("InternetExplorer.application")
IE.Visible = True
IE.navigate "http://your_website"
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Application.StatusBar = "Fetching Website Data. Please wait..."
Set objCollection = IE.document.getElementsByTagName("input")
i = 0
While i < objCollection.Length
If objCollection(i).Name = "startDate" Then
' Set text for start date
objCollection(i).Value = "09/15/2013"
ElseIf objCollection(i).Name = "endDate" Then
' Set text for end date
objCollection(i).Value = "09/21/2013"
Else
If objCollection(i).Type = "submit" And _
objCollection(i).Name = "" Then
' "Search" button is found
Set objElement = objCollection(i)
End If
End If
i = i + 1
Wend
objElement.Click ' click button to search
End Sub

Related

HTML Text with tags to formatted text in an Excel cell with buttons for multiple rows

I was working to preview the html code in excel and this reference works well
Re: HTML Text with tags to formatted text in an Excel cell
However, I am now trying to build a button for every single row (sort of like mail merge) so the aim is when I click on the button according to what row, the preview will show me the html code for that row.
For example, if I click on row 5 (A5) then I will see the html code for row 5 (A5).
The VBA code that I'm using is:
Sub HTMLPreview()
Dim Ie As Object, RowNumber As Integer
Set Ie = CreateObject("InternetExplorer.Application")
With Ie
.Visible = True
.Navigate "about:blank"
.document.body.InnerHTML = Sheets("Sheet1").Range("A1").Value
'.document.body.createtextrange.execCommand "Copy"
'ActiveSheet.Paste Destination:=Sheets("Sheet1").Range("A1")
'.Quit
End With
End Sub
Can someone please help?
Screen shot for reference.
enter image description here
Thank you heaps in advance
Try this - it will not open a new window for every button click, but tries to re-use a previously-opened IE.
Assign all your buttons to ShowHTML
Sub ShowHTML()
Dim Ie As Object, html
Set Ie = GetIE("about:blank") 'try to get already-open window
If Ie Is Nothing Then
Set Ie = CreateObject("InternetExplorer.Application") 'open a new IE
Ie.Visible = True
Ie.Navigate "about:blank"
End If
'Application.Caller is the name os the clicked-on button
html = ActiveSheet.Shapes(Application.Caller).TopLeftCell.EntireRow.Cells(1).Value
Ie.document.body.InnerHTML = html
End Sub
'Find an open IE window which has a matching URL
Function GetIE(sLocation As String) As Object
Dim o As Object, sURL As String, retVal As Object
For Each o In CreateObject("Shell.Application").Windows
sURL = ""
sURL = o.LocationURL
If sURL Like sLocation & "*" Then
Set retVal = o
'Exit For
End If
Next o
Set GetIE = retVal
End Function

Excel web automation: invalid inputs

I am trying to write code to upload data into web form at work. If I copy and paste the details the form accepts the values, but If the VBA code enters the same exact values then I get input error.
Screen script of form is below
This Excel Vba code taken from StackOverflow searches and youtube "https://www.youtube.com/watch?v=T6HRjiAdW38&list=PLBGMKJfLuhqa3q0RWyiN40NV24rxTJ0Yt&index=1"
Sub Ihub_upload()
Dim appIE As InternetExplorerMedium
Dim objElement As Object
Dim objCollection As Object
Dim sURL As String
Set appIE = New InternetExplorerMedium
sURL = "company web site "
With appIE
.navigate sURL
.Visible = True
End With
Do While appIE.Busy
Application.Wait DateAdd("s", 2, Now)
Loop
':::: Enter data collection ::::'
' I will put code below into a loop to enter multiple values if it works'
'Data collecton'
appIE.document.getElementById("CPH_MasterBody_CPH_EtlMasterBody_DDL_DataCollection").Value = 120
'File location tab 16'
appIE.document.getElementById("CPH_MasterBody_CPH_EtlMasterBody_TXT_SourceConnectionString").Value = "Y:\Databases\2019-Kv9xQ.xls"
'start date tab 18 '
appIE.document.getElementById("CPH_MasterBody_CPH_EtlMasterBody_TXT_PeriodStartDateTime").Value = "01/09/2019"
'End date tab 19'
appIE.document.getElementById("CPH_MasterBody_CPH_EtlMasterBody_TXT_PeriodEndDateTime").Value = "30/09/2019"
'publication date tab 20 '
If ThisWorkbook.Sheets("Data").Range("I3").Value <> "" Then
appIE.document.getElementById("CPH_MasterBody_CPH_EtlMasterBody_TXT_PublicationDateTime").Value = "14/11/2019"
End If
'Source URL tab 21 '
If ThisWorkbook.Sheets("Data").Range("I3").Value <> "" Then
appIE.document.getElementById("CPH_MasterBody_CPH_EtlMasterBody_TXT_SourceUrl").Value = "https://www.england.nhs.uk"
End If
':::: Save record? click ok button ::::'
Application.Wait DateAdd("s", 3, Now)
appIE.document.getElementById("CPH_MasterBody_CPH_EtlMasterBody_BTN_Save").Click

how to pass information to url via excel vba

I want to use excel vba to control Internet explorer object.
Manually we can setup in "https://www.investing.com/indices/us-30-historical-data"
Time Frame i.e. "Daily"
Start Date i.e. "01/01/2016"
End Date i.e "31/12/2016"
How to do this with excel vba ?
I've this code to open the URL
InfiniteLoop = True
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.Navigate myURLLink ' should work for any URL
SleepTime = 10000
Do
DoEvents
Sleep (SleepTime)
If ie.ReadyState >= 4 Then
Exit Do
End If
Loop Until InfiniteLoop = False
End With
But I don't know how to pass
Time Frame i.e. "Daily"
Start Date i.e. "01/01/2016"
Start Date i.e. "01/01/2016"
Can anyone help ?
I have came up with this simple macro (Excel 2010) to navigate to the sit and change the dates and data interval, hopefully it will help:
Sub test()
Dim IE As InternetExplorer
Dim IEdoc As Object
Dim IEElement As Object
Set IE = New InternetExplorer 'initialize Internet Explorer
IE.Navigate "https://www.investing.com/indices/us-30-historical-data" 'Navigate to URL
IE.Visible = True
Set IEdoc = IE.Document
Set IEElement = IEdoc.GEtelementbyID("picker") ' navigate to date input
IEElement.Value = "01/01/2016 - 01/01/2017"
Set IEElement = IEdoc.GEtelementbyID("widget") ' navigate to date widget
IEElement.Click
Set IEElement = IEdoc.GEtelementbyID("applyBtn") ' navigate to Apply button to save the dates
IEElement.Click
Set IEElement = IEdoc.GEtelementbyID("data_interval") ' navigate to Interval picker
IEElement.Value = "Daily"
End Sub

Copy dropdown table from internet VBA

I am trying to copy a table from this website: http://www.nzfma.org/data/search.aspx#
I need to select the date as yesterday's date, then copy and paste the table to a file.
My code is below:
Sub Test1()
'open IE, navigate to the website of interest and loop until fully loaded
Dim NZFMA As Worksheet
Dim TodayN As Range
Dim elemCollection As Object
Set NZFMA = Sheets("NZFMA")
Set TodayN = NZFMA.Range("B2")
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = True
.navigate "http://www.nzfma.org/data/search.aspx"
Do Until Not ie.Busy And ie.ReadyState = 4
DoEvents
Loop
'Select the dates from the drop-down box
ie.Document.getElementbyid("ctl00_cphBody_rdpDate_dateInput").Value = Format(TodayN, "yyyy-mm-dd")
'Click the submit button
ie.Document.getElementbyid("cphBody_btnSearch").Value = "Search"
'Copy the results
Set elemCollection = ie.Document.getElementbyid("cphBody_upResults")
While ie.ReadyState = 4
DoEvents
Wend
End With
End Sub
For some reason, my macro stops after the first getElementbyID line. Can someone advise as to which part of the code is wrong?
Alright, haven't tested this but something like it should work to change the first dropdown box
Dim elemCollection As HTMLFormElement
set elemCollection = ie.Document.getElementbyid("cphBody_lstMarket")
dim teststring as string
teststring = whatever
Select Case TestString
Case "BKBM"
elemCollection.selectedIndex = 1
Case "BBRS030"
elemCollection.selectedIndex = 2
etc etc
To change the date, you will need something like
ie.Document.getElementbyid("ctl00_cphBody_rdpDate_dateInput")(0).value = "05/07/2016"
Anyway, have a go with that, and please tell me the results

VBA Set Dropdown value

I have a macro that I have written in excel and I have navigated to a webpage using "ActiveWorkbook.FollowHyperlink", which works just as I need it!
However, I now need to update a dropdown menu on that webpage.
I have an ID for the dropdown and each selection obviously has a value. I want to set the selected option using the value, which I have in the excel sheet.
I am struggling because I don't know how to access elements on the page, once opened using .FollowHyperlink.
After .FollowHyperlink is the webpage then active, is there something like ActiveWebPage.getElementById?
Appreciate any help.
Mike
what you want to do is use com automation to call an instance of internet explorer and navigate to the page in question, this will give you the document model, from there you can do most anything, see IE (Internet Explorer) Automation using Excel VBA
Sample VBA follows
Private Sub IE_Autiomation()
Dim i As Long
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
' Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.Navigate "http://www.excely.com/"
' Statusbar
Application.StatusBar = "www.excely.com is loading. Please wait..."
' Wait while IE loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
' Find 2 input tags:
' 1. Text field
' <input type="text" class="textfield" name="s" size="24" value="" />
'
' 2. Button
' <input type="submit" class="button" value="" />
Application.StatusBar = "Search form submission. Please wait..."
Set objCollection = IE.document.getElementsByTagName("input")
i = 0
While i < objCollection.Length
If objCollection(i).Name = "s" Then
' Set text for search
objCollection(i).Value = "excel vba"
Else
If objCollection(i).Type = "submit" And _
objCollection(i).Name = "" Then
' "Search" button is found
Set objElement = objCollection(i)
End If
End If
i = i + 1
Wend
objElement.Click ' click button to search
' Wait while IE re-loading...
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
' Show IE
IE.Visible = True
' Clean up
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
Application.StatusBar = ""
End Sub

Resources