Combobox to tell what worksheet the data should go.

JeremySun

Board Regular
Joined
Jul 1, 2011
Messages
98
Hello,

I would like to know if it is possible to have a combobox answer determin what worksheet the userform information goes to. Example:

Combobox options are : English, Math, Science, and AP
My worksheets are: English, Math, Science, and AP

The user form contains student's first name, last name, address, e-mail, phone, etc....

I would like to have one workbook for all the information. Below is what I have

Code:
Private Sub cmdAdd_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = ""
If txtfirstname.Value = "" Or txtrank.Value = "" _
Or txtemail.Value = "" Or txtlastname.Value = "" Or txtworkplace.Value = "" Or txtssn.Value = "" _
Or txtphone.Value = "" Then
MsgBox Prompt:="All fields are required", _
Title:="REMINDER"
End If
iRow = ws.Cells(Rows.Count, 1) _
  .End(xlUp).Offset(1, 0).Row


'copy the data to the database
ws.Cells(iRow, 1).Value = Me.txtfirstname.Value
ws.Cells(iRow, 2).Value = Me.txtlastname.Value
ws.Cells(iRow, 3).Value = Me.txtrank.Value
ws.Cells(iRow, 4).Value = Me.txtemail.Value
ws.Cells(iRow, 5).Value = Me.txtworkplace.Value
ws.Cells(iRow, 6).Value = Me.txtssn.Value
ws.Cells(iRow, 7).Value = Me.txtphone.Value
ws.Cells(iRow, 8).Value = Me.txtdate.Value
ws.Cells(iRow, 9).Value = Me.cbtarget.Value    <----class names....ENGLISH,MATH,,,,,


'clear the data
Me.txtfirstname.Value = ""
Me.txtlastname.Value = ""
Me.txtrank.Value = ""
Me.txtemail.Value = ""
Me.txtworkplace.Value = ""
Me.txtssn.Value = ""
Me.txtphone.Value = ""

End Sub
 
Last edited by a moderator:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Jeremy welcome to the boards.

Replace Set ws = "" with this.
Code:
    set ws = Nothing
    If cbtarget.Text <> "" Then
        Set ws = Sheets(cbtarget.Text)
    End If

Also add Or ws Is Nothing to the validation if statement.
 
Last edited:
Upvote 0
Good to hear it works. I'm fairly certain that a board member without admin/mod privileges can't close a thread. Common practice is to leave a thread open and to responded that your question was answered.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,331
Members
452,907
Latest member
Roland Deschain

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