Copy dropdown table from internet VBA - excel

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

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

loop through rows of data to submit web form

Sub AutoLoadAccounts()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://www.urlhere.com/admin/accounts/create"
IE.Visible = True
While IE.busy
DoEvents
Wend
IE.Document.All("title").Value = ThisWorkbook.Sheets("sheet1").Range("a1")
IE.Document.All("names").Value = ThisWorkbook.Sheets("sheet1").Range("b1")
IE.Document.All("floor").Value = 30
IE.Document.getElementById("status").selectedindex = 1
IE.Document.getElementById("email_state").selectedindex = 1
IE.Document.All("id").Value = ThisWorkbook.Sheets("sheet1").Range("c1")
IE.Document.All("years").Value = ThisWorkbook.Sheets("sheet1").Range("d1")
IE.Document.All("submit").Click
End Sub
The above code I use to populate a web form and submit it. I have around 150 rows of data ranging from A1:D1. I am trying to find a way to loop through the rows 1 by 1 after submitting the form until it reaches the end.
So essentially it will start on the first row and populate the fields from A1:D1, then once complete go down to the next row and do the same for A2:D2. and so on
The trick here is to organise your source data. Using two columns you can record the field name and the required value:
A B
1 Title Sample Title
2 Names Sample Names
3 Floor Sample Floor
To loop:
Sub AutoLoadAccounts()
Dim IE As Object
Dim cRow As Range ' Current row, used to extract values from Excel.
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://www.urlhere.com/admin/accounts/create"
IE.Visible = True
While IE.busy
DoEvents
Wend
' Executes once for each row in the source range.
For Each cRow In ThisWorkbook.Sheets("sheet1").Range("A1:A3")
' Read field name and value from current row.
IE.Document.All(cRow.Value).Value = cRow.Offset(0, 1)
Next
IE.Document.All("submit").Click
End Sub
This code could be improved. At the moment the source range is hard coded (Range("A1:A3")). You could improve this, so the code automatically identifies all completed rows in Excel. If you are interested research the worksheets UsedRange object.
EDIT
Added example that reads source data from columns, not rows.
Sub AutoLoadAccounts_Columns()
Dim IE As Object
Dim cRow As Range ' Current row, used to extract values from Excel.
Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://www.urlhere.com/admin/accounts/create"
IE.Visible = True
While IE.busy
DoEvents
Wend
' Executes once for each row in the source range.
For Each cRow In ThisWorkbook.Sheets("sheet1").Range("A1:C1")
' Read field name and value from current row.
IE.Document.All(cRow.Value).Value = cRow.Offset(1, 0).Value
Next
IE.Document.All("submit").Click
End Sub

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

Looping through a row and copying each cell In a specific procedure

What I have to do is use Excel VBA to:
login to Amazon Seller
open a workbook
loop through a column to get an order number
put it in the search box
hit the search button
go to the order page and extract the data
then have the extracted data go back into a specified column in
another Excel workbook
The loop and order number parts are what I'm currently stumped on. I've figured out this much code as of this moment:
Sub MyAmazonSeller()
Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
Dim oSignInLink As HTMLLinkElement
Dim oInputEmail As HTMLInputElement
Dim oInputPassword As HTMLInputElement
Dim oInputSigninButton As HTMLInputButtonElement
'InputSearchOrder will be the destination for order numbers taken from the workbook
Dim InputSearchOrder As HTMLInputElement
Dim InputSearchButton As HTMLInputButtonElement
Dim IE As InternetExplorer
Dim AAOrder As Workbook
Dim AAws As Worksheet
MyURL = "https://sellercentral.amazon.com/gp/homepage.html"
Set IE = New InternetExplorer
' Open the browser and navigate.
With IE
.Silent = True
.Navigate MyURL
.Visible = True
Do
DoEvents
Loop Until .ReadyState = READYSTATE_COMPLETE
End With
' Get the html document.
Set HTMLDoc = IE.Document
' See if you have the sign in link is because you are in the main
' page
Set oSignInLink = HTMLDoc.getElementById("signin-button-container")
If Not oSignInLink Is Nothing Then
oSignInLink.Click
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
End If
' Get the email field and the next button
Set oInputEmail = HTMLDoc.getElementById("username")
Set oInputPassword = HTMLDoc.getElementById("password")
' Click the button and wait
oInputEmail.Value = "xxxxxx#xxxxxx.net"
' Get the password field and the sign in button
Set oInputPassword = HTMLDoc.getElementById("password")
Set oInputSigninButton = HTMLDoc.getElementById("sign-in-button")
' Click the button and wait
oInputPassword.Value = "xxxxxxxx"
oInputSigninButton.Click
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
Application.Wait (Now + TimeValue("0:00:05"))
Set AAOrder = Application.Workbooks.Open("Z:\Employee Folders\Employee\trackingnumber_sample_spreadsheet.xls")
Set AAws = AAws.Worksheets("PrimeOrdersWithNoFulfillmentRe")
Set InputSearchOrder = HTMLDoc.getElementById("sc-search-field")
'What I'm currently stuck on
InputSearchOrder.Value = "001-7163923-7572632"
Set InputSearchButton = HTMLDoc.getElementsByClassName("sc-search-button")(0)
InputSearchButton.Click
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
'Was able to add this snippet, but I'm getting an error 13, most likely with
'my e variable. I'm basically trying to do a loop within a loop, extracting 5
'pieces of data and sticking them back into their respective columns in the
'original Excel sheet. The problem comes when scraping the HTML. I'm basically
'trying to get text in the tables which have a few levels and it's frustrating
'me to no end.
With HTMLDoc
Set elems = HTMLDoc.getElementsByTagName("td")
For Each e In elems
If e.innerText Like "*1Z*" Then
Range("D2").Value = e.innerText
End If
Next e
End With
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub

I want to extract table form website using excel vba by entering certain date and submitting data . I want Table of that date

'I have tried this, this dose not contain the code for extracting table.
I need to do these 4 things using vba macro.
1.enter the website
2.enter the date
3.Search the price form the website
4.Return the table to the excel worksheet form the website
Sub ClickButton()
Dim ie As Object
Dim form As Variant, button As Variant
Dim MyHTML_Element As IHTMLElement
Set ie = CreateObject("InternetExplorer.Application")
dateinput = InputBox("Enter date of for searching table eg. 2015-01-19")
With ie
.Visible = True
.navigate ("http://www.sharesansar.com/today.php#")
While ie.ReadyState <> 4
DoEvents
Wend
ie.document.getElementsbyname("date").Item.innertext = dateinput
Set form = ie.document.getElementsByTagName("form")
Set Buttoon = form(0).onsubmit
form(0).submit
End With
Set ie = Nothing
End Sub
Typo:
Set Button = form(0).onsubmit
ought to be a button.

Resources