Announcement

Collapse
No announcement yet.

VBA : creating a new class...

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • VBA : creating a new class...

    i need some help in creating a class in excel...

    (this is new to me and i'd just like a simple example...)

    thank you...!

  • #2
    hi mjschukas

    can you elaborate furhter what you mean by "creating a class"?

    I'm not sure what you mean.

    cheers
    xlite
    Cheers
    ___________
    Xlite
    All you need to learn VBA is an internet connection and Ozgrid.com

    Comment


    • #3
      Hi mjschukas,

      Take a look at the link XL-Dennis posted.

      http://www.ozgrid.com/forum/viewthread.php?tid=8032

      If you still need help, then as xlite suggests, post back with a bit more information.

      Cheers
      Andy

      Cheers
      Andy

      Comment


      • #4
        thank you.

        good reading and would you have something more simple to start with...?

        thank you.

        Originally posted by Andy Pope
        Hi mjschukas,

        Take a look at the link XL-Dennis posted.

        http://www.ozgrid.com/forum/viewthread.php?tid=8032

        If you still need help, then as xlite suggests, post back with a bit more information.

        Cheers
        Andy

        Comment


        • #5
          Here is a little example I just created.
          Not sure if it is going to be any clearer that the extract you just read.

          This goes in a standard code module
          Code:
          Sub DemoClass()
              Dim objPeople As clsPerson
              Dim strBuf As String
                  
              Set objPeople = New clsPerson
              With objPeople
                  .Name = "John Doe"
                  .Male = True
                  .Married = False
                  If .Married Then
                      strBuf = .Title & " " & .Name & " is a happily married " & .Sex
                  Else
                      strBuf = .Title & " " & .Name & " is an available " & .Sex
                  End If
              End With
              MsgBox strBuf, vbInformation
              With objPeople
                  .Name = "Joanne Doe"
                  .Female = True
                  .Married = True
                  If .Married Then
                      strBuf = .Title & " " & .Name & " is a happily married " & .Sex
                  Else
                      strBuf = .Title & " " & .Name & " is an available " & .Sex
                  End If
                  MsgBox strBuf, vbInformation
                  .GetDivorce
                  If .Married Then
                      strBuf = .Title & " " & .Name & " is a happily married " & .Sex
                  Else
                      strBuf = .Title & " " & .Name & " is an available " & .Sex
                  End If
                  MsgBox strBuf, vbExclamation
              End With
          End Sub
          This goes in a standard class module.
          Name if clsPerson.
          Code:
          Public Name As String
          Private m_blnMarried As Boolean
          Private m_blnSex As Boolean  ' MALE=FALSE FEMALE=TRUE
          Public Function Title() As String
              If m_blnSex Then
                  If m_blnMarried Then
                      Title = "Mrs"
                  Else
                      Title = "Miss"
                  End If
              Else
                  Title = "Mr"
              End If
          End Function
          Public Property Get Sex() As String
          'Read only
              If m_blnSex Then
                  Sex = "Female"
              Else
                  Sex = "Male"
              End If
          End Property
          Public Property Let Male(IsMale As Boolean)
          ' Write Only
              If IsMale Then m_blnSex = False
          End Property
          Public Property Let Female(IsFemale As Boolean)
          ' Write Only
              m_blnSex = IsFemale
          End Property
          Public Sub GetDivorce()
              If m_blnMarried Then m_blnMarried = False
          End Sub
          Public Property Let Married(IsMarried As Boolean)
          ' Write
              m_blnMarried = IsMarried
          End Property
          Public Property Get Married() As Boolean
          ' Read
              Married = m_blnMarried
          End Property
          Hopefully it demonstrates the use of;
          Public variables that are read/write
          Read/write properties
          Readonly property
          Writeonly property
          Function
          Subroutine

          If you have a more specific question or can provide more detail of the problem you are trying to tackle then post back.

          Cheers
          Andy

          Cheers
          Andy

          Comment


          • #6
            thank you...

            and now, how do i invoke this code that i placed

            Originally posted by Andy Pope
            Here is a little example I just created.
            Not sure if it is going to be any clearer that the extract you just read.

            This goes in a standard code module
            Code:
            Sub DemoClass()
                Dim objPeople As clsPerson
                Dim strBuf As String
                    
                Set objPeople = New clsPerson
                With objPeople
                    .Name = "John Doe"
                    .Male = True
                    .Married = False
                    If .Married Then
                        strBuf = .Title & " " & .Name & " is a happily married " & .Sex
                    Else
                        strBuf = .Title & " " & .Name & " is an available " & .Sex
                    End If
                End With
                MsgBox strBuf, vbInformation
                With objPeople
                    .Name = "Joanne Doe"
                    .Female = True
                    .Married = True
                    If .Married Then
                        strBuf = .Title & " " & .Name & " is a happily married " & .Sex
                    Else
                        strBuf = .Title & " " & .Name & " is an available " & .Sex
                    End If
                    MsgBox strBuf, vbInformation
                    .GetDivorce
                    If .Married Then
                        strBuf = .Title & " " & .Name & " is a happily married " & .Sex
                    Else
                        strBuf = .Title & " " & .Name & " is an available " & .Sex
                    End If
                    MsgBox strBuf, vbExclamation
                End With
            End Sub
            This goes in a standard class module.
            Name if clsPerson.
            Code:
            Public Name As String
            Private m_blnMarried As Boolean
            Private m_blnSex As Boolean  ' MALE=FALSE FEMALE=TRUE
            Public Function Title() As String
                If m_blnSex Then
                    If m_blnMarried Then
                        Title = "Mrs"
                    Else
                        Title = "Miss"
                    End If
                Else
                    Title = "Mr"
                End If
            End Function
            Public Property Get Sex() As String
            'Read only
                If m_blnSex Then
                    Sex = "Female"
                Else
                    Sex = "Male"
                End If
            End Property
            Public Property Let Male(IsMale As Boolean)
            ' Write Only
                If IsMale Then m_blnSex = False
            End Property
            Public Property Let Female(IsFemale As Boolean)
            ' Write Only
                m_blnSex = IsFemale
            End Property
            Public Sub GetDivorce()
                If m_blnMarried Then m_blnMarried = False
            End Sub
            Public Property Let Married(IsMarried As Boolean)
            ' Write
                m_blnMarried = IsMarried
            End Property
            Public Property Get Married() As Boolean
            ' Read
                Married = m_blnMarried
            End Property
            Hopefully it demonstrates the use of;
            Public variables that are read/write
            Read/write properties
            Readonly property
            Writeonly property
            Function
            Subroutine

            If you have a more specific question or can provide more detail of the problem you are trying to tackle then post back.

            Cheers
            Andy

            Comment


            • #7
              You can either run the DemoClass form the macro dialog ALT+F8 or from within the VBE press F5.

              I saying the following with the best intentions and without trying to patronising you.

              Classes in vba are what I would consider to be part of the 'advanced techinques', certainly not something to start with.
              If you have problems understanding where to put code and run it then maybe you should take a step back and begin by learning some fundementals first.
              Try a book or one of Dave's courses.

              Please except this advice in the good sprit it is offered.

              Cheers
              Andy

              Cheers
              Andy

              Comment


              • #8
                i know enough to write code...but not to use the oop to it's true extent...

                so, i'm ok to run the code, but in the excel vba editor, i just get an empty dialog box...when i F5 (run)...

                thank you...! (trying to figure out this class stuff...


                Originally posted by Andy Pope
                You can either run the DemoClass form the macro dialog ALT+F8 or from within the VBE press F5.

                I saying the following with the best intentions and without trying to patronising you.

                Classes in vba are what I would consider to be part of the 'advanced techinques', certainly not something to start with.
                If you have problems understanding where to put code and run it then maybe you should take a step back and begin by learning some fundementals first.
                Try a book or one of Dave's courses.

                Please except this advice in the good sprit it is offered.

                Cheers
                Andy

                Comment


                • #9
                  Hi mjschukas,

                  I just copied the code into a code and class module as sepcified.
                  When i hit F5 the DemoClass is available in the Macros dialog.

                  Try going to the Democlass routine and then press F5.

                  Cheers
                  Andy

                  Cheers
                  Andy

                  Comment

                  Working...
                  X