Solved

VBA, Dim, Module, Objects etc...

Posted on 2010-08-15
5
626 Views
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!
0
Comment
Question by:JKCC
5 Comments
 
LVL 1

Assisted Solution

by:z_alex
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 -> 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
0
 
LVL 14

Assisted Solution

by:peetjh
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.
http://www.mrexcel.com/store/index.php?l=product_detail&p=186
0
 
LVL 69

Accepted Solution

by:
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).
But
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.
0
 
LVL 10

Assisted Solution

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

Author Closing Comment

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

Cheers all!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

785 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