If then statement with a button on a form

amoverton2

Board Regular
Joined
May 13, 2021
Messages
77
Office Version
  1. 2016
Platform
  1. Windows
Aloha!!

New direction for my database... In my company we have 15 different departments (C100, C200, and so forth). When we get a new person I want to use a userform to input his data. However, upon depressing a macro enabled transfer/save info button it transfers their info to a certain sheet for the specific department or code they are going to working in (for all intents and purposes each codes sheet is labeled "C###"). On the userform there is a combo box to choose which code they are going to. I just don't know how to write the code based to send the info to the right sheet. Help please!

Mahalo,
Adam
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
@amoverton2 Try like below.

VBA Code:
'Transfer Form Info
Dim Dept As String
'Get Department Code for sheet
Dept = Me.ComboBox1.Value
'use appropriate sheet
With Sheets(Dept)
 'Do stuff. Like
    .Range("D4") = Me.Textbox1.Value
    .Range("F4") = Me.Textbox2.Value
End With
 
Upvote 0
Something like this where cboCodes is the name of the codes combobox.

BUT, I would have just one worksheet with a column containing the code and use a filter on that column to view the people in departments.
Doing it like this introduces much more flexibility in terms of data management and reporting.

VBA Code:
Private Sub cmdSubmit_Click()
Dim intRow As Integer
Dim Ws As Worksheet

    Set Ws = Worksheets(Me.cboCodes.Value)
        
    intRow = Ws.Range("A1").CurrentRegion.Rows.Count + 1
    
    Ws.Range("A" & intRow & ":D" & intRow).Value = Array(Me.txtForename, Me.txtSurname, Me.cboGender, Me.cboDOB)

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,755
Members
449,094
Latest member
dsharae57

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