2

Is there a way to code the option buttons to give a value based off both being selected? I have tried multiple ways and seems I am getting nowhere. This is the last code I have tried and this is how I would like it to work if possible. The way this code is right now, I am only getting one value.

Private Sub OptBtn()

    Dim n As Long

    n = Sheets1.Range("A" & Application.Rows.Count).End(xlUp).Row
    Range("A" & Rows.Count).End(xlUp).Select
    ActiveCell.Offset(1, 0).Select
    ActiveCell.EntireRow.Insert


    If Me.OptBtn_AddCard.Value = True & Me.OptBtn_ManagedBy.Value = True Then
        Sheets1.Range("I" & n + 1).Value = "ACTIVE"

    Else

        If Me.OptBtn_AddCard.Value = True & Me.OptBtn_ManagedBy.Value = False Then
            Sheets1.Range("I" & n + 1).Value = "AVAILABLE"

        Else

            If Me.OptBtn_AddCard.Value = False & Me.OptBtn_ManagedBy.Value = True Then
                Sheets1.Range("I" & n + 1).Value = "ACTIVE"

            End If
        End If
    End If

End Sub
1
  • 1
    Me.OptBtn_AddCard.Value = True And Me.OptBtn_ManagedBy.Value = True, & operator is for concatenating strings. Commented Mar 24, 2022 at 5:53

1 Answer 1

3

Use And instead of &.

If Me.OptBtn_AddCard.Value = True And Me.OptBtn_ManagedBy.Value = True Then
    Sheets1.Range("I" & n + 1).Value = "ACTIVE"
Else
    If Me.OptBtn_AddCard.Value = True And Me.OptBtn_ManagedBy.Value = False Then
        Sheets1.Range("I" & n + 1).Value = "AVAILABLE"
    Else
        If Me.OptBtn_AddCard.Value = False And Me.OptBtn_ManagedBy.Value = True Then
            Sheets1.Range("I" & n + 1).Value = "ACTIVE"
        End If
    End If
End If

The code can be shortened like this:

If Me.OptBtn_AddCard.Value And Me.OptBtn_ManagedBy.Value Then
    Sheets1.Range("I" & n + 1).Value = "ACTIVE"
Else
    If Me.OptBtn_AddCard.Value And Not Me.OptBtn_ManagedBy.Value Then
        Sheets1.Range("I" & n + 1).Value = "AVAILABLE"
    Else
        If Not Me.OptBtn_AddCard.Value And Me.OptBtn_ManagedBy.Value Then
            Sheets1.Range("I" & n + 1).Value = "ACTIVE"
        End If
    End If
End If

I personally like the Switch() variation:

Switch function: Evaluates a list of expressions and returns a Variant value or an expression associated with the first expression in the list that is True

Sheets1.Range("I" & n + 1).Value = Switch( _
    Me.OptBtn_AddCard.Value And Me.OptBtn_ManagedBy.Value, "ACTIVE", _
    Me.OptBtn_AddCard.Value And Not Me.OptBtn_ManagedBy.Value, "AVAILABLE", _
    Not Me.OptBtn_AddCard.Value And Me.OptBtn_ManagedBy.Value, "ACTIVE")
Sign up to request clarification or add additional context in comments.

1 Comment

That solved it. Thanks so much! I figured I was doing something wrong. I tried the switch variation and now I'm hooked. Thanks again for showing me something new.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.