Re: Finding last possible variable for Element ID
Do you really need to get the 'last possible ID'?
If you loop through all elements looking for those named like 'detailControlTree_TestControl_rgTest_ctl00__9' ('9' represents any trailing number) then these can easily be written to a worksheet.
For example
Function Getid(ByRef Url As String, ByRef ElementID As String)
Dim IE As InternetExplorer
Dim doc As Variant
Dim element As Variant
Dim vValue As Variant
Set IE = New InternetExplorer
IE.Visible = True
IE.Navigate Url
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
Set doc = IE.Document
Dim i As Integer
i = 1
'// Add a new results sheet
ThisWorkbook.Sheets.Add
'// loop through all elements
For Each element In doc.all
'// Trim off the passed ElementID and convert result to number
'// This assumes the elementID (Example 'Thread_26560') will result in a number > 0
'// when the 'root' is removed. Other elements ('Thread_Title_26560') will be 0
vValue = Val(Replace(element.ID, ElementID, vbNullString))
'// If result > 0
If vValue > 0 Then
Cells(i, 2) = element.innerText
Cells(i, 1) = element.ID
i = i + 1
End If
Next
IE.Quit
End Function
Display More
Running that using
Getid "http://www.ozgrid.com/forum/forumdisplay.php?f=158", "Thread_"
to list all the thread titles on the VBA forum main page results in (Element text edited and shortened for clarity):
[table="class: grid"]
[tr][td][/td][td="bgcolor:#8E5A2E, align:left"]A[/td][td="bgcolor:#8E5A2E, align:left"]B[/td]
[/tr][tr][td="bgcolor:#ECF0F0, align:left"]1[/td][th]thread_200746[/th][th] Application Events
[/th]
[td="bgcolor:#ECF0F0, align:left"]2[/td][th]thread_200743[/th][th]Copy & Paste According to Reference
[/th]
[td="bgcolor:#ECF0F0, align:left"]3[/td][th]thread_200719[/th][th]list all possible combinations[/th]
[/tr][tr][td="bgcolor:#ECF0F0, align:left"]4[/td][th]thread_200739[/th][th]Runtime-Error 9, when other user has source sheet open
[/th]
[td="bgcolor:#ECF0F0, align:left"]5[/td][th]thread_200732[/th][th]Use a For loop and Find function to filter
[/th]
[td="bgcolor:#ECF0F0, align:left"]6[/td][th]thread_200741[/th][th]Finding last possible variable for Element ID
[/th]
[td="bgcolor:#ECF0F0, align:left"]7[/td][th]thread_200740[/th][th]Filling with Today's Date if Blank
[/th]
[td="bgcolor:#ECF0F0, align:left"]8[/td][th]thread_200730[/th][th]Automatically move to next sheet and perform Macro
[/th]
[td="bgcolor:#ECF0F0, align:left"]9[/td][th]thread_200737[/th][th]Update Master workbook automatically
[/th]
[td="bgcolor:#ECF0F0, align:left"]10[/td][th]thread_200684[/th][th]Upgrade existing macro to send data from multiple rows in one email
[/th]
[td="bgcolor:#ECF0F0, align:left"]11[/td][th]thread_200733[/th][th]Perform If... Then Statements and Arithmetic or Concatenation on Ranges
[/th]
[td="bgcolor:#ECF0F0, align:left"]12[/td][th]thread_200726[/th][th]Conditional Formatting with If Statements
[/th]
[td="bgcolor:#ECF0F0, align:left"]13[/td][th]thread_200729[/th][th]run macro after data validation
[/th]
[td="bgcolor:#ECF0F0, align:left"]14[/td][th]thread_200705[/th][th]Send Excel Document Using Lotus Notes
[/th]
[td="bgcolor:#ECF0F0, align:left"]15[/td][th]thread_200714[/th][th]Vbs copy cells into named range table
[/th]
[td="bgcolor:#ECF0F0, align:left"]16[/td][th]thread_200723[/th][th]combine multiple same name sheets into separate sheets
[/th]
[td="bgcolor:#ECF0F0, align:left"]17[/td][th]thread_200654[/th][th]Excel 2007 - 2010 VBA to interact / automate a commercial compiled database
[/th]
[td="bgcolor:#ECF0F0, align:left"]18[/td][th]thread_200728[/th][th]Command Button Combine Selected File Paths In ListBox To New Workbook Same Sheet
[/th]
[td="bgcolor:#ECF0F0, align:left"]19[/td][th]thread_200702[/th][th]Checking if sheet exists
[/th]
[td="bgcolor:#ECF0F0, align:left"]20[/td][th]thread_200708[/th][th]Insert number of rows based on cell value
[/th]
[/table]
The code is rough, could be improved and optimised, but illustrates the idea.