I have a userform that is auto populated with checkboxes that correspond to column headings on an activeworksheet.
what i want to do is click any combination of checkboes, when I click the command button it selects/copy the column(s) the checkbox relates to so I can then click another command button to open a new worksheet and paste the selection.
Unfortunately my code will only allow me to select one column at a time and not multiple. please help me, I've spent two days trying to get this to work, i think i have to somehow dd each checkbox to an array or range?
below is the command button
Dim LastColumn As Long, CurrColumn As Long
Dim p As Variant
Dim MyRange As range, Rng As range, Rng1 As range, Rng2 As range
Dim chkbox As msforms.CheckBox
CurrColumn = 1
LastColumn = ActiveSheet.Cells(CurrColumn, Columns.Count).End(xlToLeft).Column
For p = LastColumn To CurrColumn Step -1
Set chkbox = UserForm2.Controls("CheckBox_" & p)
If chkbox.value = True Then
Set MyRange = ActiveSheet.range(chkbox.Tag).EntireColumn '.Copy
End If
Next p
MyRange.Copy
end sub
form code (loads the checkboxes automatically as assigns the tag with the cell reference)
Private Sub UserForm_Initialize()
Dim curcolumn As Long
Dim LastColumn As Long
Dim i As Long
Dim chkbox As msforms.CheckBox
curcolumn = 1 'Set your column index here
'LastRow = Worksheets("holding cat").Cells(Rows.Count, curColumn).End(xlUp).Row
LastColumn = ActiveSheet.Cells(curcolumn, Columns.Count).End(xlToLeft).Column
For i = 1 To LastColumn
Set chkbox = Me.Controls.Add("Forms.CheckBox.1", "CheckBox_" & i)
chkbox.Caption = ActiveSheet.Cells(curcolumn, i).value
chkbox.Left = 5
chkbox.Top = 5 + ((i - 1) * 20)
chkbox.Tag = ActiveSheet.Cells(curcolumn, i).Address
Next i
End Sub
Bookmarks