macro help

david graham

Active Member
Joined
Dec 6, 2007
Messages
345
The code below sorts a list & then formats only the visible rows. This there a line i can add that will keep it from trying to format when there are no visible rows in the selection?

will "If Not rng Is Nothing Then" work & if so where do i need to put it?


Code:
'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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

crimson_b1ade

Well-known Member
Joined
Sep 27, 2008
Messages
1,557
Hi David, my buddy...my pal...you could try using a variable to pick up the last row instead of going all the down to row 30,000. see below example. Items in bold face red are what i changed. By the by, you don't need to Select stuff to work with them in vba.:

Rich (BB code):
Sub DavidLikesToFormat()
 
'FORMAT LABOR ITEMS
 
Dim LastRow As Long
 
Sheets("LIST").Select
Range("R1").Select
Selection.AutoFilter Field:=1, Criteria1:="6"
Range("b1").Select
 
LastRow = Sheets("LIST").Range("B" & Rows.Count).End(xlUp).Row
 
Set r = ThisWorkbook.Worksheets("LIST").Range("B14:B" & LastRow).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:C" & LastRow).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:E" & LastRow).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
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,195,961
Messages
6,012,582
Members
441,713
Latest member
Dave353

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
Top