I am hoping someone here can help. I have written a piece of code in a VBA Module but when I run it, I get a the following error
Run-time error '1004':
Method 'Range' of object '_Global' failed
The intention of the code is to look at a series of results and highlight the numbers which are higher than the three columns which contain the criteria
The code seems to fall over at the first "If" statement. Any advice greatly received.
Run-time error '1004':
Method 'Range' of object '_Global' failed
The intention of the code is to look at a series of results and highlight the numbers which are higher than the three columns which contain the criteria
Code:
Sub SGVs()
' SGV HIGHLIGHT Macro
Dim i As Integer
Dim k As Integer
For i = 9 To 48
Range("H" & i, "GW" & i).Select
If Range("E" & i) >= Range("D" & k) And Range("F" & i) >= Range("E" & i) Then
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:=Range("D" & i), Formula2:=Range("E" & i)
Selection.FormatConditions(1).Interior.ColorIndex = 35
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:=Range("E" & i), Formula2:=Range("F" & i)
Selection.FormatConditions(2).Interior.ColorIndex = 43
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:=Range("F" & i)
Selection.FormatConditions(3).Interior.ColorIndex = 50
ElseIf Range("D" & i) >= Range("E" & i) And Range("F" & i) >= Range("D" & i) Then
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:=Range("E" & i), Formula2:=Range("D" & i)
Selection.FormatConditions(1).Interior.ColorIndex = 43
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:=Range("D" & i), Formula2:=Range("F" & i)
Selection.FormatConditions(2).Interior.ColorIndex = 35
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:=Range("F" & i)
Selection.FormatConditions(3).Interior.ColorIndex = 50
ElseIf Range("F" & i) >= Range("D" & i) And Range("E" & i) >= Range("F" & i) Then
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:=Range("D" & i), Formula2:=Range("F" & i)
Selection.FormatConditions(1).Interior.ColorIndex = 35
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:=Range("F" & i), Formula2:=Range("E" & i)
Selection.FormatConditions(2).Interior.ColorIndex = 50
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:=Range("E" & i)
Selection.FormatConditions(3).Interior.ColorIndex = 43
ElseIf Range("F" & i) >= Range("E" & i) And Range("D" & i) >= Range("F" & i) Then
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:=Range("E" & i), Formula2:=Range("F" & i)
Selection.FormatConditions(1).Interior.ColorIndex = 43
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:=Range("F" & i), Formula2:=Range("D" & i)
Selection.FormatConditions(2).Interior.ColorIndex = 50
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:=Range("D" & i)
Selection.FormatConditions(3).Interior.ColorIndex = 35
End If
Next i
End Sub
The code seems to fall over at the first "If" statement. Any advice greatly received.