macro- variable-cell reference hell


Posted by suz on August 14, 2001 9:04 AM

I have a variable "m1" which references cell A1, and then loops back. How can I make "m1" then reference cell B1, C1, etc.?

Posted by Francis on August 14, 2001 7:36 PM

What is it you are trying to do? Would help if you could post the code you already have.



Posted by suz on August 15, 2001 7:04 AM

Well, what I have is obviously wrong, but here it is. Everything runs fine through the inner loop once, then stops after it filters using the new value for m2, which should result in a number of records, but actually doesn't return any. I assume that's because I can't rename m2 the way that I did. Is there another way to do that so that I loop through using each of the next 5 cells on my worksheet?

Sorry - I'm a macro amateur, so I hope this isn't too pathetic.

Dim m1 As Range 'main loop date
Set m1 = Worksheets("summary").Range("c2")
Dim m3 As Range 'end date
Set m3 = Worksheets("summary").Range("h2")
Dim c1 As Integer
For c1 = 1 To 6 Step 1
Do Until m1 = m3
Sheets("table").Select
Selection.AutoFilter
Selection.AutoFilter field:=2, Criteria1:="m1"
Dim r As Integer 'row counter
For r = 0 To 64 Step 3
Dim m2 As Range
Set m2 = Worksheets("SUMMARY").Range("C2")
Do Until m2 = m3
Dim c As Integer 'column counter
For c = 1 To 6 Step 1
'gear yes, lighting no
Selection.AutoFilter field:=13, Criteria1:="true"
Selection.AutoFilter field:=15, Criteria1:="false"
Selection.AutoFilter field:=35, Criteria1:="m2"
'copy totals
Sheets("sbttl").Select
Range("d1:g1").Select
Selection.Copy
Range("d3").Select
ActiveCell.Offset(r, 0).Activate
Selection.PasteSpecial Paste:=xlValues, operation:=xlNone, skipblanks:= _
False, Transpose:=False
'gear yes, lighting yes
Sheets("table").Select
Selection.AutoFilter field:=15, Criteria1:="true"
Selection.AutoFilter field:=36, Criteria1:="m2"
r = r + 1 'add 1 row to activecell offset
'copy totals
Sheets("sbttl").Select
Range("d1:g1").Select
Selection.Copy
Range("d3").Select
ActiveCell.Offset(r, 0).Activate
Selection.PasteSpecial Paste:=xlValues, operation:=xlNone, skipblanks:= _
False, Transpose:=False
'gear no, lighting yes
Sheets("table").Select
Selection.AutoFilter field:=13, Criteria1:="false"
Selection.AutoFilter field:=35, Criteria1:="all"
r = r + 1 'add 1 row to activecell offset
'copy totals
Sheets("sbttl").Select
Range("d1:g1").Select
Selection.Copy
Range("d3").Select
ActiveCell.Offset(r, 0).Activate
Selection.PasteSpecial Paste:=xlValues, operation:=xlNone, skipblanks:= _
False, Transpose:=False
'reset fitltered fields
Sheets("table").Select
Selection.AutoFilter field:=13, Criteria1:="all"
Selection.AutoFilter field:=15, Criteria1:="all"
Selection.AutoFilter field:=35, Criteria1:="all"
Selection.AutoFilter field:=36, Criteria1:="all"
Sheets("summary").Select
Range("c2").Select
ActiveCell.Offset(0, c).Activate
m2 = ActiveCell.Select
Next c
Loop
Next r
Sheets("summary").Select
Range("c2").Select
ActiveCell.Offset(0, c1).Activate
m1 = ActiveCell
m2 = m1

Loop
Next c1