VBA Case Loop

FalconFlyer

New Member
Joined
May 18, 2015
Messages
30
I have 2 columns (F and G) that I would ultimately run this macro on. Column F is formatted as numbers and Column G is %. The macro works on Column F, but returns "Type Mismatch" for G. Not sure what I am doing wrong.

Code:
Dim LastRow As Long
Dim r As Long
    Application.ScreenUpdating = False
    
'   Find last populated cell in column A
    LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through all rows starting from row 2
    For r = 3 To LastRow
        Select Case Cells(r, "G")
            Case Is < 0
                Range(Cells(r - 1, "G"), Cells(r, "G")).Interior.Color = RGB(247, 196, 187)
            Case Is > 0
                Range(Cells(r - 1, "G"), Cells(r, "G")).Interior.Color = RGB(200, 239, 195)
        End Select
    Next r
    
    Application.ScreenUpdating = True

Can I combine it to apply to both F and G? What am I doing wrong?

TIA
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Do you want columns F and G formatted by the same rule (so for any row, columns F and G will always be the same color)?
Do you have any non-numeric values in columns F and G?
 
Upvote 0
Thank you for your reply. It made me go back and check. Sure enough I had a div/0 error in a few of the cells in the column. Once I corrected it, the code worked.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,279
Members
452,630
Latest member
OdubiYouth

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