简体   繁体   中英

Select case among different checkboxes inside frame - Userform issue

I have putted several checkboxes in a frame and I need to make an select case statement among these checkboxes. Is there a way to select the checkboxes which are within the frame through a select case statement?

I have tried it below ( my frame is named diet_frame ) but I get a type mismatch error.

Private Sub add_button_Click()

Dim target_range As Range: Set target_range = Range("A2:G29")
Dim period As String

target_range.End(xlDown).Offset(1, 0).Select
With Selection: i = 1
    Selection.Value = name_input
    Selection.Offset(0, i) = period_input.Value

    Select Case diet_frame  <<<--- name of my frame, trying to get to the checkboxes placed inside it. 

        Case meat_input.Value = True
            Selection.Offset(0, i + 1) = carnivore
        Case vegetation_input.Value = True
            Selection.Offset(0, i + 1) = herbivore
        Case vegetation_input = True And meat_input = True
            Selection.Offset(0, i + 1) = omnivore
    End Select

    Selection.Offset(0, i + 6) = group_input.Value
End With

The help says that

  • the select case statement requires a numeric or string expression - ie an expression which result can be evaluated as a number or a string via the play of automatic conversions. So strictly on the form, and unless the Frame object default property actually returned a numeric or string expression, you're going to get a type mismatch error there with your code
  • the result of the expression from the select case statement is going to be matched against the result of the expressions for the different Case clauses - so these expressions need to be type compatible as well

Try and use the True keyword as your expression for the select case statement - booleans are automatically convertible to both numbers and strings -, and the values of your checkboxes for each Case clauses. This would also require for you to move the case where both your checkboxes are True to the first place in your Case hierarchy - reason for this is that when expressions match for more than one Case clause, only the statements following the first match are executed

Frames are usually used to group option (radio) buttons, to make them mutually exclusive. I don't think they provide the functionality you expect. You need to be explicit about a few things: Where are name_input, period_input, group_input coming from? If those are cells I expect to see some "RangeRefersTo". Do you have a single set of checkboxes or many sets aligned with these many rows of the Selection object? Programmatically generated or manually (and left as-is for the life of workbook)? You might have meant a "ForEach ... in Selection" rather than a "With"? The With is not going to iterate through cells for you. By the way, you aren't incrementing your "i" column-number so why not just hard-code the 1, 2, and 7? If you have lots of static checkboxes use the tag property to store the row number associated with them. That is a way to tie them to specific cells if you don't have bound-cells for them. If you have lots of boxes and bound-cells then you can't re-use the 3 names for them without going to some extra trouble. You want to poll a bunch of checkboxes? (quite possibly pseudocode) Dim ctrl as control For Each ctrl in controls If ctrl.typeof = "checkbox" then 'maybe further specificity based on If ctrl.name = "somename" ... ' more code... cells(ctrl.tag,7).value2 = whatever End if

As far as your "Select", your code will run as-is if you just put Select Case True (weird but it works) but for Omnivores you're setting the value 3 times. Actuall you don't even need VBA. You can do all this with cell formulas. Put 1 for true in bound cell for meat, 2 for veggie. Put a sum of them in an IF or CHOOSE formula in the target cell (where "...vore" will appear). So 1 is Carnie, 2 is Veggie, 3 is Omni. On a Gnu/Linux box or I'd be more explicit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM