Applying formatting to even and odd rows with vba in a table

wbstadeli

Board Regular
Joined
Mar 11, 2016
Messages
153
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have a table called "Orders" in my worksheet "Work Order". How can i write a macro to select all the odd rows (excluding header) in columns 2 through 4 of the table, and apply a certain custom number format " 0 "R.H. and then with all the even rows (excluding header) with the same columns, apply a custom number format of "0 "L.H."? Any help would be appreciated!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I'm not 100% sure of the formatting you want to use but give this a try.

VBA Code:
Sub FormatTable()
Dim tbl As ListObject
Dim rngData As Range
Dim rngRow As Range

    Set tbl = Sheets("Work Order").ListObjects(1)

    Set rngData = tbl.DataBodyRange

    For Each rngRow In rngData.Rows

        If rngRow.Row Mod 2 = 0 Then
            Intersect(rngRow, tbl.ListColumns(2).Range.Resize(, 3)).NumberFormat = "0 ""R.H."""
        Else
            Intersect(rngRow, tbl.ListColumns(2).Range.Resize(, 3)).NumberFormat = "0 ""L.H."""
        End If
    Next rngRow

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,917
Members
449,093
Latest member
dbomb1414

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