VBA Conditional Format List Object Table

Amapola

New Member
Joined
Jul 7, 2010
Messages
17
Good Afternoon

I'm trying to get VBA to format a report I have to do regularly. I want to format the entire row depending on value in one of the columns. As the view can change and that might impact the columns the report downloads, I'm formatting the data as a table and want to use the table header to determine which column to use as a condition. I also want to delete a couple of columns after that.
Finally, I want to save the file as xlsx with the calculated filename.

I get an error 438 on this line, Object doesn't support this property
tbl.DataBodyRange.Rows.FormatConditions.Interior.Color = RGB(205, 5, 5)

I have tried various ways to mimic conditional formatting in VBA but none work. Note, it does not need to be dynamic.

Anybody see where I go wrong?

Thanks, Christine



VBA Code:
Sub CalDueReport()


Dim shTable As Worksheet
Set shTable = Sheets("Table")
    
    Application.CutCopyMode = False
    
'Convert to Table
Dim tbl As ListObject
Dim src As Range
Set src = Range("A1").CurrentRegion
    ActiveSheet.ListObjects.Add(SourceType:=xlSrcRange, Source:=src, xlListObjectHasHeaders:=xlYes).Name = _
        "CalDue"
    Range("CalDue[#All]").Select
    ActiveSheet.ListObjects("CalDue").TableStyle = "TableStyleMedium1"
Set tbl = shTable.ListObjects("CalDue")

Dim sortcolumn As Range
Set sortcolumn = Range("CalDue[Calibration due]")
With tbl.Sort
   .SortFields.Clear
   .SortFields.Add Key:=sortcolumn, SortOn:=xlSortOnValues, Order:=xlAscending
   .Header = xlYes
   .Apply
End With

'today's date for condition and file name
Dim CurrDate As Date
CurrDate = Date
Dim ReportDate As String
ReportDate = Format(Date, "yyyy-mm-dd")

'Delete any existing Conditional Formatting
tbl.DataBodyRange.FormatConditions.Delete


'Overdue
tbl.DataBodyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=CurrDate > INDIRECT(""CalDue[@Calibration due]"")"
tbl.DataBodyRange.Rows.FormatConditions.Interior.Color = RGB(205, 5, 5)

'Next Month
tbl.DataBodyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=CurrDate + 30 > INDIRECT(""CalDue[@Calibration due]"")"
tbl.DataBodyRange.Rows.FormatConditions.Interior.Color = RGB(255, 155, 50)

'Next 3 months
tbl.DataBodyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=CurrDate + 90 > INDIRECT(""CalDue[@Calibration due]"")"
tbl.DataBodyRange.Rows.FormatConditions.Interior.Color = RGB(250, 250, 5)

'Next 6 monts
tbl.DataBodyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=CurrDate +180 > INDIRECT(""CalDue[@Calibration due]"")"
tbl.DataBodyRange.Rows.FormatConditions.Interior.Color = RGB(50, 200, 200)



'Delete Column Tool type
tbl.ListColumns("Category").Delete


'Delete Last Table Row
tbl.TotalsRowRange.Delete

'Hide Items not calibrated
Dim iCol As Long
iCol = tbl.ListColumns("Status").Index
    tbl.ListObjects("CalDue").AutoFilter Field:=iCol, _
    Criteria1:=Array("Ready to Deploy", "Ready to Deploy Deployed", "Calibration Overdue - Do not use", "Calibration Overdue - Do not use Deployed")
          

'Save as report with month date
Dim ReportName As String
ReportName = "CalDueReport_" & ReportDate & ".xlsx"
ActiveWorkbook.SaveAs Filename:="C:\UserData\XXXX\XXXX\XXXX\" & ReportName, FileFormat:=51

    
End Sub
 
Glad we could help & thanks for the feedback.
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Forum statistics

Threads
1,215,942
Messages
6,127,807
Members
449,408
Latest member
Bharathi V

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