VBA, Dim, Module, Objects etc...

Posted on 2010-08-15
Last Modified: 2012-05-10

Hi Experts,

As you can tell I'm asking very basic questions because I have recently started to expand my excel knowledge by using VBA. I have no programming background at all and have started to use the macro recorder, adjust other's code to suit my needs or have had much help with excellent code written specifically for me which I have applied but also want to understand it better.

Can someone please explain to me about Dim, modules and objects. I have seem some code written without any Dim statements at the beginning and others which have it. I want to further my understanding so that I now when it needs to be applied and when it doesn't. Also, why are some macros written under the specific sheets e.g. sheet1 and others under modules? Does it matter and is a private module only for that specific worksheet or the whole workbook etc? Also, are worksheets and ranges objects?

Can you recommend a good book or website that puts it in simple words, but has samples of vba that might be used everyday in an office environment? Sorry, for the many questions and hopefully this type of question is allowed to be posted on here. It's not a programming type question, but I figure as your the expert its probably best to ask here because I don't know anyone personally.

Thankyou in advance!
Question by:JKCC

Assisted Solution

z_alex earned 75 total points
ID: 33440193
Dim - Declares and allocates storage space for one or more variables.
Modules - Where sets of function and procedures that can be reusable in others forms or your function library.
Objects - Where you can find like text, buttons, listbox, etc..
Try to check this link for more detailed regarding objects ->

for Excel Programming try this link ->
LVL 14

Assisted Solution

peetjh earned 25 total points
ID: 33440209
I purchased the earlier version of this book not 2010 fom mrexcel and it was an excellent resource when I started.
LVL 69

Accepted Solution

Qlemo earned 375 total points
ID: 33440291
First, you are absolutely fine to post such a question here.

DIM is used to specify the usuage and type of a variable. You do not need that, if you haven't setup OPTION EXPLICIT, which forces you declare each var used. But it is good practice and reduces mistakes (typos, wrong usage) if you declare your vars explicitely.
When using arrays, DIM is always needed to declare the index range (and hence size).
For example, you can do
DIM obj
which is a very generic declaration - obj is something, and can take values of all kinds (it is a
variant, a generic type).
DIM str as String
allows only strings to be assigned to that var. If you try to assign anything else (e.g. an object), a compiler error will result.
Further, if you have this:
OPTION EXPLICITDIM str as Stringstrr = "Just a string"
you get an error because the var strr is not declared yet. Obviously that was a typo, and you would not have seen why your code does not work without the OPTION EXPLICIT.
Ranges and Worksheets and .... are objects. Worksheets are precisely collections, what means they are dynamically allocated arrays containing several objects. Hence you can do a
? WorkSheets.Count
(number of worksheets used) but not
? WorkSheets.Address
because there is no address of all worksheets.
? WorkSheets("Sheet 1").Name
will report the name of that specific worksheet. As you can see you simply put either an numeric index or a known name in the brackets of a collation to address some specific contents.
The concept of addressing and using Worksheets and Range, Cells aso. in Excel is quite complex, because it is very versatile. I don't expect you to fully understand the very short explanation above ;-).
About the concept of modules: A module is a part of VB code isolated from any object. It is only part of the project, which is again part of the application (Excel in this case). A module is "global" to all sheets and other objects. If you run code in a module, you need to fully reference the sheet you want to act on.
Code running in a specific sheet is applied to that sheet, so you do not need to reference it. But the code is not restricted to the sheet, by fully addressing other sheets it has access to them, too.
So the difference is not that big between code in modules and sheets. But code in sheets is copied if you copy the sheet. You usually put code into the sheet which is strictly bound to that sheet, and not needed elsewhere.
I cannot give you good advice about VBA beginners books, sorry. But if you can find a "VBA for Dummies" kind, you certainly should take that into consideration.
LVL 10

Assisted Solution

etech0 earned 25 total points
ID: 33440470
VBA for Dummies is a great book.

Author Closing Comment

ID: 33440588
So many responses and different bits of information....thankyou for the tips all and for the detailed response too Qlemo....

Cheers all!

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  ( Here (http…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question