copy data from userform to multiple sheets based on checkbox selection

Starchaser

New Member
Joined
Mar 4, 2017
Messages
10
Hi everybody,

I want to copy selected data out of a listbox to different other sheets based on a selection of multiple checkboxes
I've already created a similar situation, but then you could only select one sheet at a time (by using a combobox).
But this means that the user had to click the submit button for each sheet he wants to copy the data.
If the user could select all sheets in one time and click one time the "submit" it would be "userfriendly".

image_checkbox.png


Following choices are possible

1. For each checkbox selected the data in the textboxes should be copied to the sheet which has the same name as the selected boxes.
So the user can select multiple boxes to copy the data to multiple sheets in one time
2. If checkbox B and C is selected, the data should be copied to both sheets and also be copied to sheet "BC"
3. If checkbox A and B and C is selected, the data should be copied to all three sheets and also be copied to sheet "ABC"
image_checkbox_sheets.png


Many thanks for your advice
grtz
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Put the following in your userform code:

VBA Code:
Private Sub CommandButton1_Click()
  'Validations
  If TextBox1.Value = "" Then
    MsgBox "Enter ID"
    Exit Sub
  End If
  If TextBox2.Value = "" Then
    MsgBox "Enter Name"
    Exit Sub
  End If
  If TextBox3.Value = "" Then
    MsgBox "Enter Firt Name"
    Exit Sub
  End If
  
  If CheckBox1.Value + CheckBox2.Value + CheckBox3.Value = 0 Then
    MsgBox "Mark checkbox"
    Exit Sub
  End If
  
  'Add data
  If CheckBox1.Value = True Then Call Add_Data(CheckBox1.Caption)
  If CheckBox2.Value = True Then Call Add_Data(CheckBox2.Caption)
  If CheckBox3.Value = True Then Call Add_Data(CheckBox3.Caption)
End Sub

Private Sub Add_Data(sName As String)
  Dim lr As Long
  If Evaluate("ISREF('" & sName & "'!A1)") Then
    With Sheets(sName)
      lr = .Range("B" & Rows.Count).End(3).Row + 1
      .Range("A" & lr).Value = TextBox1.Value
      .Range("B" & lr).Value = TextBox4.Value
      .Range("C" & lr).Value = TextBox2.Value & " " & TextBox3.Value
      .Range("D" & lr).Value = TextBox5.Value
    End With
  End If
End Sub
 
Upvote 0
Hi DanteAmor,

Thank you for the quick response.
This works fine for the first part. Data is copied to the correct forms.
But what for the second part? When B and C is True the data should also be copied to sheet BC
When A,B and C is true, the data should also be copied to sheet ABC
I've tried this (which works). But maybe there's a smarter way ;)?

1730565572646.png
 
Upvote 0
Try:

VBA Code:
Private Sub CommandButton1_Click()
  If CbA.Value = True Then Call Add_Data("A")
  If CbB.Value = True And CbC.Value = True Then Call Add_Data("BC")
  If CbA.Value = True And CbB.Value = True And CbC.Value = True Then Call Add_Data("ABC")
End Sub

Private Sub Add_Data(sName As String)
  Dim lr As Long
  With Sheets(sName)
    lr = .Range("B" & Rows.Count).End(3).Row + 1
    .Range("B" & lr).Value = TxtRowNo.Value
    .Range("C" & lr).Value = TxtName.Value & " " & TxtFName.Value
    .Range("D" & lr).Value = TxtCountry.Value
    .Range("A3:D" & lr).Sort .Range("B3"), xlAscending, Header:=xlGuess
  End With
End Sub


Note Code Tag:
In future please use code tags when posting code.​
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.​


😇
 
Upvote 0
Solution
Hey DanteAmor,

Thanks again, I've added two extra lines and every works perfect. Much shorter code than my solution.
VBA Code:
Private Sub CmdSubmit_Click()
  If CbA.Value = True Then Call Add_Data("A")
  If CbB.Value = True Then Call Add_Data("B")
  If CbC.Value = True Then Call Add_Data("C")
  If CbB.Value = True And CbC.Value = True Then Call Add_Data("BC")
  If CbA.Value = True And CbB.Value = True And CbC.Value = True Then Call Add_Data("ABC")
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,871
Messages
6,175,092
Members
452,612
Latest member
MESTeacher

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