Loading
Ozgrid Excel Help & Best Practices Forums

Excel Training / Excel Dashboards Reports



Results 1 to 9 of 9

Thread: VBA : creating a new class...

  1. #1
    Join Date
    12th December 2003
    Posts
    714
    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...!

    Excel Video Tutorials / Excel Dashboards Reports


  2. #2
    Join Date
    13th September 2003
    Location
    Singapore
    Posts
    1,236
    hi mjschukas

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

    I'm not sure what you mean.

    cheers
    xlite

    Excel Video Tutorials / Excel Dashboards Reports


  3. #3
    Join Date
    7th March 2003
    Location
    Essex, England
    Posts
    11,310
    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

    Excel Video Tutorials / Excel Dashboards Reports


  4. #4
    Join Date
    12th December 2003
    Posts
    714
    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

    Excel Video Tutorials / Excel Dashboards Reports


  5. #5
    Join Date
    7th March 2003
    Location
    Essex, England
    Posts
    11,310
    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

    Excel Video Tutorials / Excel Dashboards Reports


  6. #6
    Join Date
    12th December 2003
    Posts
    714
    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

    Excel Video Tutorials / Excel Dashboards Reports


  7. #7
    Join Date
    7th March 2003
    Location
    Essex, England
    Posts
    11,310
    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

    Excel Video Tutorials / Excel Dashboards Reports


  8. #8
    Join Date
    12th December 2003
    Posts
    714
    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

    Excel Video Tutorials / Excel Dashboards Reports


  9. #9
    Join Date
    7th March 2003
    Location
    Essex, England
    Posts
    11,310
    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

    Excel Video Tutorials / Excel Dashboards Reports


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Possible Answers

  1. Creating A Custom Collection Class
    By vonWerra in forum EXCEL HELP
    Replies: 2
    Last Post: June 2nd, 2010, 17:22
  2. Class module is collection of other class
    By james.aldcroft in forum EXCEL HELP
    Replies: 7
    Last Post: May 25th, 2006, 07:20
  3. Creating a class ( object )
    By iwrk4dedpr in forum EXCEL HELP
    Replies: 5
    Last Post: January 20th, 2006, 06:16
  4. Creating a Properties property of a class
    By A9192Shark in forum Excel and/or Access Help
    Replies: 14
    Last Post: December 9th, 2004, 18:49

Bookmarks

Posting Permissions

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