why dim?

jb3700

New Member
Joined
Jun 8, 2015
Messages
8
would I ever need to ues or start with a dim? It always seams to work with out one. Foe example :

Sub Cake()

Dim jb As String


jb = InputBox("do you like cake? Yes/No", "CAKE")
If jb = "Yes" Or jb = "yes" Then
MsgBox "Go For It", , "GOOD"
End If

If jb = "No" Or jb = "no" Then
MsgBox "Whatever Bro", , "Go Home"
End If


End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

jb3700

New Member
Joined
Jun 8, 2015
Messages
8
yes I am aware that is to Declaring Variables, but the code works if you use the dim Dim jb As String or just use =. If i was to delete Dim jb As String and run the macro works the same.
 

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,836
Office Version
  1. 2010
Platform
  1. Windows
If i was to delete Dim jb As String and run the macro works the same.

I thought that was pretty clearly stated at the link:

By default, VBA doesn't require that you declare your variables using the Dim statement.

The article goes on to explain why you should even though it's not required.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
66,490
Office Version
  1. 365
Platform
  1. Windows
If you read the link that shg provided, it goes on to explain that if you make a typo like this:
Code:
[COLOR=#333333]jb = InputBox("do you like cake? Yes/No", "CAKE")[/COLOR]
[COLOR=#333333]If [/COLOR][COLOR=#ff0000]jd[/COLOR][COLOR=#333333] = "Yes" Or [/COLOR][COLOR=#ff0000]jd[/COLOR][COLOR=#333333] = "yes" Then[/COLOR]
[COLOR=#333333]MsgBox "Go For It", , "GOOD"[/COLOR]
[COLOR=#333333]End If[/COLOR]
your code will run without error, but it won't be doing what you expect it to (because of your typo).

If you use "Option Explicit", which forces you to declare all your variables, and you have a typo, the Compiler will catch it and alert you and not let you run the code until you fix it. So it greatly decreases your changes of typos and unexpected behavior.

Another thing not noted in that link, if we see code with no variable declarations, that is often a red flag alerts us that "this code was probably written by an amateur". You probably don't want that reputation (even if you are an "amateur").
 

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,836
Office Version
  1. 2010
Platform
  1. Windows
And have a smaller memory footprint.
 

Forum statistics

Threads
1,185,987
Messages
5,955,173
Members
438,184
Latest member
elea_acero_

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top