VBA, Dim, Module, Objects etc...

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!
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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 -> http://msdn.microsoft.com/en-us/library/tyd4tfw8%28VS.80%29.aspx

for Excel Programming try this link -> http://www.excel-vba.com/excel-vba-contents.htm
JPIT DirectorCommented:
I purchased the earlier version of this book not 2010 fom mrexcel and it was an excellent resource when I started.
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VBA for Dummies is a great book.
JKCCAuthor Commented:
So many responses and different bits of information....thankyou for the tips all and for the detailed response too Qlemo....

Cheers all!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.