Declaring variables - Dimm'ing :)

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
437
Hi there... just a newbie getting my feet wet with programming in excel. I have a lot of declarations and I'm sure I can be declaring them in a better fashion. Can someone provide a more concise alternative to the following:

Code:
Dim MyChart As Chart
Dim ChartData1 As Range
Dim ChartData2 As Range
Dim ChartData3 As Range
Dim ChartData4 As Range
Dim ChartData5 As Range
Dim ChartData6 As Range
Dim ChartData7 As Range
Dim ChartData8 As Range
Dim ChartData9 As Range
Dim ChartIndex1 As Integer
Dim ChartIndex2 As Integer
Dim ChartIndex3 As Integer
Dim ChartIndex4 As Integer
Dim ChartIndex5 As Integer
Dim ChartIndex6 As Integer
Dim ChartIndex7 As Integer
Dim ChartIndex8 As Integer
Dim ChartIndex9 As Integer
Dim ChartIndex16 As Integer
Dim ChartName1 As String
Dim ChartName2 As String
Dim ChartName3 As String
Dim ChartName4 As String
Dim ChartName5 As String
Dim ChartName6 As String
Dim ChartName7 As String
Dim ChartName8 As String
Dim ChartName9 As String
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I don't think that there is much you can do with it as in VBA unlike some other languages you have to explicitly declare each variable.

You could do something like the below but doesn't save much.
Code:
Dim MyChart As Chart
Dim ChartData1 As Range, ChartData2 As Range, ChartData3 As Range
Dim ChartData4 As Range, ChartData5 As Range, ChartData6 As Range
Dim ChartData7 As Range, ChartData8 As Range, ChartData9 As Range
Dim ChartIndex1 As Integer, ChartIndex2 As Integer, ChartIndex3 As Integer
Dim ChartIndex4 As Integer, ChartIndex5 As Integer, ChartIndex6 As Integer
Dim ChartIndex7 As Integer, ChartIndex8 As Integer, ChartIndex9 As Integer
Dim ChartIndex16 As Integer
Dim ChartName1 As String, ChartName2 As String, ChartName3 As String
Dim ChartName4 As String, ChartName5 As String, ChartName6 As String
Dim ChartName7 As String, ChartName8 As String, ChartName9 As String

and you could change all the Integers to Longs as Integers are only long integers in modern systems which saves 3 letters per Integer variable
 
Upvote 0
Not sure if this is a good idea or not, but you can Dim variables like this
Code:
Sub dimtest()

Dim UsdRws(3) As Long

    UsdRws(1) = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
    UsdRws(2) = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
    UsdRws(3) = Sheet3.Range("A" & Rows.Count).End(xlUp).Row
MsgBox UsdRws(1) & vbLf & UsdRws(2) & vbLf & UsdRws(3)


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

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