PDA

View Full Version : Solved: If statement to change format



infinity
10-30-2005, 09:18 PM
Hey all,
I am pretty new to VBA, so I hope someone can help me. I am trying to use and If statement to change the interior color of a group of cells. I am inserting a row at 6:6,then if B7 interior color is grey then I want B6:K6 to be white. If B7 interior color is white, I want B6:K6 to be grey. This is the code I thought I would use for that but I keep getting an error saying that "Object does not support this property or method". Any suggestions? Thank you so much in advance.


Range("B7").Select
If Selection.Interior = ColorIndex = 1 Then
Range("B6:K6").Select
With Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
Range("B7").Select
ElseIf Selection.Interior = ColorIndex = 15 Then
Range("B6:K6").Select
With Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
End If

HaHoBe
10-30-2005, 09:40 PM
Hi, infinity,

the ColorIndex for White depends on the color pattern in the workbook - if I check mine (not altered) it shows 2 for white but won?t recognize any cells to show white but xlNone. The check for the ColorIndex should read like this (and as you do not have any other colours then grey and white the ElseIf-statement may be changed to read just Else)


Option Explicit

Sub infinity()
Rows("6:6").Insert Shift:=xlDown
With Range("B6:K6")
If Range("B7").Interior.ColorIndex = xlNone Then
With .Interior
.ColorIndex = 15
.Pattern = xlSolid
.PatternColorIndex = xlSolid
End With
Else
With .Interior
.ColorIndex = xlNone
.Pattern = xlSolid
.PatternColorIndex = xlNone
End With
End If
End With
End Sub
Ciao,
Holger

infinity
10-30-2005, 09:54 PM
Thank you so much! That did exactly what I wanted it to do!