VBA not cell filling correctly in IF/THEN structure

kmmsquared

New Member
Joined
Jan 7, 2011
Messages
33
Hello,

I am using the following code below to filter a column and color the rows of that contain cells with the correct values (this occurs 3x in my code). My problem is the if e is nothing then the column still colors. I only want the rows to be colored if e is not nothing. Any suggestions would be greatly appreciated-there is probably something simple that I can't think of. Thanks!

Code:
With Sheets("Sheet1")
   lngLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
   s = Application.WorksheetFunction.Match("Esc", Range("1:1"), 0)
   e = Range("E2:E" & lngLastRow).SpecialCells(xlCellTypeVisible)      
 
Columns("E").AutoFilter Field:=1, Criteria1:="MAKE"
 
[COLOR=red]If Not e Is Nothing Then
Range(Cells(2, 1), Cells(lngLastRow, s)).SpecialCells(xlCellTypeVisible).Activate
With Selection.Interior
.ThemeColor = xlThemeColorAccent5
.TintAndShade = 0.599993896298105
End With
[/COLOR]End If
 
I just ran two if then statements. One colors the cell and one tints the cells.
It may be an extra step, but it works correctly everytime.

Code:
Columns("E").Select
Selection.AutoFilter
Columns("E").Select
Selection.AutoFilter Field:=1, Criteria1:="G"
If Not e.SpecialCells(xlCellTypeVisible) Is Nothing Then
Range(Cells(2, 1), Cells(lngLastRow, s)).SpecialCells(xlCellTypeVisible).Interior.ThemeColor = xlThemeColorAccent2
End If
If Not e.SpecialCells(xlCellTypeVisible) Is Nothing Then
Range(Cells(2, 1), Cells(lngLastRow, s)).SpecialCells(xlCellTypeVisible).Interior.TintAndShade = 0.799981688894314
End If
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,216,115
Messages
6,128,915
Members
449,478
Latest member
Davenil

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