Running VBA based on one or more Checkboxes selected

justme101

Board Regular
Joined
Nov 18, 2017
Messages
67
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a sheet which is the source of some data and the ultimate goal is to copy the to another sheet. Now, the data is being copied from 4 columns. and is being transposed into rows to a different sheet. I have got the code working nicely. But, now the requirement is that not all columns beed to be copied all the time. Sometimes column x might need to be copied, sometimes X and Y, sometimes X and Z...and so on. There are 4 columns which have the data to be copied A, B, C and D.

I created a VBA user form which has 4 check boxes, basically giving the option to select which column(s) need to be copied over. So, if only the check box for column A and C are checked, the code will copy only columns A and C to the destination sheet. So, you can see there will be multiple permutations and combinations of the check boxes here. I need guidance on how to structure this code. I can write the action code in the shell, just need the shell for it. Any help is greatly appreciated. Thank you.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
For example:
-assign to the checkboxes the name CBx1, CBx2, ..., where x1 is the number of the columns potentially to be copied

-then use the following approach:
VBA Code:
Dim toCopy, cCol

toCopy = Array(4, 6, 7, 8)          '<<< List of the columns to copy
For Each cCol In toCopy
    If UserForm1.Controls("CB" & cCol) Then
        'here the code to copy column #cCol
    End If
Next cCol

With this example the chckboxes have to be named CD4, CB6, CB7, CB8

Try...
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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