Adding data with a search userform & then populating another userform with values..

pierre robinson

New Member
Joined
Sep 28, 2016
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Hi Team.

Bit hard to describe here, but what I am doing is adding an address from a list onto a datasheet, using a userform,(oddly enough called "SearchUF"). So the code adds the new address in Col B using xlup and the remainder of that row is populated using a series of formulas. Which works perfectly well.

What I want to happen after the select command button is clicked on the SearchUF, is for to it to close and another userform (Userform1) to open, populated with that new address and the subsequent fields that have been added via the formulas.

I can get Userform1 to show, but it will not populate the various text boxes.

I also understand that it necessarily needs to be a separate event, but cant figure out the syntax required.

Code as below:

Private Sub cbSelectUpdate_Click()

Dim lastrow As Integer
Dim Address As String


Address = cbSearchAddress.Value
lastrow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row




If Application.WorksheetFunction.CountIf(Sheets("Datasheet").Range("B3:B20000"), Address) > 0 Then
MsgBox "Address has already been added", 0, "Please Check"
Exit Sub


Else


Worksheets("Datasheet").Range("B" & lastrow + 1).Value = cbSearchAddress.Value




End If




Unload SearchUF

Call UpdateUF


End Sub



Private Sub UpdateUF()


Dim Targetrow As String


Targetrow = Sheets("Engine").Range("B1").Value


'lastrow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row




Userform1.AddItem.cbAddress = Sheets("Datasheet").Range("Data_Start").Offset(Targetrow, 1).Value


Userform1.AddItem.tbBeds = Sheets("Datasheet").Range("Data_Start").Offset(Targetrow, 10).Value


:rofl:
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
How about using the ".Show" command, to activate UserForm1, then put your code to populate it, into it's "_Initialize" event:
Code:
Private Sub cbSelectUpdate_Click()

Dim lastrow As Integer
Dim Address As String

Address = cbSearchAddress.Value
lastrow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row


If Application.WorksheetFunction.CountIf(Sheets("Datasheet").Range("B3:B20000"), Address) > 0 Then
MsgBox "Address has already been added", 0, "Please Check"
Exit Sub

Else

Worksheets("Datasheet").Range("B" & lastrow + 1).Value = cbSearchAddress.Value

End If

Unload SearchUF

UserForm1.show

End Sub

Then, for userform1:
Code:
Private Sub UserForm_Initialize()
Dim Targetrow As String

Targetrow = Sheets("Engine").Range("B1").Value

'lastrow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row

Userform1.AddItem.cbAddress = Sheets("Datasheet").Range("Data_Start").Offset(Targetrow, 1).Value

Userform1.AddItem.tbBeds = Sheets("Datasheet").Range("Data_Start").Offset(Targetrow, 10).Value
End Sub
... not tested, so it may be better to test on a copy of your real work!
 
Upvote 0
Yep, hearing you - the issue with that is that Userform1 loads at the same time as the SearchUF.

My logic is that the UF should load after the searchUF closes, but......

VBA...all good fun :)

Cheers


How about using the ".Show" command, to activate UserForm1, then put your code to populate it, into it's "_Initialize" event:
Code:
Private Sub cbSelectUpdate_Click()

Dim lastrow As Integer
Dim Address As String

Address = cbSearchAddress.Value
lastrow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row


If Application.WorksheetFunction.CountIf(Sheets("Datasheet").Range("B3:B20000"), Address) > 0 Then
MsgBox "Address has already been added", 0, "Please Check"
Exit Sub

Else

Worksheets("Datasheet").Range("B" & lastrow + 1).Value = cbSearchAddress.Value

End If

Unload SearchUF

UserForm1.show

End Sub

Then, for userform1:
Code:
Private Sub UserForm_Initialize()
Dim Targetrow As String

Targetrow = Sheets("Engine").Range("B1").Value

'lastrow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row

Userform1.AddItem.cbAddress = Sheets("Datasheet").Range("Data_Start").Offset(Targetrow, 1).Value

Userform1.AddItem.tbBeds = Sheets("Datasheet").Range("Data_Start").Offset(Targetrow, 10).Value
End Sub
... not tested, so it may be better to test on a copy of your real work!
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

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