Working with VBA

Hi!
Help to write the macro, please.

Sub Test1()
Dim Link%, Cond%, Result%: Link = 5: Cond = 5
Result = Application.Sum(Link, Cond)
MsgBox Result
End Sub

Sub Test2()
' Temporarily make Async functions run synchronously
' https://seotoolsforexcel.com/working-with-vba/
Dim Link$, Cond$, Result$
Link = "https://twitter.com/fvirtanen"
Cond = "//p[@class='ProfileHeaderCard-bio u-dir']"
' Result = Application.XPathOnUrl(Link, Cond) '>> Run-time error '438'
Result = Evaluate("=XPathOnUrl(Link, Cond)") '>> Run-time error '13'
End Sub

Test1 is working, Test2 - Run-time error '438' or Run-time error '13'.
What am I doing wrong?

Several things you're missing, first, evaluate takes the whole expression and you need to manually concatenate the formula with the Link and Cond variables, including the quotes. Second, Evaluate accepts an array because XPathOnUrl returns an array. Try this code:

Sub Test2()

    Dim arrResult() As Variant
    
    Dim Link As String, Cond As String
    
    Link = """https://twitter.com/fvirtanen"""
    Cond = """//p[@class='ProfileHeaderCard-bio u-dir']"""

    arrResult = Evaluate("=XPathOnUrl(" & Link & "," & Cond & ")")
    
    MsgBox arrResult(1)

End Sub

diskborste, it works. Thank you!

diskborste, I learned how to programmatically connect and disconnect SEOTools, but there are problems with Function TEST.
What am I doing wrong?

Not sure, difficult to review the code at a glance. I recommend using breakpoints and stepping through the code while keeping an eye on the variables via the Locals window.

diskborste, I tried, but did not find anything informative when debugging...

You should be able to see which variable is not getting populated with a value from the formulas.

Also try this code to make it easier to catch bad requests:

        On Error Resume Next
        arrContent = Evaluate("formula")

        Do Until Application.CalculationState = xlDone
            DoEvents
        Loop

        If err.Number > 0 Then
            'error do stuff
        End If

Replace error do stuff to Debug.Print Err.Number and didn't catch anything...

If no error occurs then should be simple to save each request to a variable and see if it gets populated and with what.

diskborste, I'm not so good in VBA, need help from the community.

Try and change the requests to dummy values and you should be able to figure out where it goes wrong with stepping through the code.