VBA Variable Help (Newbie Question)

helpneeded2

Board Regular
Joined
Jun 25, 2021
Messages
110
Office Version
  1. 365
Platform
  1. Windows
I only have a very surface level understanding of VBA.

I cannot figure out why this is generating an error:

VBA Code:
Dim DateStart As Date
Dim DateEnd As Date
DateStart = ThisWorkbook.Sheets("Sheet1").Range("D4").Value
DateEnd = ThisWorkbook.Sheets("Sheet1").Range("D6").Value

Sub RetrieveData()

    If DateStart = True Or DateEnd = True Then
        MsgBox "Date must be entered for both the Start Date and the End Date!"
        Else
        If DateStart > DateEnd Then
            MsgBox "The End Date must be greater than the Start Date!"
            Else
            MsgBox "Good to Go"
        End If
    End If
    
End Sub

When I try executing this, I get a Compile error message: Invalid outside procedure

Could someone please let me know what I am doing wrong.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Upon some light reading, you can declare variables outside of a Sub, but you cannot set their values outside of the sub.
 
Upvote 0
Upon some light reading, you can declare variables outside of a Sub, but you cannot set their values outside of the sub.
That is correct. You can declare public variables outside of any procedures, but you cannot set them outside of any procedures.
By declaring them outside of the procedure, it ensures that they maintain their value during that Excel session and can be used in multiple procedures.
 
Upvote 0
The only way you can declare a value outside of a sub is as a Constant (a value that does not change) but not as a variable (a value that can change)

This works because it has a single nonchanging (constant) value:
VBA Code:
Public Const DateStart = True

this would not because it refers to a cell which could potentially change
VBA Code:
Public Const DateStart = ThisWorkbook.Sheets("Sheet1").Range("D4").Value
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

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
Back
Top