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`
 
In following code I opened workbook as object, do some work, and then close the same workbook.

My Question is, did workbook closed/Quit?
Is there any other code to close the workbook?


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
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
My Question is, did workbook closed/Quit?
If you're asking me if the workbook was closed, then I'm not sure as I can't see your monitor.
If you're asking something else, then I'm afraid I don't understand.

Also, where are you running this code from?
 
Upvote 0

Forum statistics

Threads
1,221,526
Messages
6,160,340
Members
451,637
Latest member
hvp2262

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