Consulting

Results 1 to 8 of 8

Thread: VBA Code - Set up condition to an Option Button

  1. #1
    VBAX Regular
    Joined
    Sep 2014
    Posts
    20
    Location

    VBA Code - Set up condition to an Option Button

    Hi,

    I'm in final stage of my project. I need your help in this critical hour.

    I set up 3 "Option Buttons" viz, "Korfball", "Sepaktakraw" and "Kabbadi".

    I want to set up a condition to one of the Option Button "Kabbadi".

    The Option Button "Kabbadi" should activate only when there is a value in cell A27 or B28. (code enclosed in for reference)
    or
    The ability to click on the Option Button "Kabbadi" should be enabled only when there is a value in cell A27 or B28. (code enclosed in for reference)

    code.jpg

    I request you to help me with this so that I can complete my project.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    In the UserForm_Initialize sub
    With Sheets("Sheet Name Here")
    If (Range("A27") = "") And (Range("B28") = "") Then
    Me.Controls("Kabbadi").Enabled = False
    Else
    Me.Controls("Kabbadi").Enabled = True
    End If
    End With
    You can use the # Icon to place Code tags around your code to format it like this. Click the icon and paste the code between the two code tags, or Select the code and click the Icon.

    BTW, I would refactor those three Control_Click subs to
    Private Sub Control_Click()
    ShowPictures
    End Sub
    Then I would add
    Sub ShowPictures()
    With Me
    .Pictures(picKorfball").Visible = .Korfball.Value
    Etc
    Etc
    End with
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Regular
    Joined
    Sep 2014
    Posts
    20
    Location
    Thank you, Sam.
    Let me try it.

  4. #4
    VBAX Regular
    Joined
    Sep 2014
    Posts
    20
    Location
    If I'm unable to do it, I'll share the file with you through "zippyshare.com.

    Will you help me if I share the file? (In case I'm not able to fit the above close)?

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Share the file by uploading it here.

    At the bottom of the Post Editor, you see "Go Advanced". In the Advanced Editor Page, below the Editor, you will see "Manage Attachments."

    BTW, anything you have written in the Basic Editor will be carried over to the Advanced Editor for you.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    VBAX Regular
    Joined
    Sep 2014
    Posts
    20
    Location
    Sure, I am attaching you a file for your reference.

    Kindly help me with the VBA code
    Attached Files Attached Files

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Whoops! That is not a UserForm, it is a Worksheet. Worksheets don't have an Initialize Event, but they have an Activate Event. In order to allow the control to change state, I used the Worksheet's Change Event.

    Worksheet controls must be refered to as OLEObjects or Shapes, except for their Event Subs.

    Add these two subs to your Project in the Worksheet Code Page.
    Private Sub Worksheet_Activate()
        If (Range("A27") = "") And (Range("B28") = "") Then
            OLEObjects("Kabbadi").Enabled = False
        Else
            OLEObjects("Kabbadi").Enabled = True
        End If
    End Sub
    
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If (Range("A27") = "") And (Range("B28") = "") Then
            OLEObjects("Kabbadi").Enabled = False
        Else
            OLEObjects("Kabbadi").Enabled = True
        End If
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    VBAX Regular
    Joined
    Sep 2014
    Posts
    20
    Location
    Excellent!!!!
    That worked!!
    You have an excellent knowledge in VBA.......
    Thank you once again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •