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

DreyFox

New Member
Joined
Nov 25, 2020
Messages
44
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!
 

Some videos you may like

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
3,951
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
 

DreyFox

New Member
Joined
Nov 25, 2020
Messages
44
Office Version
  1. 2016
Platform
  1. Windows
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?
 

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
3,951
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.
 

DreyFox

New Member
Joined
Nov 25, 2020
Messages
44
Office Version
  1. 2016
Platform
  1. Windows
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.
 

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
3,951
Its good to learn at least one new thing each day. :)
 

Watch MrExcel Video

Forum statistics

Threads
1,127,062
Messages
5,622,466
Members
415,896
Latest member
Blooze

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
Top