Lookup for postcode module error

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,259
Office Version
  1. 2010
Platform
  1. Windows
Hi I hopeyou can help, I am trying to make a post code lookup, so for example inTextBox51 you type in your postcode like ‘WR5 3’ then automatically it updates textbox52 with the area of the postcode for example ‘Worcester’. It gets the data from my sheet called ‘PostCode’and looks at row A for the Postcode and row B is the area, I have made a module below called ‘GetData’ but it is not working, and In the sub for textbox51 I have put in GetData for it to goto the module.

My codingis below hope you can help.
Code:
Option Explicit
Code:
[FONT=Calibri][SIZE=3][COLOR=#000000]Dim id As String, i As Long, j As Integer, flag As Boolean[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]Sub GetData()
   Dim Rw As Variant
   If Len(UserForm1.TextBox51.Value) > 0 Then
      
      flag = False
      id = UserForm1.TextBox51.Value
      Rw = Application.Match(id, Sheets("PostCode").Range("A2:A9316"), 0)
      
      If Not IsError(Rw) Then
         flag = True
         For j = 1 To 1
            UserForm1.Controls("TextBox52" & j).Value = ThisWorkbook.Worksheets("PostCode").Cells(Rw, j).Value
         Next j
      End If
      
      If flag = False Then
         For j = 1 To 1
            UserForm1.Controls("TextBox52" & j).Value = ""
         Next j
      End If
      
   Else
     
   End If
End Sub[/COLOR][/SIZE][/FONT]

 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
What/where is the actual error?
 
Upvote 0
Hi I get a yellow arrow pointing at the snippet below.
Code:
UserForm1.Controls("TextBox52" & j).Value = ""
 
Upvote 0
Untested, but try this:

Code:
Sub GetData()
   Dim Rw As Variant
   With UserForm1
        If Len(.TextBox51.Value) > 0 Then
          
           Rw = Application.Match(.TextBox51.Value, Sheets("PostCode").Range("[COLOR=#ff0000]A1[/COLOR]:A9316"), 0)
           
           If Not IsError(Rw) Then
                 .TextBox52.Value = Sheets("PostCode").Cells(Rw, "B").Value
           Else
                 .TextBox52.Value = ""
           End If
          
        End If
   End With
End Sub

It's better if you use a command button to call Sub GetData, like this (assuming Sub GetData is in Module1):

Code:
Private Sub CommandButton1_Click()
Call Module1.GetData
End Sub
 
Upvote 0
For some reason you are looping here:
Code:
For j = 1 To 1
            UserForm1.Controls("TextBox52" & j).Value = ""

Which means that you are trying to access Textbox521 which doesn't exist in your userform.
 
Upvote 0
1. You don't need loop to insert the value in textbox52

2. if j=1, then ("TextBox52" & j) means "TextBox521", that's what cause the error

3. this line:
Rw = Application.Match(id, Sheets("PostCode").Range("A2:A9316"), 0)
since you begin at Range("A2") then if rw = 1 means row 2 not 1

so with this line:
ThisWorkbook.Worksheets("PostCode").Cells(Rw, j).Value

the row will off by 1, so I change Range("A2:A9316") to Range("A1:A9316")
 
Upvote 0
that is brilliant I understand where I went wrong, thank you for your help, much appreciated.
 
Upvote 0
You're welcome, glad to help, & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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