If row is even then

pincivma

Board Regular
Joined
Dec 12, 2004
Messages
203
Hi All

I'm struggling with this code. I don't know how to do it in VBA. Here is what I want the code to say:

If row is even then

Code goes here

End If

Thanks.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi All

I'm struggling with this code. I don't know how to do it in VBA. Here is what I want the code to say:

If row is even then

Code goes here

End If

Thanks.
Maybe:
Code:
If row mod 2 = 0 then
'code here
M
 
Upvote 0
Try

Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
    For r = 1 To lr
        If r Mod 2 = 0 Then
            'your code here
        End If
    Next r
End Sub
 
Upvote 0
Another option
Code:
   Dim i As Long
   For i = 1 To 10
      If Application.IsEven(i) Then Rows(i).Interior.Color = 56789
   Next i
 
Upvote 0
Hi Fluff

This is funny. My brother's nickname is Fluff. Anyway, thanks for your input. However, I'm still struggling with the macro so I'm sending you a code that I did. I want even rows to be gray and uneven rows to be no color. Here is my macro but it does not work properly. Check it out and tell me what I did wrong.

Sub ClearCheckmark()

Sheets("Sheet1").Select
If Row Mod 2 = 2 Then

With Selection
.Clear ' This code deletes the check mark and the color green
.Style = "Grey" 'sets the cell style using a custom style named Custom Gray
End With
End If

If Row Mod 2 = 1 Then ' for odd rows

With Selection
.Clear ' This code deletes the check mark and the color green
End With
End If
ActiveCell.Offset(0, 1).Select

End Sub
 
Last edited:
Upvote 0
I don't have a "Grey" style so you can modify this to suit

Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
    For r = 1 To lr
        If r Mod 2 = 0 Then
          With Rows(r)
            .Clear ' This code deletes the check mark and the color green
            .Style = "Check Cell" 'sets the cell style using a custom style named Custom Gray
        End With
        Else:
        Rows(r).Clear
        End If
    Next r
End Sub
 
Last edited:
Upvote 0
Hi All
Thanks for all your replies. However, I don't think that I have made myself understood. All I want the macro to do is clear the check mark in the cell that I select (ActiveCell.Select), i.e. only one cell and clear that cell only. Then, all I want the macro to do is this: If the row is even, color only that one cell grey (not the whole row). If the row is odd, then just clear that cell only. However, all those macros that have been posted are all great. I can store them and use them when the need arises

Thanks
 
Upvote 0
Maybe this then...

Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
    For r = 1 To lr
        If r Mod 2 = 0 Then
          With Range("A" & r)
            .Clear ' This code deletes the check mark and the color green
            .Style = "Check Cell" 'sets the cell style using a custom style named Custom Gray
        End With
        Else:
        Range("A" & r).Clear
        End If
    Next r
End Sub
 
Upvote 0
Maybe this:

Code:
Sub ClearCheckmark()

Sheets("Sheet1").Select
With activecell
   .Clear ' This code deletes the check mark and the color green
   If .Row Mod 2 = 0 Then .Style = "Grey" 'sets the cell style using a custom style named Custom Gray
   .Offset(0, 1).Select
End With
End Sub
 
Upvote 0
I finally got the macro to work. Thanks to Michael and Rory. Both macros work. Now it is just a matter of deciding which one to use.
Thanks guys.
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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