Use And in IF Statement VBA

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Afternoon

My question today is I am trying to apply multiple conditions to a cell. The first condition formats the cell blue if the mod is 0. I would also like to format the font bold. (Maybe other formatting as well) The code I am using below. Thank You.

Sub format_every_other_row()


finalrow = Cells(Rows.Count, 1).End(xlUp).Row
For x = 1 To finalrow
If x Mod 2 = 0 Then
Cells(x, 1).Interior.Color = rgbCornflowerBlue
Else: Cells(x, 1).Interior.Color = rgbDeepPink


End If
Next


End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
You can "house" a lot of actions within a True (before Else) or False(Else) result.

Code:
    finalrow = Cells(Rows.Count, 1).End(xlUp).Row
For x = 1 To finalrow
    If x Mod 2 = 0 Then
        Cells(x, 1).Interior.Color = rgbCornflowerBlue
        Cells(x, 1).Font.Bold
        Else: Cells(x, 1).Interior.Color = rgbDeepPink
    
    
    End If
Next
 
Last edited:
Upvote 0
Thank you.

I actually started to use the with statement below the if. Is this practical.

Sub FormateveryOtherRow2()


finalrow = Cells(Rows.Count, 1).End(xlUp).Row
For x = 1 To finalrow
If x Mod 2 = 0 Then


With Cells(x, 1)
.Interior.Color = rgbCornflowerBlue
.Font.Bold = True
.Font.Size = 11
.Font.Name = "Arial"




End With
Else: Cells(x, 1).Interior.Color = rgbDeepPink


End If


Next


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
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