Reputation: 13
Scenario: I have an existing Workbook with the same Conditional Formatting (CF) applied to all sheets. The CF is applied to a few column (minus the header row), with the "Applies to:" range set as =$A$2:$I$1048576, for example. The Workbook is already filled with data across about 30 sheets.
The problem: I need to update all the CF across the Workbook. To do so, I updated the CF on the first sheet to the new CF. Then I wrote a simple VBA macro to apply the updated CF to all other sheets, as to not go sheet-by-sheet updating all the CF on each sheet. My code works great, with one exception. It does not copy the "Applies to:" field of the conditional formatting.
I am only copying the formatting for the first row of data, because each sheet has a different number of rows of data. Below the last row of data on each sheet is the CF key (shows the user what the row colors generated by the CF means).
I have searched thoroughly and cannot find a solution to this issue.
Below is my code. How can I fix it to address this issue?
Sub CondFormatting()
'
' CondFormatting Macro
'
Dim WS_Count As Integer
Dim i As Integer
WS_Count = ActiveWorkbook.Worksheets.Count
Sheets(1).Range("A2:I2").Copy
For i = 2 To WS_Count
Sheets(i).Cells.FormatConditions.Delete
Sheets(i).Range("A2:I2").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Next i
Application.CutCopyMode = False
End Sub
Thanks.
** UPDATE **
Thanks to Jeffery's response, below, I was able to resolve the Conditional Formatting range issue I was having. For those with a similar problem, below is my revised code, which copies and applies the formatting from one Table to another across the entire WorkBook.
Sub CondFormatting()
'
' CondFormatting Macro
'
Dim WS_Count As Integer
Dim i As Integer
WS_Count = ActiveWorkbook.Worksheets.Count
Sheets(1).Range("A2").ListObject.DataBodyRange.Copy
For i = 2 To WS_Count
Sheets(i).Cells.FormatConditions.Delete
Sheets(i).Range("A2").ListObject.DataBodyRange.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Next i
Application.CutCopyMode = False
End Sub
Best regards.
Upvotes: 1
Views: 3361
Reputation: 4824
The 'Applies To' range is set by the selection active when you create (or in your case copy) the CF rules. So if you want the 'Applies To' to reference the range =$A$2:$I$1048576 then you will need to select that entire range in each sheet before applying the conditional formatting.
That said, applying CF to entire columns seems overkill to me. Better to change each data area on each sheet to an Excel Table (aka ListObject), because these grow and shrink along with your data, and so does any conditional formatting applied.
Upvotes: 0