Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 636
  • Last Modified:

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!
0
JKCC
Asked:
JKCC
4 Solutions
 
z_alexCommented:
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
 
JPIT DirectorCommented:
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
 
QlemoC++ DeveloperCommented:
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
 
etech0Commented:
VBA for Dummies is a great book.
0
 
JKCCAuthor Commented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now