How would I extract a specific range of cells from another sheet to the current one depending on the selection of a checkbox?

DreyFox

Board Regular
Joined
Nov 25, 2020
Messages
61
Office Version
  1. 2016
Platform
  1. Windows
Hello!

For example, I have checkbox1, checkbox2 and checkbox3. If I select 1 and 2, then only a specific range say A1:B20 from another sheet is extracted into the current one. There doesn't need to be an else condition (no need to check if the checkboxes have been unchecked in my case).

Any help is greatly appreciated!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
VBA Code:
Sub CopyRows()

For Each chkbx In ActiveSheet.CheckBoxes
    If chkbx.Value = 1 Then
        For r = 1 To Rows.Count
            If Cells(r, 1).Top = chkbx.Top Then
                With Worksheets("Sheet2")
                    LRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
                    .Range("A" & LRow & ":D" & LRow) = _
                    Worksheets("Sheet1").Range("A" & r & ":D" & r).Value
                End With
                Exit For
            End If
        Next r
    End If
Next

End Sub

The above is a segment of the complete workbook and additional code. If you take the time to study the code
you can understand how to edit the code to fit your purposes.

Download workbook : Copy-selected-rows-checkboxes.xlsm
 
Upvote 0
VBA Code:
Sub CopyRows()

For Each chkbx In ActiveSheet.CheckBoxes
    If chkbx.Value = 1 Then
        For r = 1 To Rows.Count
            If Cells(r, 1).Top = chkbx.Top Then
                With Worksheets("Sheet2")
                    LRow = .Range("A" & Rows.Count).End(xlUp).Row + 1
                    .Range("A" & LRow & ":D" & LRow) = _
                    Worksheets("Sheet1").Range("A" & r & ":D" & r).Value
                End With
                Exit For
            End If
        Next r
    End If
Next

End Sub

The above is a segment of the complete workbook and additional code. If you take the time to study the code
you can understand how to edit the code to fit your purposes.

Download workbook : Copy-selected-rows-checkboxes.xlsm
Would this work with ActiveX checkboxes as well?
 
Upvote 0
For ActiveX you'll need to be looking into OLEObjects.Add("Forms.checkbox.1").

Keep in mind that ActiveX controls are notorious for creating issues due to them being "unstable" shall we say ?
They tend to fail erratically and it has nothing to do with your coding skills. Best to stick with Form Controls.
 
Upvote 0
For ActiveX you'll need to be looking into OLEObjects.Add("Forms.checkbox.1").

Keep in mind that ActiveX controls are notorious for creating issues due to them being "unstable" shall we say ?
They tend to fail erratically and it has nothing to do with your coding skills. Best to stick with Form Controls.
I wish i'd known this before creating hundreds and hundreds of ActiveX controls.
 
Upvote 0

Forum statistics

Threads
1,213,565
Messages
6,114,337
Members
448,568
Latest member
Honeymonster123

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