If 3 conditions VBA

ExcelNooberino

New Member
Joined
Jan 2, 2019
Messages
43
Office Version
  1. 2016
Platform
  1. Windows
Hi! Hope y'all doing just fine...

So I have this problem with a recent code I'm working on. Basically I want to apply some formatting to the color off a range of cells after I run this macro I've created but it keeps telling me that there's a "Next without For" and I don't understand why... I just want something that allows me to do this.

If the cell value in column A is greater than the cell value in column B set the background to green.
If the cell value in column A is less than the cell value in column C set the background to red.
If the cell value in column A is in between the cell value B and C set the background to orange.

So far I have this (cell A is like mine Cell(i, 8).Value):

Code:
Dim LastRow As LongDim FirstRow As Long


LastRow = Sheet5.Range("B" & Rows.Count).End(xlUp).row
FirstRow = Sheet5.Cells(10, 8).row


If LastRow = FirstRow Then
'do nothing


Else


    For i = 10 To LastRow
    If Cells(i, 8).Value > Cells(i, 11).Value Then
        Cells(i, 8).Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 5287936 'Green
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        With Selection.Font
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = 0
            .Bold = True
        End With
    If Cells(i, 8).Value > Cells(i, 10).Value Then
        Cells(i, 8).Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 255 'Red
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        With Selection.Font
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = 0
            .Bold = True
        End With
        Else
        Cells(i, 8).Select
            With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 49407 'Orange
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        With Selection.Font
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = 0
            .Bold = True
        End With
    End If
    Next i
End If

Thanks in advance!
 
Last edited by a moderator:

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try making this change
Code:
[COLOR=#ff0000]Else[/COLOR]If Cells(i, 8).Value > Cells(i, 10).Value Then
 
Upvote 0
That little change made all the diference! Thanks a lot mate, really appreciate it! :D
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,936
Messages
6,122,340
Members
449,079
Latest member
rocketslinger

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