Setting a variable macro range - tricky???
Posted by Chris C on November 23, 2001 10:39 AM
I'm trying to run a macro that runs a subroutine using a user defined range.
The entire range is January 1994 to October 2001.
The user enters a starts date (say January 1995) and an end date (say October 2000).
Spreadsheet then trims the value into two components - month ranges from 1 to 12 and date ranges from 94 to 101 (100 = yr 2000, 101 = yr 2001).
So January 1995 would return a 01 for the month and 95 for the year - October 2000 would return a 10 for the month and 100 for the year
Now, assuming that I don't allow the user to define the range (it is set for Jan 94 to Oct 2001), then my macro is defined as follows:
DIM Month, Year, Check, Check2, i as Integer
DIM Months, Years as String
i = 1
For YEAR = 94 To 101
CHECK = 1
CHECK2 = 12
If YEAR = 101 Then
CHECK = 1
CHECK2 = 10
End If
For MONTH = CHECK To CHECK2
MONTHS = MONTH
YEARS = YEAR
If YEAR = 100 Then
YEARS = "00"
End If
If YEAR = 101 Then
YEARS = "01"
End If
If MONTH < 10 Then
MONTHS = "0" & MONTH
End If
Range("N" & I).Select
ActiveCell = "='" & MONTHS & "-" & YEARS & "'!R223C127]"
I = I + 1
Next MONTH
Next YEAR
So what this does, is from the Summary page, starting at Column N, row 1, it returns the value located at EK223 for each month from Jan 94 to Oct 01.
So if you highlighted say N6, this would correspond with June 1994 so the formula reads ='06-94'!EK$223.
So what I want to do is let the user control the range for the months (check to check2) and the years (which I guess I will have to set up new variables).
So the main question is how can I set a variable to equal a value in a cell on the worksheet?
So if the range was Jan 95 - Oct 2000, then it would create the formula in column N which references to sheets "01-95" through to "10-00".
Any help would be appreciated!