none
[Excel 2k3 + VBA] Move CheckBox code from UserForm to a Class Module RRS feed

  • Question

  • Hi,

    I have this code in the UserForm:

    Private Sub CheckBox1_Click()
        Select Case (CheckBox2.Value = -1)
            Case True
                CheckBox1.Value = 0
            Case False
        End Select
        Select Case (CheckBox1.Value = -1)
            Case True
                Me.CheckBox3.Enabled = True
                Me.CheckBox4.Enabled = True
            Case False
                Me.CheckBox3.Enabled = False
                Me.CheckBox4.Enabled = False
        End Select
    End Sub
    Private Sub CheckBox2_Click()
        Select Case (CheckBox1.Value = -1)
            Case True
                CheckBox2.Value = 0
            Case False
        End Select
        Select Case (CheckBox2.Value = -1)
            Case True
                MsgBox "Not a good answer", , "Please Try Again!"
            Case False
        End Select
    End Sub
    Private Sub CheckBox3_Click()
        Select Case (CheckBox4.Value = -1)
            Case True
                CheckBox3.Value = 0
            Case False
        End Select
        Select Case (CheckBox3.Value = -1)
            Case True
                Me.CheckBox5.Enabled = True
                Me.CheckBox6.Enabled = True
            Case False
                Me.CheckBox5.Enabled = False
                Me.CheckBox6.Enabled = False
        End Select
    End Sub
    Private Sub CheckBox4_Click()
        Select Case (CheckBox3.Value = -1)
            Case True
                CheckBox4.Value = 0
            Case False
        End Select
        Select Case (CheckBox4.Value = -1)
            Case True
                MsgBox "Not a good answer", , "Please Try Again!"
            Case False
        End Select
    End Sub
    


    What I want to do is have just this code in the UserForm:

    Private Sub CheckBox1_Click()
    checkbox1()
    End Sub
    Private Sub CheckBox2_Click()
    checkbox2()
    End Sub
    Private Sub CheckBox3_Click()
    checkbox3()End Sub
    End Sub
    Private Sub CheckBox4_Click()
    checkbox4()
    End Sub
    


    and the rest of the code to be in a Class Module:

    Private Sub CheckBox1()
        Select Case (CheckBox2.Value = -1)
            Case True
                CheckBox1.Value = 0
            Case False
        End Select
        Select Case (CheckBox1.Value = -1)
            Case True
                Me.CheckBox3.Enabled = True
                Me.CheckBox4.Enabled = True
            Case False
                Me.CheckBox3.Enabled = False
                Me.CheckBox4.Enabled = False
        End Select
    End Sub
    Private Sub CheckBox2()
        Select Case (CheckBox1.Value = -1)
            Case True
                CheckBox2.Value = 0
            Case False
        End Select
        Select Case (CheckBox2.Value = -1)
            Case True
                MsgBox "Not a good answer", , "Please Try Again!"
            Case False
        End Select
    End Sub
    Private Sub CheckBox3()
        Select Case (CheckBox4.Value = -1)
            Case True
                CheckBox3.Value = 0
            Case False
        End Select
        Select Case (CheckBox3.Value = -1)
            Case True
                Me.CheckBox5.Enabled = True
                Me.CheckBox6.Enabled = True
            Case False
                Me.CheckBox5.Enabled = False
                Me.CheckBox6.Enabled = False
        End Select
    End Sub
    Private Sub CheckBox4()
        Select Case (CheckBox3.Value = -1)
            Case True
                CheckBox4.Value = 0
            Case False
        End Select
        Select Case (CheckBox4.Value = -1)
            Case True
                MsgBox "Not a good answer", , "Please Try Again!"
            Case False
        End Select
    End Sub
    

     

    How do I do, please, please?

     

    Regards


    • Edited by Admin-Dev Friday, December 16, 2011 4:59 PM refine
    Friday, December 16, 2011 4:58 PM

Answers

  • You need to change your moved code to address the fully qualified object: All instances of, for example

    CheckBox4.Value

    need to be

    UserFormName.CheckBox4.Value

    (that is, if they are direcly located on the userform and not, say, a frame)

    You could also write the code so that you pass the userform as an object, so that the code could be shared between different userforms.

     


    HTH, Bernie
    • Marked as answer by Admin-Dev Friday, December 16, 2011 9:32 PM
    Friday, December 16, 2011 5:12 PM

All replies

  • You need to change your moved code to address the fully qualified object: All instances of, for example

    CheckBox4.Value

    need to be

    UserFormName.CheckBox4.Value

    (that is, if they are direcly located on the userform and not, say, a frame)

    You could also write the code so that you pass the userform as an object, so that the code could be shared between different userforms.

     


    HTH, Bernie
    • Marked as answer by Admin-Dev Friday, December 16, 2011 9:32 PM
    Friday, December 16, 2011 5:12 PM
  • Thank you, it really help me making the code work!

    actually I was in need for just a module not a class module...

    Friday, December 16, 2011 9:32 PM