vba getelelementbyclassname() - vba

I'm beginner
I am studying about web scraping but, I have some problem i can't solve by myself.
I need your soultion.
I'm finding it that how to input getelementbytagname(a) in getelementbyclassname() with for function
like this:
For i = 0 To 23
Cells(3 + i, 5) = doc.getElementsByClassName("name").getElementsByTagName("a")(i).innerText
Cells(3 + i, 5).WrapText = False
Next i
'=========================================================
'above code
'this was error
what is problem?
help me.
thanks a lot
I'm waiting your answer

Related

How to return SAP error message in excel cell through VBA?

I wrote an automation script that uses the following SAP GUI:
objSess.findById("wnd[0]").Maximize
objSess.findById("wnd[0]/tbar[0]/okcd").Text = "flqaf"
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]").sendVKey 17
objSess.findById("wnd[1]/usr/cntlALV_CONTAINER_1/shellcont/shell").CurrentCellColumn = "TEXT"
objSess.findById("wnd[1]/usr/cntlALV_CONTAINER_1/shellcont/shell").SelectedRows = "0"
objSess.findById("wnd[1]/usr/cntlALV_CONTAINER_1/shellcont/shell").DoubleClickCurrentCell
objSess.findById("wnd[0]/usr/ctxtS_BELNR-LOW").Text = W_BPNumber
objSess.findById("wnd[0]/usr/ctxtS_BELNR-LOW").SetFocus
objSess.findById("wnd[0]/usr/ctxtS_BELNR-LOW").caretPosition = 10
objSess.findById("wnd[0]").sendVKey 8
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/tbar[0]/btn[3]").press
Everything works fine, however sometimes the problem aborts in SAP and exactly when that happens I want to capture it by writing the term "error" into an excel cell.
I tried adding this line in vba
objSheet.Cells(iRow, 5) = GuiStatusbar.Text
As well as
objSheet.Cells(iRow, 5) = objSessFindById("wnd[0]/sbar").Text
The code still runs fine but my cells in column 5 remain empty. Any ideas how to solve this?
It might help if you wait a little.
fo example:
...
objSess.findById("wnd[0]").sendVKey 0
waitTill = Now() + TimeValue("00:00:01")
While Now() < waitTill
DoEvents
Wend
if objSess.findById("wnd[0]/sbar").messageType = "E" then 'other types: W, I, S, A
objSheet.Cells(iRow, 5) = objSessFindById("wnd[0]/sbar").Text
end if
objSess.findById("wnd[0]/tbar[0]/btn[3]").press
...
Regards, ScriptMan

add names of two variables and make a new dynamic variable

I am writing a code where I have a for loop in which I give a variable (named VType ) some value. For loop goes for a range of i variables. Now I want to make a new variable by concatenating names of both variables. for example if i = 1 then I want to make variable VType1. Here is my piece of code.
nrec = Split(Split(ie.document.body.innerHTML, "Found <strong>")(1), "</strong> records")(0)
If nrec = 1 Then
lnk.Click
Else
For j = 1 To nrec
link.Click
Do While ie.readyState <> 4: Wait 5: Loop
Application.Wait (Now + TimeValue("0:00:01"))
'VType , j = GetType
'Application.Wait (Now + TimeValue("0:00:01"))
IMO , j = GetValue("IMO:")
'MMSI = GetValue("MMSI:")
YBuilt , j = GetValue("Year Built:")
Flag , j = GetValue("Flag:")
DWT , j = GetValue("Deadweight:")
Next j
num = "1 - " & IMO1
For i = 2 To nrec
num = num & vbCrLf & i & "abc"
Next I
fin = InputBox(num, nrec & " records found for a. please select right one.")
Exit For
End If
There is not a way to directly do what you're specifically requesting. However, you can use arrays to get a similar outcome. Arrays are a not a topic that can be explained in a single posted answer, but if you do a little research you can probably figure out how the below might be useful...
Dim VTtyp(0 to i) as string
'while Looping...
Vtype(i) = "Whatever you want stored in this round of i"
When your code completes, you'll have all fields saved as variables that can be called from this array. An example is if you wanted to call the one that was tied to the number "2" you could type: Vtype(2) and it would call the text from the 2 iteration.
Again this example is extremely simplified and there are things to consider such as dim size, changing the dim, preserving the array, etc. and that is something you'll have to research further. However bottom line is, "there is not a way to do what you're specifically trying to do."
You can achieve this using Dictionary objects concept. Go through the below link to know more about dictionary objects.
https://www.tutorialspoint.com/vbscript/vbscript_dictionary_objects.htm

Overflow.Exception crashing my program

I know this is how it's supposed to be done however, in VB.NET it gives me an overflow exception crashing my program. If there is any "VB.NET equivalent" of doing this, help is much appreciated.
Dim hActiveWeapon = _mem.rdInt(GetLocalBase() + &H2EE8)
Dim wepEntity = _mem.rdInt(_client + Offsets.EntityList + ((hActiveWeapon & &HFFF) - 1) * &H10) ' Crashes here # hActiveWeapon & &HFFF
Dim wepIndex = _mem.rdInt(wepEntity + Offsets.iItemDefinitionIndex)
I am doing this to get the lower 12 bytes of m_hActiveWeapon (0xFFF = 0b111111111111 = 4095), so I can get the index of the active weapon.
Meh the answer was simple, changing "&" to "And" fixed it.

VBA Excel Transform to loop

Here i have my hundred line of code please enlighten me about how do i put line code into loop
here is my try but i wont work out still trying
If sp.Name Like "Rounded Rectangle*" Or sp.Name Like "Oval*" Then
For i = 11 To 100
x = i - 9
Sheet2.Shapes.Range(Array("Rounded Rectangle " + i)).TextFrame.Characters.Text = Sheet1.Range("A" + x)
Next i
End If
and repeat until X = 110
in this case how can i change it in to correct loop please advice
thank you
This is a general approach to making a loop to cover a string variable....say we want to loop over Shape("Rectangle 1")....Shape("Rectangle 2")....Shape("Rectangle 3)..... , etc.
Dim str As String, i As Long
For i = 11 To 100
str = "Rectangle " & CStr(i)
Sheets2.Shapes(str)................
Next i
and use a similar approach to make "A2"..."A3".........
simple math:
For i = 11 To 100
change to
For i = 11 To 119

sub declaration visual Basic

I am really new with vba, so please forgive my slaughtering of the code. I'm trying to write a macro for excel (my first one), and I get "statement invalid outside of type block" (pointing at the first line). Here is my code:
Sub MakeHTMLTable()
Worksheets("Sheet1").Activate
endRow As Integer
For Count = 1 To 200
For CountY = 1 To 200
If (!ActiveSheet.Cells(Count, CountY).Value.IsEmpty) Then
ActiveSheet.Cells(Count, CountY).Value = "<td>" + ActiveSheet.Cells(Count, CountY).Value + "</td>"
End If
Exit For
Exit For
For i = 1 To 200
If (!ActiveSheet.Cells(i, 1).Value.IsEmpty()) Then
ActiveSheet.Cells(i, 1).Value = "<tr>" + ActiveSheet.Cells(i, 1)
End If
Exit For
For x = 1 To 200
If (!ActiveSheet.Cells(x, 1).Value.IsEmpty()) Then
endRow = x
End If
Exit For
For countAgain = 1 To 200
If (!ActiveSheet.Cells(x, countAgain).Value.IsEmpty()) Then
ActiveSheet.Cells(x, countAgain).Value = ActiveSheet.Cells(x, countAgain).Value + "</tr>"
End If
Exit For
End Sub
I really don't understand, as the debugger fails on the line of computer generated code without even making it to mine. Have I missed ending an If" or For block?
I also realize that I'm probably reinventing the wheel. Any help on more appropriate built in functions would be appreciated.
It looks like you've got quite a few syntax errors in your code.
In VBA, local variables are declared with the Dim keyword.
So, the endRow declaration should look like this:
Dim endRow As Integer
For loops should end with the Next statement. So, your For loops should look like this:
For x = 1 To 200
If (!ActiveSheet.Cells(x, 1).Value.IsEmpty()) Then
endRow = x
End If
Next
VBA uses the keyword Not instead of !, so, your conditionals should look like this:
If (Not (ActiveSheet.Cells(i, 1).Value.IsEmpty())) ...
Try removing most of the code and adding it back in line-by-line until all of it works. The VBA syntax can be cumbersome for those who aren't used to it.