Loop and Check box

gerald.lebret

New Member
Joined
Nov 4, 2011
Messages
7
Hello,

I have designed a form with about 160 controls among about a third of checkboxes.
The purpose for the users is to be able to check several checkboxes and when they validate the form vba will create a new entry in an excel sheet.

I've written the following code but can't make it work.

Can anyone help?


Code:
Private Sub Update_Click()
Dim CBX As CheckBox
Dim LastRow As Long
Dim ctrl As Control
On Error GoTo ErrorHandle
  
LastRow = Worksheets("Rapport").Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(LastRow, 1).Select
  
    For Each ctrl In Me.Controls
        If TypeOf ctrl Is MSForms.CheckBox Then
           If CheckBox.Value = True Then
           ActiveCell.Value = CheckBox.Caption
            End If
        End If
    Cells(LastRow, 1).Select
    Next ctrl
ErrorHandle:
MsgBox Err.Description
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi Gerald,

You were not incrementing the "LastRow" so all data was sent to the same cell...

Try this small change;

Code:
Private Sub Update_Click()
Dim CBX As CheckBox
Dim LastRow As Long
Dim ctrl As Control
On Error GoTo ErrorHandle
  
LastRow = Worksheets("Rapport").Cells(Rows.Count, 1).End(xlUp).Row + 1
  
    For Each ctrl In Me.Controls
        If TypeOf ctrl Is MSForms.CheckBox Then
            If CheckBox.Value = True Then
                [COLOR=#ff0000]Cells(LastRow, 1).[/COLOR]Value = CheckBox.Caption
            End If
        End If
        [COLOR=#ff0000]LastRow = LastRow + 1[/COLOR]
    Next ctrl
ErrorHandle:
MsgBox Err.Description
End Sub

Hope this helps,
Cheers,
Alan.
 
Upvote 0
As well as Alans's suggestions may want to try changing part of this line:

Rich (BB code):
If CheckBox.Value

to this

Rich (BB code):
If ctrl.Value


Dave
 
Upvote 0
Thank you guys for your answer. It does go to the right cell. However, I do not get the caption of my check boxes when I tick them. Any Idea??

Thanks
 
Upvote 0

Forum statistics

Threads
1,214,887
Messages
6,122,095
Members
449,064
Latest member
Danger_SF

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