Insert New Row With New Linked Checkbox

Man_of_Sleep

New Member
Joined
Aug 5, 2020
Messages
19
Office Version
  1. 2013
Platform
  1. Windows
Hi,

I have this code to insert new row button.

VBA Code:
Option Explicit
 
Private Sub CommandButton1_Click()
    addNewRow
End Sub
 
Sub addNewRow()
    
    ' Do not insert a row before the first row.
    Dim iTopRow As Integer
    iTopRow = 1
    
    If (ActiveCell.Row > iTopRow) Then

        ' Get the active row number.
        Dim rowNum As Integer
        rowNum = ActiveCell.Row
        
        Rows(rowNum).EntireRow.Insert       ' Insert a new row.
        
    End If
End Sub

Then I am trying to insert the new row, with a new linked checkbox.
The checkbox is automatic linked to the new cell in the new added row.
 

Attachments

  • help.JPG
    help.JPG
    31 KB · Views: 213

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
How about (to be pasted in the module of the worksheet to be affected, like you did)
VBA Code:
Sub addNewRow_r2()
    
    ' Do not insert a row before the first row.
    Const TopRow As Long = 1
    
    ' Get the active row number.
    Dim rowNum As Long
    rowNum = ActiveCell.Row
    
    If (rowNum > TopRow) Then

        Rows(rowNum).Insert       ' Insert a new row.
        
        ' === add a Check Box ===
        Dim oCB As CheckBox
        Dim c   As Range
        
        Set c = Cells(rowNum, 1)
        With c
            Set oCB = CheckBoxes.Add(.Left, .Top, .Width, .Height)
            oCB.LinkedCell = .Address
        End With
    End If
End Sub
 
Upvote 0
wow.. it work really well and great.
i am truly amazing with members in this forum.

Thank you very much GWteB.
 
Upvote 0
sorry..

Can you help me one more?
How to make the checkbox only appear the box when I click the add row button?

I don't want the text 'check box' beside the box.
How to automatically delete them?

Thanks in advance again.
 
Upvote 0
The displayed text "Check Box 1" is the value of the check box's Caption property and can be cleared easily.
In the last section of my post #2 code, insert the red line of code as per below:
Rich (BB code):
        With c
            Set oCB = CheckBoxes.Add(.Left, .Top, .Width, .Height)
            oCB.LinkedCell = .Address
            oCB.Caption = vbNullString
        End With
 
Upvote 0
wow.. before I asked the question, I've spent 3 hours to figured the solution due to my zero VBA knowledge.
You solved with just 1 line.

Thank you very much GWteB.
 
Upvote 0
You are welcome and thanks for letting me know.
 
Upvote 0
Dear GWteB,

Sorry, for pop-up this thread.
I am searching for the solution but i cant find.

I make new excel file.
Then I copy and paste the above code in the module.
But when I run the code, get Run-time error "424'. Object required.

With this line highlighted:
Set oCB = CheckBoxes.Add(.Left, .Top, .Width, .Height)

Can you help me how to solve this issue.

Thank you very much.
 
Upvote 0
Dear GWteb

Please help me with this.

When I copy the VBA above and run it in my excel the check box will not be inserted to all the rows column.
if i inserted raw only jan or april is having check box but the rest of month check box is not been inseted.

Thank you very much for your help.
 

Attachments

  • Capture.JPG
    Capture.JPG
    153.6 KB · Views: 42
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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