Excel:VBA: How to make script dynamic? - html

I have the following script:
Sub Test1()
Dim IE As Object
Set IE = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
IE.Visible = True
IE.navigate "https://share.amazon.com/sites/IPV/Lists/IPV%20Appeals%20tracker/NewForm.aspx?Source=https%3A%2F%2Fshare%2Eamazon%2Ecom%2Fsites%2FIPV%2FLists%2FIPV%2520Appeals%2520tracker%2FAllItems%2Easpx&RootFolder="
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
IE.document.getElementById("Title_fa564e0f-0c70-4ab9-b863-0177e6ddd247_$TextField").Value = ThisWorkbook.Sheets("sheet1").Range("a1")
IE.document.getElementById("Seller_x0020_ID_2b32b5fa-ace8-44d5-bba5-5c0e321671ed_$TextField").Value = ThisWorkbook.Sheets("sheet1").Range("b1")
IE.document.getElementById("Seller_bba5-5c0e321671ed_$TextField").Value = ThisWorkbook.Sheets("sheet1").Range("c1")
IE.document.getElementById("Seller_bba5-5c0e321671ed_$TextField").Value = ThisWorkbook.Sheets("sheet1").Range("d1")
IE.document.getElementById("Seller_bba5-5c0e321671ed_$TextField").Value = ThisWorkbook.Sheets("sheet1").Range("e1")
End Sub
This script is suppose to copy the values in the following row range in excel and autofill a website form.
The problem with this is that i have applied it for just the first row, but I wanted a way to be able to select which row to apply this to.
I hope my explanation makes sense.

Concatenate in row number e.g.
Dim rowNumber As Long
rowNumber = 2
IE.document.getElementById("Title_fa564e0f-0c70-4ab9-b863-0177e6ddd247_$TextField").Value = ThisWorkbook.Sheets("sheet1").Range("A" & rowNumber)
If using in a loop then you would concatenate in the loop variable and make the id also a variable.
Same principle for your other items.
You could also pick the row number up from a cell
IE.document.getElementById("Title_fa564e0f-0c70-4ab9-b863-0177e6ddd247_$TextField").Value = ThisWorkbook.Sheets("sheet1").Range("A" & ActiveSheet.Range("A1").value)
Avoid ActiveSheet and use the actual sheet name however.

Related

VBA: script to extract from a HTML table?

Apologies for the basic question but I've only been coding about a week!
To give some context I have a basic task where I have to manually search and type in a number to retrieve a persons qualifications and expiry dates which could be multiple per person. I've managed to write a script that searches each person in turn from a list of qualification numbers I have on an excel spreadsheet from this site:
https://www.bindt.org/Certification/pcn-certificate-verification/
My script then currently clicks the more information next to each qualification to expand the data (unsure if this is required in all honesty).
My problem is I have no idea how to extract the data back into excel, I've not managed to extract it by using class names, grabbing the whole table etc.
Would it be possible to get some help with writing the code to grab the data?
For an example if you use the number 213474 on thee site it will give you an idea of what I need to grab.
Many thanks in advance!
Tom
Edit 8/8/21
Hi Raymond,
I've put my code below so far, it all falls down at:
Set Table = IE.document.getElementByClassName("container")(1)
I do have another issue where when I go to the second person in the loop as they all have different qualification numbers the go to error 10 doesn't seem to work.
My main issue is no matter what I try to grab off the results be it tbody, tr etc I can't seem to find any combination that works.
Sub PCN()
Dim IE As Object
Dim lastrow As Integer
ThisWorkbook.Sheets("PCN").Range("A1").Select
lastrow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
Dim i As Integer
Dim rng As Range
Set rng = Application.Range("c2:C" & lastrow)
For i = 2 To lastrow
Debug.Print rng.Cells(RowIndex:=i, ColumnIndex:="c").Value
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://www.bindt.org/Certification/pcn-certificate-verification/pcn-verification-form"
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
IE.document.getElementById("txtNumber").Value = ThisWorkbook.Sheets("PCN").Range("C" & i).Value
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
IE.document.getElementById("submit").Click
On Error GoTo 10
IE.document.getElementById("certificate-link-1").Click
IE.document.getElementById("certificate-link-2").Click
IE.document.getElementById("certificate-link-3").Click
IE.document.getElementById("certificate-link-4").Click
IE.document.getElementById("certificate-link-5").Click
IE.document.getElementById("certificate-link-6").Click
IE.document.getElementById("certificate-link-7").Click
IE.document.getElementById("certificate-link-8").Click
IE.document.getElementById("certificate-link-9").Click
IE.document.getElementById("certificate-link-10").Click
IE.document.getElementById("certificate-link-11").Click
IE.document.getElementById("certificate-link-12").Click
IE.document.getElementById("certificate-link-13").Click
IE.document.getElementById("certificate-link-14").Click
IE.document.getElementById("certificate-link-15").Click
IE.document.getElementById("certificate-link-16").Click
IE.document.getElementById("certificate-link-17").Click
IE.document.getElementById("certificate-link-18").Click
IE.document.getElementById("certificate-link-19").Click
IE.document.getElementById("certificate-link-20").Click
IE.document.getElementById("certificate-link-21").Click
IE.document.getElementById("certificate-link-22").Click
10
Set Table = IE.document.getElementByClassName("container")(1)
Set tbody = Table.getElementsByTagName("tbody")(0)
trs = tbody.getElementsByTagName("tr")
Do While trs Is Nothing
DoEvents
Application.Wait Now + TimeValue("00:00:02")
trs = tbody.getElementsByTagName("tr")
Loop
Dim myvalue As String: myvalue = trs.Cells(3).innerText
IE.Quit
Next
End Sub

Filling in HTML inputs with VBA. result has inputs filled in but treats it like its empty

Bit of an odd problem I have encountered. I am filling in a website for my work (cannot share) in order to speed up some searches for accounts. I have managed to fill in what I need but it still treats it as if the boxes are empty even though they are now filled with the data I selected.
One workaround I am currently using is tabbing through everything, for some reason that works but I really do not want to resort to using sendkeys.
I have tried changing the aEle.innerText to value and placeholder but not have worked.
Here is the code I am using, it's pretty messy right now, the html I am using does not use any IDs so i had to loop through each tag. any input would be appreciated. Thanks
Dim fullAcct, acct, branch, daterange1, daterange2 As String
Dim objIE As InternetExplorer
Dim aEle As HTMLLinkElement
Dim y, branchInput, accountInput, dateInput As Integer 'integer variables used as counters
'split selected account# into strings
fullAcct = selection
branch = Left(fullAcct, 3)
acct = Mid(fullAcct, 5, 6)
daterange1 = Date
daterange2 = DateAdd("yyyy", -25, daterange1) 'max date range
Set objIE = New InternetExplorer
objIE.Visible = True
'navigate IE to this web page
objIE.Navigate "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.com"
Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
'set the counter for the for each loop and indicators of what to update
y = 1
branchInput = 2 'brach input
accountInput = 3
dateInput = 5
'for each <a> element in the collection of objects with a tag name "input"
For Each aEle In objIE.Document.getElementsByTagName("input")
'**tried using .value and .placerholder but innerText seemed to work the best
If y = branchInput Then 'check for second input (branch)
aEle.innerText = branch
End If
If y = accountInput Then 'check for third input (account#)
aEle.innerText = acct
End If
If y = dateInput Then 'check for date input
aEle.innerText = daterange2
End If
y = y + 1
Next
SendKeys "{TAB 10}" 'must be tabbed through for some reason or the data isnt read
Application.Wait (Now + TimeValue("0:00:01"))
End Sub

How to get a particular link from a web page's source code?

i need a macro in VBA that is able to extract all HTML source code from an url contained in a EXCEL cell and put it line by line in all different Excel cells.
I've previously searched different solutions on the net but not finding the right one.
Thanks for helping ;)
EDIT:
thanks to the libraries just insert i could also test another macro that i've previously found on the net:
Sub Naviga()
Dim texto As String
Dim objIE As Object
Dim DestUrl As String
DestUrl = "http://www.google.it"
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = False
objIE.Navigate2 DestUrl
Do
DoEvents
Loop Until objIE.ReadyState = READYSTATE_COMPLETE
Range("A" & 1).Value = objIE.document.body.innerHTML
End Sub
and it's works, but unfortunately i would like that the link was acquired directly from a cell in excel, and when the line is copied, the next line, start with the next cell, the cell below.
How can i modify the macro?
EDIT 2:
The solution is near, i've just fixed the code, now is more clean:
Sub EstrSorgPag()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.navigate Range("H1")
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Range("A" & 1).Value = IE.document.body.innerHTML
End Sub
but lacks the last part where the macro should copy the content cell by cell (A1,A2,A3,A4... and so on)
EDIT 3:
Hello guys, i wrote this short code that extract all links from a web page's source code:
Sub EstraiURLdaWeb()
Dim doc As HTMLDocument
Dim output As Object
Set IE = New InternetExplorer
IE.Visible = False
IE.navigate Range("L1")
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Set doc = IE.document
Set output = doc.getElementsByTagName("a")
i = 5
For Each link In output
Range("A" & i).Value = link
i = i + 1
Next
MsgBox "Fatto!"
End Sub
But i would need to extract this in particular:
<li class="bubble"><span>Main</span></li>
how can I do?
Verify the <a>'s InnerHTML or InnerText
If you already got all the <a> tag elements, you can loop through them all (you got this already) and create a logical condition, if each particular element contains the keyword you are looking for.
Set output = doc.getElementsByTagName("a")
For Each link In output
If link.InnerHTML = "Main" Then
Range("A" & i).Value2 = link
End If
Next
Combine more GetElement(s) methods
To get more narrow collection of HTML elements, you can combine multiple GetElement(s) methods. Like so:
You can get all the HTML elemens with specific class:
Set BubbleCollection = doc.getElementsByClassName("bubble")
Then you can scan this collection for <a> tags:
Set output = BubbleCollection.getElementsByTagName("a")
Check how many elements you've got (optional for debugging/refining the search):
Debug.Print output.length

Copying columns of values from Excel worksheet to an HTML table in a website

I'm hoping to copy values from an excel table into an html table. If anyone has experience doing this between Excel 2013 and Internet Explorer 11, please let me know and I can give details. Thank you
The following is a little section of the code for the website that I am trying to automate the fill-in process for:
Table and Source Code for Charge Numbers for the Site
This is showing just one part of the process (the charge numbers), which we have anywhere from 10-25 each week with their corresponding hours charged to them each day (as seen in the table matrix).
There are 7 charge numbers on each page and Charge Numbers all have name="chargeNo" and increase id names with each box downward like so:
'1) id="chargeNo0"
'2) id="chargeNo1"
'3) id="chargeNo2"
'4) id="chargeNo3"
'5) id="chargeNo4"
'6) id="chargeNo5"
'7) id="chargeNo6"
Each day of the week also has set names as follows:
Table and Source Code for Hours Charged Each Day
'Saturday: name="hrs0"
'Sunday: name="hrs1"
'Monday: name="hrs2"
'Tuesday: name="hrs3"
'Wed: name="hrs4"
'Thurs: name="hrs5"
'Fri: name="hrs6"
I'm trying to automate the process of pulling the arrays of charge times I have from Excel and inputting them into their respective textboxes.
This script will convert elements in ColumnA into an array.
Sub MakeArray()
Dim arr As Variant
With ActiveSheet
arr = WorksheetFunction.Transpose(.Range(.[A1], .Cells(Rows.Count, "A").End(xlUp)))
End With
End Sub
Sub MakeString()
Dim s As String
Const DELIMITER = ","
With ActiveSheet
s = Join(WorksheetFunction.Transpose(.Range(.[A1], .Cells(Rows.Count, "A").End(xlUp))), DELIMITER)
End With
End Sub
If you want to pass the array to an HTML TextBox, try this.
Sub CreateArrayAndPassToHTMLTextbox()
Dim ie As Object
Dim arr As Variant
Const DELIMITER = ","
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://home.arcor.de/peter.schleif/SearchForTDelement.html"
While ie.Busy: DoEvents: Wend
With ActiveSheet
arr = WorksheetFunction.Transpose(.Range(.[A1], .Cells(Rows.Count, "A").End(xlUp)))
If Not IsArray(arr) Then arr = Array(arr)
ie.document.getElementById("trackField").Value = Join(arr, DELIMITER)
End With
End Sub
If, for instance, you want to pass data to a ComboBox, try this.
Sub passValueToComboBox1()
Dim ie As Object
Dim oHTML_Element As IHTMLElement
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://peterschleif.bplaced.net/excel/combobox/index.php"
While ie.Busy Or ie.readyState <> 4: DoEvents: Wend
Set oHTML_Element = ie.document.getElementsByName("selectedReportClass")(0)
If Not oHTML_Element Is Nothing Then oHTML_Element.Value = "com.db.moap.report.FUBU7"
For Each oHTML_Element In ie.document.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
End Sub

I need help inputting a column from excel in VBA with html macro.

I selected 2 cells and named them "perch" and "stats". using what i inputted in the cell, i was able to run a search and retrieve the data i wanted from the html code(which i changed for safety). But my problem is want to input an entire column not just a single cell. And produce multiple results in a single column. Also the number of inputs will change periodically ~300, so i can't stipulate a specific range.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = Range("perch").Row And _
Target.Column = Range("perch").Column Then
Dim IE As New InternetExplorer
'IE.Visible = True
IE.navigate "http://"google/not_real_link"
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Dim Doc As HTMLDocument
Set Doc = IE.document
Dim sTD As String
sTD = Trim(Doc.getElementsByTagName("td")(33).innerText)
IE.Quit
Range("stats").Value = sTD
End If
End Sub
You'll usually see the Worksheet_SelectionChange event used instaed of the Worksheet_Change.
Here are some tips on using these events:
Turn off EnableEvents. If you don't you might re-trigger the event and get stuck in an infinite loop
Use the Intersect method to test to see if your range and the Target intersect.
You should use Target.Cells.Count to see if multiple cells are being effected.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("perch")) Is Nothing Then
Application.EnableEvents = False
Application.EnableEvents = True
End If
End Sub
I'm not really sure how you want to handle multiple values.
Creating a function to add value(s) to the end of a list often helps.
Sub AddStatusRow(value As Variant)
Const StatColumn = 5
Dim lastRow As Long
lastRow = Range(Cells(1, StatColumn), Cells(Rows.count, StatColumn)).End(xlUp).Row + 1
Cells(lastRow, StatColumn) = value
End Sub
You might want to consider iterating over the rows of the table instead of Doc.getElementsByTagName("td")(33).innerText.
You can find lots of great examples of what you want to do from the URL below.
http://www.tushar-mehta.com/publish_train/xl_vba_cases/vba_web_pages_services/index.htm