Microsoft Excel - How can I simplify the following (2) macros to make it run quicker?

Asked By C R on 03-Apr-13 07:53 PM

Hi,

For the first macro, I need to add two compenents: (1): Change all cell references to a range, (2) Once this is completed add an xldown in the For statement so the macro does not run if there is no data in column j. I imagine it would go something like "for each j in range("I11").end(xldown)

For the second macro, is this possible to consolidate?

Thanks,
C

Macro 1:
Sub IndentifyRGB()
    For j = 11 To 999
    If Cells(j, 9) = "" Then
    Cells(j, 5) = "-"
      Else
      If Cells(j, 9) <> "" Then
      Cells(j, 5) = Cells(j, 9).Interior.Color Mod 256 & " " & (Cells(j, 9).Interior.Color Mod 256 ^ 2) \ 256 & " " & Cells(j, 9).Interior.Color \ 256 ^ 2
      End If
    End If
Next j
End Sub

Macro 2:
Sub ColorRows()
    For i = 9 To 87
      If Range("e" & i).Value = "P" Then
      Sheets("Summary2").Range("g" & i).Interior.Color = RGB(191, 191, 191)
      Sheets("Summary2").Range("h" & i).Interior.Color = RGB(191, 191, 191)
      Sheets("Summary2").Range("i" & i).Interior.Color = RGB(191, 191, 191)
      Sheets("Summary2").Range("j" & i).Interior.Color = RGB(191, 191, 191)
      End If
     
      If Range("e" & i).Value = "L" Then
      Sheets("Summary2").Range("g" & i).Interior.Color = RGB(249, 184, 119)
      Sheets("Summary2").Range("h" & i).Interior.Color = RGB(249, 184, 119)
      Sheets("Summary2").Range("i" & i).Interior.Color = RGB(249, 184, 119)
      Sheets("Summary2").Range("j" & i).Interior.Color = RGB(249, 184, 119)
      End If
     
      If Range("e" & i).Value = "O" Or Range("e" & i).Value = "-" Then
      Sheets("Summary2").Range("g" & i).Interior.Color = RGB(255, 255, 255)
      Sheets("Summary2").Range("h" & i).Interior.Color = RGB(255, 255, 255)
      Sheets("Summary2").Range("i" & i).Interior.Color = RGB(255, 255, 255)
      Sheets("Summary2").Range("j" & i).Interior.Color = RGB(255, 255, 255)
      End If

    Next i
End Sub

Harry Boughen replied to C R on 03-Apr-13 10:33 PM
Hello CR
Try the following:

Option Explicit

Sub IndentifyRGB()
    Dim j As Integer
    Dim rngData, rngCell As Range
    
    Set rngData = Range("I11:I" & Range("I65536").End(xlUp).Row)
    For Each rngCell In rngData
    If rngCell = "" Then
    rngCell.Offset(0, -4) = "-"
    Else
    rngCell.Offset(0, -4) = rngCell.Interior.Color Mod 256 & " " & (rngCell.Interior.Color Mod 256 ^ 2) \ 256 & " " & rngCell.Interior.Color \ 256 ^ 2
    End If
    Next rngCell
End Sub

Sub ColorRows()
Dim i As Integer
Dim strRange As String

    For i = 9 To 87
    strRange = "G" & i & ": J" & i
    If Range("e" & i).Value = "P" Then
    Sheets("Summary2").Range(strRange).Interior.Color = RGB(191, 191, 191)
    ElseIf Range("e" & i).Value = "L" Then
    Sheets("Summary2").Range(strRange).Interior.Color = RGB(249, 184, 119)
    ElseIf Range("e" & i).Value = "O" Or Range("e" & i).Value = "-" Then
    Sheets("Summary2").Range(strRange).Interior.Color = RGB(255, 255, 255)
    End If
    Next i
End Sub

I am not sure whether you mean rows 9 to 87 to be fixed or variable but if you want them to be variable then that is possible too.
Regards
Harry
Robbe Morris replied to C R on 04-Apr-13 08:52 AM
Application.Calculation = xlManual 

run your code here

Application.Calculation = xlAutomatic

I suspect you'll see a significant increase in performance.
C R replied to Robbe Morris on 04-Apr-13 01:11 PM
Thanks, all! Both of these work great.