Search code examples
excelvbafor-looprange

Add checkboxes in every second row


Below is a section of a Sub which adds rows to a spreadsheet, including checkboxes in every second column.

Every time I try to use a union or complex range it doesn't seem to work for the rngCel2 Dim statement I have.
The top section isn't the part of concern but I've added it for context and just in case some part of it is interfering.

Sub Addrow()

    Dim rngCel2 As Range
    Dim ChkBx As CheckBox
    
    LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
    EventNo = LastRow - 3
    
    NewEvent = EventNo + 1
    
    ActiveSheet.Cells(LastRow + 1, 1).Select
    ActiveCell.Value = NewEvent
    
    If NewEvent Mod 2 = 0 Then
        ActiveSheet.Range(ActiveCell, ActiveCell.Offset(0, 25)).Interior.Color = RGB(242, 242, 242)
    End If
    
    ActiveSheet.Range(ActiveCell.Offset(0, 4), ActiveCell.Offset(0, 23)).Select

    For Each rngCel2 In Selection
        With rngCel2.MergeArea.Cells
            If .Resize(1, 1).Address = rngCel2.Address Then
                Set ChkBx = ActiveSheet.CheckBoxes.Add(.Left, .Top, .Width, .Height)
                With ChkBx
                    .Value = xlOff
                    .LinkedCell = rngCel2.MergeArea.Cells.Address
                    With .Border
                    End With
                End With
            End If
        End With
    Next rngCel2
    
    For Each ChkBx In ActiveSheet.CheckBoxes
        ChkBx.Caption = ""
    Next ChkBx

End Sub

What the code should achieve:
enter image description here

Each macro use (tied to a button) should add one row beneath the last. There isn't any code to recolour the checkbox columns yet.

I tried selecting the required cells individually. That returns an error code because the code requires a range.
The same issue appears when I try a union function.

I tried finding a 'for every second cell' function or using a counter.


Solution

  • Try it without using select.

    Here is my interpretation:

    Sub Addrow()
        Dim ws As Worksheet
        Dim rngCel2 As Range
        Dim ChkBx As CheckBox
        Set ws = ActiveSheet
        With ws
        
            lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
            EventNo = lastrow - 4
            NewEvent = EventNo + 1
            .Cells(lastrow, 1) = NewEvent
        
            If NewEvent Mod 2 = 0 Then
                .Range(.Cells(lastrow, 1), .Cells(lastrow, 26)).Interior.Color = RGB(242, 242, 242)
            End If
            
            a = 6                                    'start column
            b = 26                                   'end column
            Application.ScreenUpdating = False
            For x = a To b Step 2                    'step 2 skips to the next
                Set rngCel2 = .Cells(lastrow, x)
                With rngCel2
                    Set ChkBx = ActiveSheet.CheckBoxes.Add(.Left, .Top, .Width, .Height)
                    With ChkBx
                        .Value = xlOff
                        .LinkedCell = rngCel2.Address
                        With .Border
                        End With
                    End With
                    .Interior.Color = rgbGrey
                End With
            Next x
        
        
            For Each ChkBx In .CheckBoxes
                ChkBx.Caption = ""
            Next ChkBx
        End With
     
    End Sub