Copy columns when selecting from the checkbox

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hello, how can I modify the code so that I can copy the columns when I click on the checkbox sequentially without forcing me to copy the column in a specific place.

I am currently using this code but it forces me to copy in a specific place

VBA Code:
Private Sub CheckBox1_Click()
sh1.Select
 If form1("CheckBox1").Value = True Then
Columns("A:A").Select
    Selection.Copy
    Sheets("sheet2").Select
    Columns("A:A").Select
    sh2.Paste
 Else
    CheckBox1.Value = False
    sh2.Columns("A:A") = ""
    sh2.Select
    End If
End Sub

Private Sub CheckBox2_Click()
 sh1.Select
 If form1("CheckBox2").Value = True Then
Columns("B:B").Select
    Selection.Copy
    Sheets("sheet2").Select
    Columns("B:B").Select
    sh2.Paste
 Else
    CheckBox2.Value = False
    sh2.Columns("B:B") = ""
    sh2.Select
    End If
End Sub
Private Sub CheckBox3_Click()
 sh1.Select
 If form1("CheckBox3").Value = True Then
Columns("C:C").Select
    Selection.Copy
    Sheets("sheet2").Select
    Columns("C:C").Select
    sh2.Paste
 Else
    CheckBox3.Value = False
    sh2.Columns("C:C") = ""
    sh2.Select
    End If
End Sub
Private Sub CheckBox4_Click()
 sh1.Select
 If form1("CheckBox4").Value = True Then
Columns("D:D").Select
    Selection.Copy
    Sheets("sheet2").Select
    Columns("D:D").Select
    sh2.Paste
 Else
    CheckBox4.Value = False
    sh2.Columns("D:D") = ""
    sh2.Select
    End If
End Sub
Private Sub CheckBox5_Click()
 sh1.Select
 If form1("CheckBox5").Value = True Then
Columns("E:E").Select
    Selection.Copy
    Sheets("sheet2").Select
    Columns("E:E").Select
    sh2.Paste
 Else
    CheckBox5.Value = False
    sh2.Columns("E:E") = ""
    sh2.Select
    End If
End Sub

 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
What is: form1 ?

22.png

UserForm​

 
Upvote 0
Here is how I would write it:
VBA Code:
Private Sub CheckBox1_Click()
'Modified  10/9/2022  9:22:42 PM  EDT
 If form1("CheckBox1").Value = True Then
    Sheets("Alpha").Columns("A:A").Copy
    Sheets("Bravo").Columns(1).PasteSpecial
    Application.CutCopyMode = False
    
    Else
        CheckBox1.Value = False
            Sheets("Bravo").Columns("A:A") = ""
    End If
End Sub
 
Upvote 0
Now I know the sheet names will be different.
But all the selecting is not needed.

Not sure what this means:
You said:
without forcing me to copy the column in a specific place.
 
Upvote 0
Here is how I would write it:
VBA Code:
Private Sub CheckBox1_Click()
'Modified  10/9/2022  9:22:42 PM  EDT
 If form1("CheckBox1").Value = True Then
    Sheets("Alpha").Columns("A:A").Copy
    Sheets("Bravo").Columns(1).PasteSpecial
    Application.CutCopyMode = False
   
    Else
        CheckBox1.Value = False
            Sheets("Bravo").Columns("A:A") = ""
    End If
End Sub
It is required not to specify the place of copying, for example, why column 5 is selected first, it is copied into A:A, and if column 3 is selected in the second choice, it is copied into B:B and vice versa, the order of columns is according to the choice from the checkbox
 
Upvote 0
Try this
VBA Code:
Option Explicit


Private Sub CheckBox1_Click()
    Call ForAllCheckBoxes(CheckBox1)
End Sub

Private Sub CheckBox2_Click()
    Call ForAllCheckBoxes(CheckBox2)
End Sub

Private Sub CheckBox3_Click()
    Call ForAllCheckBoxes(CheckBox3)
End Sub

Private Sub CheckBox4_Click()
    Call ForAllCheckBoxes(CheckBox4)
End Sub

Private Sub CheckBox5_Click()
    Call ForAllCheckBoxes(CheckBox5)
End Sub


Private Sub ForAllCheckBoxes(ChkBox As Control)
    Dim fndHead As Range, col As Long
   
If ChkBox.Value = True Then
    'already been copied ???
    With Sheets("Sheet2")
        Set fndHead = .Range("1:1").Find(What:=ChkBox.Caption, LookIn:=xlValues, _
                                        LookAt:=xlWhole, SearchOrder:=xlByColumns, _
                                        SearchDirection:=xlNext, MatchCase:=False)
        If Not fndHead Is Nothing Then
            MsgBox "The " & ChkBox.Caption & " column already exists" & vbLf & _
                   "You need to uncheck to remove existing first"
            Exit Sub
        End If
    End With
    
    'find column to copy
    With Sheets("Sheet1")
        Set fndHead = .Range("1:1").Find(What:=ChkBox.Caption, LookIn:=xlValues, _
                                        LookAt:=xlWhole, SearchOrder:=xlByColumns, _
                                        SearchDirection:=xlNext, MatchCase:=False)
        If Not fndHead Is Nothing Then
            'copy the column
            .Columns(fndHead.Column).Copy
        Else
            MsgBox ChkBox.Caption & "Not found"
            Exit Sub
        End If
    End With
    'find where to paste
    With Sheets("Sheet2")
        If .Cells(1) = "" Then
            col = 1
        Else
            col = .Cells(1, .Columns.Count).End(xlToLeft).Column + 1
        End If
        'paste it
        .Columns(col).PasteSpecial
    End With
    'stop the marching ants
    Application.CutCopyMode = False
    
Else
    'find column to delete
    With Sheets("Sheet2")
        Set fndHead = .Range("1:1").Find(What:=ChkBox.Caption, LookIn:=xlValues, _
                                        LookAt:=xlWhole, SearchOrder:=xlByColumns, _
                                        SearchDirection:=xlNext, MatchCase:=False)
        If Not fndHead Is Nothing Then
            'remove the column
            .Columns(fndHead.Column).Delete
        End If
    End With
End If

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,828
Messages
6,181,210
Members
453,023
Latest member
alabaz

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