Hello Everyone. This is my first time posting, but I hope someone can help because I am going batty.
I have attempted to use VBA to get Zip +4 zip codes from usps website. Everything was going fine with sample data, but when I copied new data into the table, I start getting a runtime 5 error: Invalid procedure call or argument.
Here is my data
[TABLE="width: 671"]
<colgroup><col><col><col><col span="2"><col></colgroup><tbody>[TR]
[TD]Address[/TD]
[TD]City[/TD]
[TD]State[/TD]
[TD]Zipcode[/TD]
[TD]Zip[/TD]
[TD]Zip4[/TD]
[/TR]
[TR]
[TD="align: right"]1642[/TD]
[TD]Harmon+Street[/TD]
[TD]Berkeley[/TD]
[TD]ca[/TD]
[TD]94703[/TD]
[TD]2636[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD]Peabody+Terrace[/TD]
[TD]Cambridge[/TD]
[TD]ma[/TD]
[TD]02138[/TD]
[TD]6211[/TD]
[/TR]
[TR]
[TD="align: right"]1600[/TD]
[TD]Pennsylvania+Ave[/TD]
[TD]Washington[/TD]
[TD]DC[/TD]
[TD]20500[/TD]
[TD]0003[/TD]
[/TR]
[TR]
[TD="align: right"]1280[/TD]
[TD]N+Frontage+Rd+W[/TD]
[TD]Vail[/TD]
[TD]CO[/TD]
[TD]81657[/TD]
[TD]4457[/TD]
[/TR]
[TR]
[TD="align: right"]350[/TD]
[TD]5th+Ave[/TD]
[TD]New+York[/TD]
[TD]NY[/TD]
[TD]10118[/TD]
[TD]0110[/TD]
[/TR]
[TR]
[TD="align: right"]3[/TD]
[TD]Christine Dr[/TD]
[TD]Barrington [/TD]
[TD]RI[/TD]
[TD]02806[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]2100[/TD]
[TD]Channing+Way[/TD]
[TD]Berkeley[/TD]
[TD]CA[/TD]
[TD]94720[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1642[/TD]
[TD]Harmon+Street[/TD]
[TD]Berkeley[/TD]
[TD]ca[/TD]
[TD]94703[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD]Peabody+Terrace[/TD]
[TD]Cambridge[/TD]
[TD]ma[/TD]
[TD]02138[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1600[/TD]
[TD]Pennsylvania+Ave[/TD]
[TD]Washington[/TD]
[TD]DC[/TD]
[TD]20500[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1280[/TD]
[TD]N+Frontage+Rd+W[/TD]
[TD]Vail[/TD]
[TD]CO[/TD]
[TD]81657[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]350[/TD]
[TD]5th+Ave[/TD]
[TD]New+York[/TD]
[TD]NY[/TD]
[TD]10118[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1111[/TD]
[TD]S+Figueroa+St[/TD]
[TD]Los+Angeles[/TD]
[TD]CA[/TD]
[TD]90015[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]2100[/TD]
[TD]Channing+Way[/TD]
[TD]Berkeley[/TD]
[TD]CA[/TD]
[TD]94720[/TD]
[TD]
[/TD]
[/TR]
</tbody>[/TABLE]
My VBA code keeps stopping at the the 6th entry (3 Christine Dr Barrington RI 02806)
Sub ZipLookUp()
Dim URL As String, xmlHTTP As Object, html As Object, htmlResponse As String
Dim SStr As String, EStr As String, EndS As Integer, StartS As Integer
Dim Zip4Digit As String
Dim number As String
Dim address As String
Dim city As String
Dim state As String
Dim zipcode As String
Dim abc As String
Dim cell As Range
For Each cell In Range("C2:C15")
number = Sheet1.Range("A" & cell.Row)
address = Sheet1.Range("B" & cell.Row)
city = Sheet1.Range("C" & cell.Row)
state = Sheet1.Range("D" & cell.Row)
zipcode = Sheet1.Range("E" & cell.Row)
URL = "https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=1&companyName=&address1="
URL = URL & number & "+" & address & "&address2=&city=" & city & "&state=" & state & "&urbanCode=&postalCode=&zip=" & zipcode
Set xmlHTTP = CreateObject("MSXML2.XMLHTTP")
xmlHTTP.Open "GET", URL, False
On Error GoTo NoConnect
xmlHTTP.send
On Error GoTo 0
Set html = CreateObject("htmlfile")
htmlResponse = xmlHTTP.responseText
If htmlResponse = Null Then
MsgBox ("Aborted - HTML response was null")
GoTo End_Prog
End If
SStr = "<span class=""zip4"">": EStr = "</span><br />" 'Searches for a string within 2 strings
StartS = InStr(1, htmlResponse, SStr, vbTextCompare) + Len(SStr)
EndS = InStr(StartS, htmlResponse, EStr, vbTextCompare)
Zip4Digit = Left(Mid(htmlResponse, StartS, EndS - StartS), 4)
Sheet1.Range("F" & cell.Row).Value = Zip4Digit
GoTo End_Prog
NoConnect:
If Err = -2147467259 Or Err = -2146697211 Then MsgBox "Error - No Connection": GoTo End_Prog 'MsgBox Err & ": " & Error(Err)
End_Prog:
Next cell
the VBA runs as expected until I reach this entry, and then upon hitting the Sheet1 line, I get the arrow.
Can Someone help me please?
I have attempted to use VBA to get Zip +4 zip codes from usps website. Everything was going fine with sample data, but when I copied new data into the table, I start getting a runtime 5 error: Invalid procedure call or argument.
Here is my data
[TABLE="width: 671"]
<colgroup><col><col><col><col span="2"><col></colgroup><tbody>[TR]
[TD]Address[/TD]
[TD]City[/TD]
[TD]State[/TD]
[TD]Zipcode[/TD]
[TD]Zip[/TD]
[TD]Zip4[/TD]
[/TR]
[TR]
[TD="align: right"]1642[/TD]
[TD]Harmon+Street[/TD]
[TD]Berkeley[/TD]
[TD]ca[/TD]
[TD]94703[/TD]
[TD]2636[/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD]Peabody+Terrace[/TD]
[TD]Cambridge[/TD]
[TD]ma[/TD]
[TD]02138[/TD]
[TD]6211[/TD]
[/TR]
[TR]
[TD="align: right"]1600[/TD]
[TD]Pennsylvania+Ave[/TD]
[TD]Washington[/TD]
[TD]DC[/TD]
[TD]20500[/TD]
[TD]0003[/TD]
[/TR]
[TR]
[TD="align: right"]1280[/TD]
[TD]N+Frontage+Rd+W[/TD]
[TD]Vail[/TD]
[TD]CO[/TD]
[TD]81657[/TD]
[TD]4457[/TD]
[/TR]
[TR]
[TD="align: right"]350[/TD]
[TD]5th+Ave[/TD]
[TD]New+York[/TD]
[TD]NY[/TD]
[TD]10118[/TD]
[TD]0110[/TD]
[/TR]
[TR]
[TD="align: right"]3[/TD]
[TD]Christine Dr[/TD]
[TD]Barrington [/TD]
[TD]RI[/TD]
[TD]02806[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]2100[/TD]
[TD]Channing+Way[/TD]
[TD]Berkeley[/TD]
[TD]CA[/TD]
[TD]94720[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1642[/TD]
[TD]Harmon+Street[/TD]
[TD]Berkeley[/TD]
[TD]ca[/TD]
[TD]94703[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]2[/TD]
[TD]Peabody+Terrace[/TD]
[TD]Cambridge[/TD]
[TD]ma[/TD]
[TD]02138[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1600[/TD]
[TD]Pennsylvania+Ave[/TD]
[TD]Washington[/TD]
[TD]DC[/TD]
[TD]20500[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1280[/TD]
[TD]N+Frontage+Rd+W[/TD]
[TD]Vail[/TD]
[TD]CO[/TD]
[TD]81657[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]350[/TD]
[TD]5th+Ave[/TD]
[TD]New+York[/TD]
[TD]NY[/TD]
[TD]10118[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]1111[/TD]
[TD]S+Figueroa+St[/TD]
[TD]Los+Angeles[/TD]
[TD]CA[/TD]
[TD]90015[/TD]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]2100[/TD]
[TD]Channing+Way[/TD]
[TD]Berkeley[/TD]
[TD]CA[/TD]
[TD]94720[/TD]
[TD]
[/TD]
[/TR]
</tbody>[/TABLE]
My VBA code keeps stopping at the the 6th entry (3 Christine Dr Barrington RI 02806)
Sub ZipLookUp()
Dim URL As String, xmlHTTP As Object, html As Object, htmlResponse As String
Dim SStr As String, EStr As String, EndS As Integer, StartS As Integer
Dim Zip4Digit As String
Dim number As String
Dim address As String
Dim city As String
Dim state As String
Dim zipcode As String
Dim abc As String
Dim cell As Range
For Each cell In Range("C2:C15")
number = Sheet1.Range("A" & cell.Row)
address = Sheet1.Range("B" & cell.Row)
city = Sheet1.Range("C" & cell.Row)
state = Sheet1.Range("D" & cell.Row)
zipcode = Sheet1.Range("E" & cell.Row)
URL = "https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=1&companyName=&address1="
URL = URL & number & "+" & address & "&address2=&city=" & city & "&state=" & state & "&urbanCode=&postalCode=&zip=" & zipcode
Set xmlHTTP = CreateObject("MSXML2.XMLHTTP")
xmlHTTP.Open "GET", URL, False
On Error GoTo NoConnect
xmlHTTP.send
On Error GoTo 0
Set html = CreateObject("htmlfile")
htmlResponse = xmlHTTP.responseText
If htmlResponse = Null Then
MsgBox ("Aborted - HTML response was null")
GoTo End_Prog
End If
SStr = "<span class=""zip4"">": EStr = "</span><br />" 'Searches for a string within 2 strings
StartS = InStr(1, htmlResponse, SStr, vbTextCompare) + Len(SStr)
EndS = InStr(StartS, htmlResponse, EStr, vbTextCompare)
Zip4Digit = Left(Mid(htmlResponse, StartS, EndS - StartS), 4)
Sheet1.Range("F" & cell.Row).Value = Zip4Digit
GoTo End_Prog
NoConnect:
If Err = -2147467259 Or Err = -2146697211 Then MsgBox "Error - No Connection": GoTo End_Prog 'MsgBox Err & ": " & Error(Err)
End_Prog:
Next cell
the VBA runs as expected until I reach this entry, and then upon hitting the Sheet1 line, I get the arrow.
Can Someone help me please?