Coding Practices

Uziel

New Member
Joined
Jun 10, 2011
Messages
7
Hello All, in Excel I declare my variables at the top of my sheet to avoid having to recreate them over and over. Is that a bad practice for any reason?

Thank you!

Code:
Option Explicit
Option Base 1

'Application
Dim aCS As Long
'Objects
Dim oWB As Workbook
Dim oWS As Worksheet
Dim oPT As PivotTable
Dim oPI As PivotItem
Dim oPIL As PivotItemList
Dim oPIs As PivotItems
Dim oComAddin As COMAddIn
Dim oComAddins As COMAddIns
Dim oAddin As AddIn
Dim oAddins As AddIns
'Ranges
Dim rRange As range
Dim rCell As range
Dim rUR As range
Dim rRow As range
Dim rCol As range
'Long Numbers
Dim i As Long
Dim j As Long
Dim lCS As Long
Dim lCalcMode As Long
'Strings
Dim sTemp As String
'Booleans
Dim bCS As Boolean
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Unless those values need to persist when no code is running, it's better practice to either declare variables locally or receive them as arguments, as appropriate.

Reusing ordinary scalar variable requires that you reset them prior to usage, whereas locally declared variables are created on entry to the procedure and initialized to default values.

IMO, module-level and global variables should be used only after you're convinced your skeptical self that it's the best way for each one.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,855
Members
452,948
Latest member
UsmanAli786

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