Module Variables

gripper

Board Regular
Joined
Oct 29, 2002
Messages
176
I have the below code that iterates correctly. My issue is setting the module variables so that I know where this code ended and to set the variables so when I run another Sub it will use the module variables at the top as the starting point. I am not sure if these are getting set and read properly.

The first code is the one that runs initially and is SUPPOSE to set the ending cell as the sub ends The second sub is suppose to read in the module variable and set a start point from that location. I have not used global type variables in Excel and I think that is my issue.

Thank you

VBA Code:
Dim lastStartCell As Range
Dim lastEndCell As Range

Sub ColorCells()
    Dim numIterations As Integer
    numIterations = InputBox("How many iterations do you want to perform?")
    
    Dim startCell As Range
    Set startCell = Range("B2")
    
    Dim i As Integer
    For i = 1 To numIterations
        startCell.Offset((i - 1) * 1, (i - 1) * 1).Resize(4).Interior.Color = RGB(255, 0, 0)
    Next i
    
    'set variables for start and end cells of the last bar
    Set lastStartCell = startCell 
    Set lastEndCell = startCell.Offset(4, 0)
    
End Sub

Sub NextColorRowsx()
    Dim numIterations As Integer
    numIterations = InputBox("How many iterations do you want to perform?")
    
    Dim startCell As Range
    Set startCell = lastStartCell
    
    Dim i As Integer
    For i = 1 To numIterations
        startCell.Offset((i - 1) * 1, (i - 1) * 1).Resize(4).Interior.Color = RGB(255, 0, 0)
    Next i
    
    'set variables for start and end cells of the last bar
   ' Set lastStartCell = startCell.Offset(0, 0)
   ' Set lastEndCell = startCell.Offset(4, 0)
    
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
There really isn't a question there?
If the subs are not in the same module then the variables are out of scope because if scope is not explicitly declared it defaults to Private. Thus scope is limited to the module code contained in the same module only. If the subs are in the same module then step through (F8) the code and check variables after code lines that affect them have executed.
 
Upvote 0
A minute late :) I believe the question would be how to... Declare as 'Public' and they should retain their values until `End` either through code or VBA IDE.
 
Upvote 0

Forum statistics

Threads
1,215,214
Messages
6,123,664
Members
449,114
Latest member
aides

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