VBA run-time error 1004

Gazman101

New Member
Joined
Apr 27, 2017
Messages
3
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

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.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You don't have a value of k, so its trying to look at col D row 0
 
Upvote 0
If I am reading this correctly k should be = 0 at this line of code causing an error.


If Range("E" & i) >= Range("D" & k) And Range("F" & i) >= Range("E" & i) Then
 
Upvote 0
The value of k here will be 0, there is no row 0.
Code:
        If Range("E" & i) >= Range("D" & k) And Range("F" & i) >= Range("E" & i) Then
What should the value of k be?
 
Upvote 0
Thank you all. Couldn't see the wood for the trees. Its a typo in the line the "k" should be an "i". I've been staring at this for days. Amazing what a second pair of eyes can see. FYI the "k" refers to a second bit of code, that i didn't post, which is basically a duplicate of the above but skips a few rows.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top