Weirdness with .SpecialCells(xlCellTypeVisible)

techturtle

New Member
Joined
Mar 23, 2011
Messages
5
I have a subroutine (Excel 2010) that includes subtotaling the data on a worksheet, then collapsing the subtotals. With the subtotal on level 2, I am trying to bold all the subtotaled numbers and apply a back color, while leaving the cells in between unbolded and white background. The code that I'm using is this:
Code:
Range(Cells(2, 1), Cells(lngLastRow, lngLastCol)).Select
With Selection
    .Font.Bold = False
    .Interior.Color = vbWhite
End With

With Selection
    .SpecialCells(xlCellTypeVisible).Select
    .Font.Bold = True
    .Interior.Color = vbLtBlue
End With
The first selection and With block works as expected; it undoes all bolding and turns everything white. The second With block starts with the same selection, then uses the .SpecialCells(xlCellTypeVisible).select to change it so only the subtotaled rows are selected. In addition to seeing a visible change in the selection, I have verified via selection.cells.count that only those cells are in the selection. However, when the .Font.Bold and .Interior.Color sections execute, they hit everything, not just visible or selected cells. Any idea why this is happening?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Possibly

Code:
With Selection.SpecialCells(xlCellTypeVisible)
    .Font.Bold = True
    .Interior.Color = vbLtBlue
End With
 
Upvote 0
Possibly

Code:
With Selection.SpecialCells(xlCellTypeVisible)
    .Font.Bold = True
    .Interior.Color = vbLtBlue
End With
That did it. I just realized it was doing the same thing in another part of my code as well, so double thanks for this fix. Any idea why it did it that way? I mean, I could prove that only the correct cells were selected, why wouldn't they be the only ones to get the selection-based formatting?
 
Upvote 0
I'm not sure why you had that problem but selecting is generally best avoided anyway.
 
Upvote 0
The problem was because the With Selection creats a temporary range object for that parent selection, and each line is executed based on that range object. If you perform a select inside of the With Selection, it does not overwrite that parent object's range reference.
 
Upvote 0
Here's why

Rich (BB code):
With Selection
    .SpecialCells(xlCellTypeVisible).Select
    .Font.Bold = True
    .Interior.Color = vbLtBlue
End With

In the WITH structure, the Red Dots refer to what was originally referenced in the with. REGARDLESS of whatever was slected afterwards.
so even after doing the specialcells(..).select, the with is STILL referring to whatever was selected at the time the With was executed.

Here's a simplified example

Rich (BB code):
Sub Test
 
Range("A1").Select
With Selection
    Range("Z1").Select
    Msgbox .Address 'This will return $A$1
End With
End Sub
 
Upvote 0
Here's why
In the WITH structure, the Red Dots refer to what was originally referenced in the with. REGARDLESS of whatever was slected afterwards.
so even after doing the specialcells(..).select, the with is STILL referring to whatever was selected at the time the With was executed.

Excellent explanation, thank you! Makes sense now why it would do what it did. Even when I was testing the cell count, I was doing it from the Immediate Window, so it was outside the WITH block's special little saved range. And here I always thought that using the the WITH and the dots just saved me some typing...
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

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