VBA, mid replies with Run-time error 5 (Invalid procedure call or argument)

MoBo

New Member
Joined
Jan 2, 2013
Messages
11
I'm strugling with VBA code. I'm quite new at VBA programming. The code below returns an error at line with mid function Scraping from web page works OK (red bold text). I would like to get only part of the text.
Sample of data from web page looks like this:
firstXXXXsecond
Desired result of this VBA should return XXXX to cell C2 on sheet "All".
If I use only variable data without mid line it works Ok, but result is not the one i want.

I would appreciate any hint.

Sub GetData()

Dim IE As Object
Dim part_1 As Integer, len As integer
Dim data As String, data_trimed As String

Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://www.xy.com/a.php?ID_xy_200=1"
IE.Visible = True

While IE.busy
DoEvents
Wend

IE.document.All("input_name").Value = ThisWorkbook.Sheets("All").Range("B2")
IE.document.All("input_name").Select
IE.document.All("trigger").Select

data = IE.document.getElementById("output").innerHTML
part_1 = inStr(data, "first") + 5
len = inStr(data, "second") - inStr(data, "first") - 5
data_trimed = Mid(data, part_1, len)
ThisWorkbook.Sheets("All").Range("C2") = data_trimed

IE.Quit

End Sub
 
Here's all the code I use. If i comment mid function it works OK. Otherwise it stops there. I'm testing on sample where mylen is always > 0, otherwise it can return negative as well, and i have to fix that too.

sample of thata from web looks like:
HTML:
Koda OK (string i need)
Al results but ime2 are OK. Code works only if i comment line with ime2 = mid..
For error i got Run-time error 5 (Invalid procedure call or argument) on ime2 = mid line
Using a negative argument in the Mid function will cause an error.
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Using a negative argument in the Mid function will cause an error.

I'm so stupid. All the time negative length was the problem. Thank you for all your time.

I have additional question. How can I send keystroke TAB to IE at
IE.document.All("Code").Value = ThisWorkbook.Sheets("Vsi").Range("B5").Offset(i - 1)
 
Upvote 0
That's why I asked in post #2 what the value of len (or mylen) was when you get the error.:)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top