Loop through All checkboxes in userform , and perform action from a button

Muhammad_Bilal

New Member
Joined
Sep 1, 2017
Messages
14
I have a Userform having several Checkboxes and a Button to perform action.

I want to loop through all checkboxes, If the value of check box is true it should show its Caption in MsgBox. but my code return error.

Code:
Dim C As MSForms.Control
    For Each C In Me.Controls '<--| loop through userform controls
        If TypeName(C) = "CheckBox" Then
        If Me.CheckBox.Value = True Then
        
            MsgBox C.Name
            End If
        End If
    Next C

Q2. Can I use do/loop to perform the same job, is there another method rather than `For Each`
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try
Code:
Dim C As MSForms.Control
    For Each C In Me.Controls '<--| loop through userform controls
        If TypeName(C) = "CheckBox" Then
        If C.Value = True Then
        
            MsgBox C.Name
            End If
        End If
    Next C

End Sub
 
Upvote 0
working fine thanks bro.

do you have any idea about following?

Q2. Can I use do/loop to perform the same job, is there another method rather than `For Each`
 
Upvote 0
Yes you probably could, but I don't often use them, so not 100% sure.
I prefer For/Next or For/Each loops where ever possible.
 
Upvote 0
My situation is I am using your corrected code to opening Workbook from MS Word where I find something and delete it. to achieve that I am using Userform having check boxes and buttons.
I am having problem with Excel Object naming Xc
For the first time I run Paste_click macro it run properly; but on second-time, it Run-time Error '13': Type mismatch.
I have checked all spellings, everything is fine.
I had Xc.Quit at the end, and Set Xc= Nothing
I did not understand where its going wrong. I think on the first run there may be something which I did not Quit or Set to Nothing. I put whole code below. kindly help...


Code:
Dim Xc As Object
Set Xc = CreateObject("Excel.Application")
Xc.Visible = True
Set Wb = Xc.Workbooks.Open("C:\Users\dell\Desktop\EMEA CEEMEA\EMEA CC FINAL LIST.xls")
    
    Dim C As MSForms.Control
    For Each C In Me.Controls
        If TypeName(C) = "CheckBox" Then
        If C.Value = True Then
        If C.Caption = "Select All" Then
        Else
       




        Dim ff As String
        ff = Trim(C.Caption)
      With Wb
            .Application.Sheets("Sheet2").Select
            .Application.Range("A1").Select
           
            .Application.Cells.Find(What:=ff, After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
            .Application.ActiveCell.Rows("1:1").EntireRow.Select
            .Application.Selection.Delete Shift:=xlUp
            .Application.Range("A1").Select
      


      End With
        
        End If
        End If
        End If
    Next C


Wb.Close SaveChanges:=True
Workbooks.Close
Set Wb = Nothing
Xc.Quit
Set Xc = Nothing
 
Last edited by a moderator:
Upvote 0
What line of code gives the error?
 
Upvote 0
Error is at following code, on second time running of the macro.

Code:
.Application.Cells.Find(What:=ff, After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate

on the first run its work smoothly.
 
Upvote 0
Best guess is that there is nothing to find. Especially if you have already run the macro once.
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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