In Excel: Dim declares a variable: Dim customerName As String creates a text variable. Declare each variable with its own As clause. The displayed Macro1 code is a scaffold, not a data-processing solution. Excel for the web cannot create, run, or edit VBA macros. Open the workbook in the Excel desktop app to work with macros.
On this page8
VBA macro: Dim
Sub Macro1()
' dim — replace the body with your steps
Dim ws As Worksheet
Set ws = ActiveSheet
' ... your code here ...
End SubHow to run this macro
- Press Alt + F11 to open the VBA editor.
- Insert > Module.
- Paste the code above.
- Press
F5, or close the editor and run it from Developer > Macros. - Save the file as .xlsm so the macro is kept.
Identify the variable name and required data type.
Give each declared variable its own As clause.
Check whether the declaration belongs inside a procedure or at module level.
Assign an object reference with Set before using that object variable.
What this does
A declaration specifies a variable name and type. Inside a procedure its scope is that procedure; at module level it is available within the module. Dim ws As Worksheet declares an object variable, while Set ws = ActiveSheet assigns its worksheet reference. Declaring ws does not select, read or change a cell range. Read the code once before running it: note which sheets, ranges and files it touches and whether it deletes or overwrites anything. Macros run only in desktop Excel with macros enabled, and the code is kept only in a macro-enabled format such as .xlsm. The difference between a quick fix and a sheet you can trust is the extra minute you spend validating “dim in excel vba”. Start on a copy or a tiny sample, keep the affected cells visible, and compare the result with the outcome you expected before you touch the real workbook. When this is a ribbon command, the selection matters more than the button: confirm the range, apply the command, then spot-check the output before saving. The point is a workflow that saves repeating the same clicks every week, but the practical win is that someone else can open the file and understand what happened without asking you.
A worked example
Compare Dim firstName As String, lastName As String with Dim firstName, lastName As String. In the second statement only lastName is String; firstName is Variant. Inspect the displayed scaffold: its Worksheet declaration and Set assignment do not process rows or produce worksheet output. Explicit declarations make the intended type and scope visible before you add worksheet operations. Run a new macro on a copy of the workbook first. Do not count on Undo to reverse what a macro changed; the saved copy is your way back.
In Google Sheets
Google Sheets does not run VBA. A macro written for Excel has to be rewritten in Google Apps Script (Extensions ▸ Apps Script), and an .xlsm file converted to Sheets keeps its cells but drops the code. Keep this page bookmarked for the next time the same question comes up. Better still, repeat the steps once in your own workbook — doing it yourself is what turns a copied answer into something you remember. For “dim in excel vba”, the checks are about the environment as much as the code: desktop Excel with macros enabled for this file, a macro-enabled workbook and a saved copy to fall back on. Run it once on test data, compare the result with what the code says it does, and only then run it on the real sheet.
Common mistakes
- Assuming one As String types every name on the line.
- Confusing a variable declaration with a worksheet edit.
- Running an unfinished scaffold as though it implemented a cell-range task.
Frequently asked questions
Does Dim assign a worksheet?
Dim ws As Worksheet declares the type. Set ws = ActiveSheet assigns the reference.
What happens without an As type?
Without a type declaration or type character, the variable is Variant.
Does this scaffold calculate a result?
No. Its worksheet variable is assigned, but the task body still needs an implementation.
Other ways people ask this
People reach this page typing “dim in vba excel” and “excel vba dim”, among other phrasings; whichever wording you used, the fix above is the one you want.
Why do people search for this in so many different ways?
Because the same task has many names. “dim in vba excel”, “excel vba dim” all point at the one operation explained on this page, which is why they all lead here.