I don't know what I am doing wrong....

rlink_23

Board Regular
Joined
Oct 30, 2015
Messages
149
I have used a code like this in a different part of my userform, To transfer info from a sheet to the userform. I have a list of Zipcodes in a sheet called ("Zips") in Column A is the actual Zip code, the State is in Column B, and City is Column C. What I am trying to do is when the Zip code is entered into TextBox ("Zipcode") It populates TextBox ("City") and TextBox ("State"). I have used a very similar code Elsewhere in my userform but this code WILL NOT work at all.. I know it is trying as it runs super super solw when I am typing in the TextBox ("ZipCode"). I think I have used Dim I As long before so maybe that is cousiung an issue.. idk.. Please help!!! :) Maybe I am just missing something and am not noticing the smallest detail...


Code:
Private Sub ZipCode_Change()Dim I As Long, Lastrow As Long
Lastrow = Sheets("Zips").Range("A" & Rows.Count).End(xlUp).Row
For I = 2 To Lastrow


If Sheets("Zips").Cells(I, "A").Value = ZipCode Or _
Sheets("Zips").Cells(I, "A").Value = Val("ZipCode") Then


Me.State = Sheets("Zips").Cells(I, "B").Value
Me.City = Sheets("Zips").Cells(I, "C").Value






End If


Next






End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Instead of looping through, why not use match? Much more efficient.
Code:
if iserror(application.match(Zipcode, sheets("Zips"),0))=false then
matched = application.match(Zipcode, sheets("Zips"),0)
Me.State = Sheets("Zips").Cells(matched, "B").Value
Me.City = Sheets("Zips").Cells(matched, "C").Value
else
msgbox Zipcode & " not found in Zips list.", vbcritical, "ALERT"
endif
 
Upvote 0
Thank you Roderick!! I will give that a shot! Stand by and I will let you know if it works!! :)
 
Upvote 0
Another one...

Code:
[color=darkblue]Private[/color] [color=darkblue]Sub[/color] ZipCode_Change()
    [color=darkblue]Dim[/color] v [color=darkblue]As[/color] [color=darkblue]Variant[/color]
    [color=darkblue]If[/color] Len(ZipCode.Text) >= 2 [color=darkblue]Then[/color]
        [color=darkblue]With[/color] Sheets("Zips")
            v = Application.Match(ZipCode.Text & "*", .Range("A:A"), 0)
            [color=darkblue]If[/color] IsNumeric(v) [color=darkblue]Then[/color]
                Me.State = .Cells(v, "B").Value
                [color=darkblue]If[/color] Len(ZipCode.Text) >= 5 [color=darkblue]Then[/color]
                    Me.City = .Cells(v, "C").Value
                [color=darkblue]Else[/color]
                    Me.City = ""
                [color=darkblue]End[/color] [color=darkblue]If[/color]
            [color=darkblue]Else[/color]
                Me.State = ""
                Me.City = ""
            [color=darkblue]End[/color] [color=darkblue]If[/color]
        [color=darkblue]End[/color] [color=darkblue]With[/color]
    [color=darkblue]Else[/color]
        Me.State = ""
        Me.City = ""
    [color=darkblue]End[/color] [color=darkblue]If[/color]
End [color=darkblue]Sub[/color]
 
Upvote 0
I get a Run-Time error "1004

Application-defined or object defined error
on this line
If IsError(Application.Match(ZipCode, Sheets("Zips"), 0)) = False Then
 
Upvote 0
oops forgot the full range
If IsError(Application.Match(ZipCode, Sheets("Zips").range("A:A"), 0)) = False Then

Alphafrog's solution is more robot and complete. Might try his.
 
Upvote 0
Ok Roderick, I put the first number of the Zipcode in and instantly I get the msgbox popping up. Id does not even give me a chance to add the whole thing.. lol
 
Upvote 0
Try using an AfterUpdate rather than a change event
Code:
Private Sub ZipCode_AfterUpdate()

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,780
Messages
6,121,522
Members
449,037
Latest member
tmmotairi

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