macro that formats visible rows after a list is sorted

david graham

Active Member
Joined
Dec 6, 2007
Messages
345
I use the code below to sort a list, & then format only the visible rows. I need an addition to this code that will keep it from trying to format if there are no visible cells in the selection.


Code:
Range("R1").Select
    Selection.AutoFilter Field:=1, Criteria1:="3"
    
    Set r = ThisWorkbook.Worksheets("LIST").Range("B12:b30000").SpecialCells(xlCellTypeVisible)
   With r.Interior
        .ColorIndex = 6
        .Pattern = xlSolid
        r.Font.ColorIndex = 6
        r.HorizontalAlignment = xlCenter
     End With
     
     Set r = ThisWorkbook.Worksheets("LIST").Range("c12:c30000").SpecialCells(xlCellTypeVisible)
    With r.Interior
        .ColorIndex = 6
        .Pattern = xlSolid
        r.Font.ColorIndex = 1
        r.Font.Bold = True
        r.HorizontalAlignment = xlLeft
     r.Borders(xlDiagonalDown).LineStyle = xlNone
    r.Borders(xlDiagonalUp).LineStyle = xlNone
    r.Borders(xlEdgeLeft).LineStyle = xlNone
    End With
    With r.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = 1
    End With
    With r.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = 1
    End With
    With r.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = 1
    End With
     
     Set r = ThisWorkbook.Worksheets("LIST").Range("d12:H30000").SpecialCells(xlCellTypeVisible)
    With r.Interior
        .ColorIndex = 6
        .Pattern = xlSolid
        r.Font.ColorIndex = 1
    r.HorizontalAlignment = xlCenter
    End With
    
'Selection.AutoFilter Field:=1
 

'FORMAT LABOR ITEMS
  Sheets("LIST").Select
 Range("R1").Select
    Selection.AutoFilter Field:=1, Criteria1:="6"
Range("b1").Select
Set r = ThisWorkbook.Worksheets("LIST").Range("B14:b30000").SpecialCells(xlCellTypeVisible)
   With r.Interior
        .ColorIndex = 2
        .Pattern = xlSolid
        r.HorizontalAlignment = xlLeft
        r.VerticalAlignment = xlTop
        r.WrapText = True
        r.Font.ColorIndex = 2
        r.RowHeight = 30.75
    End With
Set r = ThisWorkbook.Worksheets("LIST").Range("C14:C30000").SpecialCells(xlCellTypeVisible)
   With r.Interior
        .ColorIndex = 2
        .Pattern = xlSolid
        r.HorizontalAlignment = xlLeft
        r.VerticalAlignment = xlTop
        r.WrapText = True
        r.Font.ColorIndex = 1
        r.RowHeight = 30.75
    End With
Set r = ThisWorkbook.Worksheets("LIST").Range("D14:E30000").SpecialCells(xlCellTypeVisible)
   With r.Interior
        .ColorIndex = 15
        .Pattern = xlSolid
        r.HorizontalAlignment = xlCenter
        r.VerticalAlignment = xlCenter
        r.WrapText = True
        r.Font.ColorIndex = 1
        r.RowHeight = 30.75
    End With
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
This is a better example of my code, thanks



Code:
'FORMAT 'FORMAT LABOR ITEMS
  Sheets("LIST").Select
 Range("R1").Select
    Selection.AutoFilter Field:=1, Criteria1:="6"
 
Set r = ThisWorkbook.Worksheets("LIST").Range("B14:b30000").SpecialCells(xlCellTypeVisible)
   With r.Interior
        .ColorIndex = 2
        .Pattern = xlSolid
        r.HorizontalAlignment = xlLeft
        r.VerticalAlignment = xlTop
        r.WrapText = True
        r.Font.ColorIndex = 2
        r.RowHeight = 30.75
    End With
 
Set r = ThisWorkbook.Worksheets("LIST").Range("C14:C30000").SpecialCells(xlCellTypeVisible)
   With r.Interior
        .ColorIndex = 2
        .Pattern = xlSolid
        r.HorizontalAlignment = xlLeft
        r.VerticalAlignment = xlTop
        r.WrapText = True
        r.Font.ColorIndex = 1
        r.RowHeight = 30.75
    End With
 
Set r = ThisWorkbook.Worksheets("LIST").Range("D14:E30000").SpecialCells(xlCellTypeVisible)
   With r.Interior
        .ColorIndex = 15
        .Pattern = xlSolid
        r.HorizontalAlignment = xlCenter
        r.VerticalAlignment = xlCenter
        r.WrapText = True
        r.Font.ColorIndex = 1
        r.RowHeight = 30.75
    End With
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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