james potter
New Member
- Joined
- Aug 15, 2005
- Messages
- 45
Hello,
I have created a vba-macro that runs through a certain range. It compairs the value of the above cell with the current cell. If it is the same then it will give the cell the same color, so not then it will give a random color. I have tested it in one sheet and works fine, but to do it for all sheets in the workbook it jams. The range starts in every sheet in the same cell, d5.
How can I make the code so it will loop through all sheets from cell d4 and give the cell a random color if the compairing fails?
I have created a vba-macro that runs through a certain range. It compairs the value of the above cell with the current cell. If it is the same then it will give the cell the same color, so not then it will give a random color. I have tested it in one sheet and works fine, but to do it for all sheets in the workbook it jams. The range starts in every sheet in the same cell, d5.
How can I make the code so it will loop through all sheets from cell d4 and give the cell a random color if the compairing fails?
Code:
Sub looping()
Dim noSheets
Dim x
noSheets = ActiveWorkbook.Worksheets.Count
For x = 1 To noSheets
ActiveCell = Worksheets(x).Range("d5")
Do
If Worksheets(x).ActiveCell.Value = ActiveCell.Offset(-1, 0).Value Then
Selection.Interior.ColorIndex = ActiveCell.Offset(-1, 0).Interior.ColorIndex
Else
Selection.Interior.ColorIndex = (random color)
End If
ActiveCell.Offset(1, 0).Select
Loop Until IsEmpty(ActiveCell)
Next x
End Sub