Userform Functionality: How to Use a Combobox to Specify Which Worksheet Data is Pulled From/Entered Into

TheJay

Active Member
Joined
Nov 12, 2014
Messages
364
Office Version
  1. 2019
Platform
  1. Windows
Hello there, I am designing a userform which enables me to search a workbook with several worksheets containing contact details for different types of organisation.

sly1CUj.png

G96K28Y.png


There are five worksheets that I want to include in the combobox for the 'Update' tab, but there are a number of worksheets which I do not want to include but I also do not want to hide from view outside of the userform.

The worksheets I want to include are called 'CC', 'LC', 'HA', 'LL', 'LSA' and the ones I do not want to include are 'SCH', 'DVP', 'EMP', 'SC', 'WH', 'SS', 'HP'

How do I include these in my combobox so that once one of them is selected, it pulls the data from that worksheet into the userform fields which can be scrolled through using the 'next' and 'prev' buttons, and then a particular entry can be updated, deleted or a new entry can be inserted into that particular worksheet?

I really appreciate your help.
 
Last edited:
Hi,
You can declare an object variable & assign it to the worksheet object via your combobox change event.

As an example, something like this:

Rich (BB code):
Dim ws As Worksheet
Private Sub ComboBox1_Change()
    With Me.ComboBox1
        If .ListIndex <> -1 Then Set ws = Worksheets(.Text)
    End With
End Sub

Where ws is your object variable this is initialized by use of the Set Statement.
By placing the variable to the top of your forms code page outside any procedure it becomes available to other procedures in the code page.

To populate your combobox you can use the UserForm_Initialize event

Rich (BB code):
Private Sub UserForm_Initialize()
    Me.ComboBox1.List = Array("CC", "LC", "HA", "LL", "LSA")
End Sub

When your form is displayed, your combobox should now contain the worksheet names – each selection you make, the ws object variable will then refer to the selected worksheet.

You can use this in your other codes, navigation, delete etc.

Hope Helpful

Dave
 
Upvote 0
Hello Dave,

Thanks for your reply.

I can insert the code to populate the combobox and that works fine. I can't figure out where or how to insert the other code though?

Code:
Dim [COLOR=#ff0000]ws[/COLOR] As Worksheet
Private Sub cbContactType_Change()
    With Me.cbContactType
        If .ListIndex <> -1 Then Set ws = Worksheets(.Text)
    End With
End Sub
 
Last edited:
Upvote 0
So far I have:
Code:
Dim ws As WorksheetPrivate Sub cbContactType_Change()
    With Me.cbContactType
        If .ListIndex <> -1 Then Set ws = Worksheets(.Text)
    End With
End Sub
Private Sub UserForm_Initialize()
    Me.cbContactType.List = Array("[COLOR=#333333]CC", "LC", "HA", "LL", "LSA[/COLOR], "DVPS")
End Sub
Private Sub cmdbClose_Click()
Unload Me
End Sub

It does not populate the data into the text boxes from the worksheet selected in the dropdown though.
 
Upvote 0
So far I have:
Code:
Dim ws As WorksheetPrivate 
Sub cbContactType_Change()
    With Me.cbContactType
        If .ListIndex <> -1 Then Set ws = Worksheets(.Text)
    End With
End Sub
Private Sub UserForm_Initialize()
    Me.cbContactType.List = Array("[COLOR=#333333]CC", "LC", "HA", "LL", "LSA[/COLOR], "DVPS")
End Sub
Private Sub cmdbClose_Click()
Unload Me
End Sub

It does not populate the data into the text boxes from the worksheet selected in the dropdown though.


I did state in my post

You can use this in your other codes, navigation, delete etc.

where I assumed you already had other codes to perform required tasks but I take it from your post that you have no other codes for the Userform?

If this is so & you are completely new to userforms, I suggest that you start here:http://www.contextures.com/xlUserForm01.html

Gives a step by step guide on the subject & you can download sample workbook for free.


Hope Helpful

Dave
 
Upvote 0
Hello Dave, thanks for your reply. I have dabbled with userforms and VBA but I just can't get my head around how the combobox code will interact with all the other text boxes and buttons relating to the selected sheet rather than a particular one.
 
Upvote 0
If you do have other codes in your userform then post them here. I & others may be able to offer further guidance. If not, then do have a look at suggested site which may help answer some of your question.

Dave
 
Upvote 0
Hello Dave, I have attempted to implement the code to choose the sheet to add data to and this is what I have:

Code:
Dim WS As Worksheet

Private Sub cbContactType_Change()
    With Me.cbContactType
        If .ListIndex <> -1 Then Set WS = Worksheets(.Text)
    End With
End Sub




Private Sub UserForm_Initialize()
    Me.cbContactType.List = Array("Council Contacts", "Local Contacts", "Housing Associations", "Landlords", "Letting & Selling Agents", "Developers")
End Sub


Private Sub cmdbNew_Click()
Dim cNum As Integer
Dim X As Integer
Dim nextrow As Range
Dim sht As String
sht = cbContactType.Value
If Me.cbContactType.Value = "" Then
MsgBox "Select a contact type"
Exit Sub
End If
cNum = 8
Set nextrow = Sheets(sht).Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)
For X = 1 To cNum
nextrow = Me.Controls("Reg" & X).Value
Set nextrow = nextrow.Offset(0, 1)
Next
For X = 1 To cNum
Me.Controls("Reg" & X).Value = ""
Next
End Sub


Private Sub cmdbClose_Click()
Unload Me
End Sub

I am getting an error on:

Code:
nextrow = Me.Controls("Reg" & X).Value

'Could not find the specified object'

Do you know what I have done wrong?
 
Upvote 0
Do you have controls on the userform named Reg1, Reg2 etc?
 
Upvote 0
Hello Norie, thanks for your reply.

I did, yes. The problem was that I specified 8 when there were only 7 text boxes. Now it seems to run the code without any debug errors but it puts the data into columns 'C' to 'I' rather than 'A' to 'G'. Also, it enters the data into a row where data has already been populated.
 
Last edited:
Upvote 0

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