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

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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