VBA not recognising numbers

AndrewD04

New Member
Joined
Aug 24, 2017
Messages
40
Office Version
  1. 365
Good Afternoon,

i am using this code to search anything listed in Column A which seems to work fine however if i write a number and not text it breaks can anyone hel.

Sub XMLHTTP1()

Dim url As String, i As Long, lastRow As Long
Dim XMLHTTP As Object, html As New HTMLDocument, objResultDiv As HTMLAnchorElement


lastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastRow

url = "https://www.google.co.in/search?q=" & Cells(i, 1) & "&rnd=" & WorksheetFunction.RandBetween(1, 10000)

Set XMLHTTP = CreateObject("MSXML2.serverXMLHTTP")
XMLHTTP.Open "GET", url, False
XMLHTTP.setRequestHeader "Content-Type", "text/xml"
XMLHTTP.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"
XMLHTTP.send

Set html = New HTMLDocument
html.body.innerHTML = XMLHTTP.ResponseText
Set objResultDiv = html.querySelector("div#rso h3.r a")

Cells(i, 2) = objResultDiv.innerText
Cells(i, 3) = objResultDiv.href

DoEvents
Next

End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi,
Try to add the following if condition and if true proceed the rest of the code but if false take next i.
Let me know if that helps.

Code:
For i=2 to lastRow
If not(IsNumeric(Cells(i, 1)))=true then ' proceed the rest of the code
End if
Next i
 
Upvote 0
Hi,
Try to add the following if condition and if true proceed the rest of the code but if false take next i.
Let me know if that helps.

Code:
For i=2 to lastRow
If not(IsNumeric(Cells(i, 1)))=true then ' proceed the rest of the code
End if
Next i

Didn't seem to work however I have found a way around it so will update the code later for reference it seems to work tested a few times,
 
Upvote 0
Didn't seem to work however I have found a way around it so will update the code later for reference it seems to work tested a few times,

Hi,
The function IsNumeric checks whether the value is a number and if so (ex. 123 or "123") returns true. Not statement up front the isnumeric function stands for return true if the value in cell is string and false for numbers. Therefore in my opinion it should work to skip numbers as long as they are provided in cell as mentioned above. It will not work if the value in text is a combination of text and numbers "Abs_123". I suggest you upload an example of your file or advise what kind of data you process with thecode vba code of yours. It'd help to help you out.

Regards,
Sebastian
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
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