New-ish guy back again. Will someone in plain English tell me what is DIM in VBA and why do you use it?
New-ish guy back again. Will someone in plain English tell me what is DIM in VBA and why do you use it?
DIM is short for Dimension, you use it to declare variables.
Hope that helps.
RoyUK
--------
For Excel Tips & Solutions, free examples and tutorials why not check out my web site
Free DataBaseForm example
And declaring variables allows the programming to reserve memory for the information, to know how much memory to reserve, and to know what type of information that variable will hold. This will allow the programming to use memory and processor time more efficiently, causing your code to run faster.
Thanks now I know what they are. How are they used? Or where can I go to learn more about how to use them?
Also, by declaring Option Explicit at the top of each module, you will be forced to declare variables, which helps a lot. For instance, with the same code as the previous post:![]()
Option Explicit Sub SeeDim() Dim aRng As Range, oC As Range Dim aInt As Integer Dim aLong As Long Dim aWorksheet As Worksheet aInt = 5 aLong = 10 Set aWorksheet = Worksheets("Sheet1") Set aRng = aWorksheet.Range("B1:B5") For Each oC In aRng oC.Value = aLong oC.Offset(0, 1).Value = aInt Next oC End Sub
If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.
---Keep on Coding in the Free World---
The following code declares variablesWhat I find to be most valuable about declaring variables is that it helps to enable the intellisense while coding. If you want to test the intellisense, re-write the code I gave line by line and you'll see what i mean.![]()
Sub SeeDim() Dim aRng As Range, oC As Range Dim aInt As Integer Dim aLong As Long Dim aWorksheet As Worksheet aInt = 5 aLong = 10 Set aWorksheet = Worksheets("Sheet1") Set aRng = aWorksheet.Range("B1:B5") For Each oC In aRng oC.Value = aLong oC.Offset(0, 1).Value = aInt Next oC End Sub
There's some notes that I put together here
Thanks everyone. This really helps me understand dim and how to use it. This is awesome.
If you are satisfied with the solution(s) provided, please mark your thread as Solved.
New quick method:
Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.
Or you can use this way:
How to mark a thread Solved
Go to the first post
Click edit
Click Go Advanced
Just below the word Title you will see a dropdown with the word No prefix.
Change to Solved
Click Save
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks