VBA Loop

raccoon588

Board Regular
Joined
Aug 5, 2016
Messages
118
i have the following code, it loops through the check boxes within the user form. Id like to be able to have the message change depending on which check box is true. how could i adjust this loop to have a specific message per check box that is true and then keep going continuing the loop every time hitting a different true check box and giving a different message depending on what comes up true.



Code:
Private Sub CmdSubmit_Click()
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
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
If your checkboxes are named checkbox1, checkbox2 etc try
Code:
Dim C As MSForms.Control
Dim msg As String
   For Each C In Me.Controls '<--| loop through userform controls
      If TypeName(C) = "CheckBox" Then
         If C.Value = True Then
            MsgBox Choose(CLng(Right(C.name, 1)), "box1", "box2", "box3")
         End If
      End If
   Next C
 
Upvote 0
my check boxes are named ChkFD1-ChkFD7, ChkFH1-ChkFH7, and ChkSH1-ChkSH7. do i need to adjust for this?
 
Upvote 0
for example if the check box is true on ChkFD1 it will show Full Day & lblDayOne if ChkFD2 is true then Full Day & lblDayTwo etc.
 
Upvote 0
How about
Code:
Dim C As MSForms.Control
Dim msg As String
   For Each C In Me.Controls '<--| loop through userform controls
      If TypeName(C) = "CheckBox" Then
         If C.Value = True Then
            Select Case Mid(C.name, 4, 2)
               Case "FD"
                  MsgBox "Full Day " & Choose(CLng(Right(C.name, 1)), lblDayOne, lbldaytwo, lbldaythree)
               Case "HD"
                  MsgBox "Half Day " & Choose(CLng(Right(C.name, 1)), lblDayOne, lbldaytwo, lbldaythree)
               Case "SH"
                  MsgBox "Whatever " & Choose(CLng(Right(C.name, 1)), lblDayOne, lbldaytwo, lbldaythree)
            End Select
         End If
      End If
   Next C
 
Upvote 0
It is looping through the Full Days but does not move to the other two cases.

I was just able to adjust where it was getting hung up. thank you. it works
 
Last edited:
Upvote 0
is there also a way to take the data from the message and populate it into the excel sheet behind the user form? in an ongoing list?
 
Upvote 0
Something like
Code:
               Case "FD"
                  msg = "Full Day " & Choose(CLng(Right(C.name, 1)), lblDayOne, lbldaytwo, lbldaythree)
                  mesgbox msg
                  Range("A" & Rows.count).End(xlUp).Offset(1).Value = msg
 
Upvote 0

Forum statistics

Threads
1,213,482
Messages
6,113,913
Members
448,532
Latest member
9Kimo3

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