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
Comment Utility
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
Comment Utility
I purchased the earlier version of this book not 2010 fom mrexcel and it was an excellent resource when I started.
LVL 68

Accepted Solution

Qlemo earned 375 total points
Comment Utility
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
Comment Utility
VBA for Dummies is a great book.

Author Closing Comment

Comment Utility
So many responses and different bits of information....thankyou for the tips all and for the detailed response too Qlemo....

Cheers all!

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This script will sweep a range of IP addresses (class c only, and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now