What are Class Modules

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,030
Through sheer stubbornness and a bit of dumb luck I figured out how to use a class module to disable my custom buttons in my toolbars if I close all workbooks and then enable them if one or more is open. But I'd really like to learn more. I saw another message posted where Brett asked about Class Modules. Mr. Mark O'Brien's response to Brett was that a proper explanation would be too long for this forum. Could one of you old pro's recommend either a book or two or else a couple of websites with good information on Class Modules?

Thank you,
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Greg. Can't really answer your question, I don't know much about Class modules myself.
I am curious about your code for your custom buttons though. Could you please send me a copy. I have a lot of custom buttons I would like to grey out when they aren't needed.
Thanks Mike
 
Upvote 0
Glad to; this may not be the most elegant solution. I just got annoyed enough to trying until I could figure out a way to get my custom buttons to disable like the built-ins do.

In my Personal.xls wb in the declarations section I put:

Dim MyXL As New ApplicationClassModule

and

Private Sub Workbook_Open()
Set MyXL.MyApp = Application
End Sub

In my Personal wb I added a class module named ApplicationClassModule

Within that one I have:

Public WithEvents MyApp As Application

Private Sub MyApp_WindowActivate(ByVal Wb As Excel.Workbook, ByVal Wn As Excel.Window)
On Error Resume Next
If Application.Windows.Count >= 2 Then
CommandBars("CustomSelection").Controls("Select Column Header").Enabled = True
CommandBars("CustomSelection").Controls("Select Col No Header").Enabled = True
End If

Private Sub MyApp_WindowDeactivate(ByVal Wb As Excel.Workbook, ByVal Wn As Excel.Window)
On Error Resume Next
If Application.Windows.Count <= 2 Then
CommandBars("CustomSelection").Controls("Select Column Header").Enabled = False
CommandBars("CustomSelection").Controls("Select Col No Header").Enabled = False
End If
 
Upvote 0
Here's a couple of articles to study:

http://www.microsoft.com/officedev/articles/movs109.htm

http://www.vb-world.net/books/vb6objects/sample7.html

Danger, Danger! Opinions Ahead!
(Coming from somebody who hasn't done a lot of Class programming)



Okay, let's say that you are deep into a project, the basics are in place, the program is actually 'working'. Have you created a ton of variables that each seem to have the same phrase in them?

Let's say you have created a program to assist you on picking the guys you hang with on Friday night.

You might be checking your friends for:

Do I owe them money?
Do they owe me money?
Am I currently dating their ex-girlfriend?
Do they work Friday night?
What time do they get off, if they do work Friday night?
Are they currently grounded, or in jail?
Is their car running?
Have their parents forbidden them to associate with you?
Don't forget a rating, from 0-to 10.
Might as well keep their phone number(s) in the database...

You could find yourself with:
Code:
Dim JoeOwesMe as Boolean
Dim EddyOwesMe as Boolean
Dim JohnnieOwesMe as Boolean
Dim SamOwesMe as Boolean

Dim DatingJoesEx as Boolean
Dim DatingEddysEx as Boolean
Dim DatingJohnniesEx as Boolean
Dim DatingSamsEx as Boolean

Sub IM_MyBuddy(ThisBuddy as Integer)
....
....
.....
End Sub

So, without class, you would need to hard-code each friend's values; adding a friend would entail a lot of code. A bunch of arrays could also work, but you would have to work at tying them together.


However.......

With a MyFriends object, you could do things like:

Set Eddy as New MyFriends
Eddy.InitializeDataBase

With Eddy
If .NotGrounded And Not .OweHimMoney And Not .DatingHisEx Then

.Send_IM_AndAskWhatHesDoingFridayNight
If .NotBusyFridayNight Then

.AddToList
End If
End With


Now, before you take my advice as gospel, let's see what the others have to say about this subject.

Okay guys, let's hear from you all.
 
Upvote 0

Forum statistics

Threads
1,222,682
Messages
6,167,616
Members
452,124
Latest member
lozdemr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top