LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
mns mns is offline
external usenet poster
 
Posts: 23
Default if / then structure with macros

Hi there,
I am using a macro to count colored cells. You can see the code below.
Then I use to count the cells with
=SUMPRODUCT(--(ColorIndex(Sheet1!F33:F285)=35))
where 35 is the colorindex (i got it with =colorindex(a1) )
now i need to and an if statement:)
something like,
scan the range of F33:F285, find the cells containing number 3,
check if these cells has colorindex 35.

with a noob approch it'd be like
=IF(COUNTIFS(Sheet1!B33:B285,3),SUMPRODUCT(--(ColorIndex(Sheet1!F33:F285)=35)))
I thing I cannot figure out if/then structure right:(

any ideas?

thanx in advance.


Function ColorIndex(rng As Range, _
Optional text As Boolean = False) As Variant

Dim cell As Range, row As Range
Dim i As Long, j As Long
Dim iWhite As Long, iBlack As Long
Dim aryColours As Variant

If rng.Areas.Count 1 Then
ColorIndex = CVErr(xlErrValue)
Exit Function
End If

iWhite = WhiteColorindex(rng.Worksheet.Parent)
iBlack = BlackColorindex(rng.Worksheet.Parent)

If rng.Cells.Count = 1 Then
If text Then
aryColours = DecodeColorIndex(rng, True, iBlack)
Else
aryColours = DecodeColorIndex(rng, False, iWhite)
End If

Else
aryColours = rng.Value
i = 0

For Each row In rng.Rows
i = i + 1
j = 0

For Each cell In row.Cells
j = j + 1

If text Then
aryColours(i, j) = _
DecodeColorIndex(cell, True, iBlack)
Else
aryColours(i, j) = _
DecodeColorIndex(cell, False, iWhite)
End If

Next cell

Next row

End If

ColorIndex = aryColours

End Function

Private Function WhiteColorindex(oWB As Workbook)
Dim iPalette As Long
WhiteColorindex = 0
For iPalette = 1 To 56
If oWB.Colors(iPalette) = &HFFFFFF Then
WhiteColorindex = iPalette
Exit Function
End If
Next iPalette
End Function

Private Function BlackColorindex(oWB As Workbook)
Dim iPalette As Long
BlackColorindex = 0
For iPalette = 1 To 56
If oWB.Colors(iPalette) = &H0 Then
BlackColorindex = iPalette
Exit Function
End If
Next iPalette
End Function

Private Function DecodeColorIndex(rng As Range, _
text As Boolean, _
idx As Long)
Dim iColor As Long
If text Then
iColor = rng.Font.ColorIndex
Else
iColor = rng.Interior.ColorIndex
End If
If iColor < 0 Then
iColor = idx
End If
DecodeColorIndex = iColor
End Function


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
If structure LiAD Excel Programming 9 September 1st 09 09:14 AM
VBA Structure Charts PBcorn Excel Programming 1 May 21st 08 11:01 PM
vba structure Rick Excel Programming 3 January 8th 08 03:10 AM
if structure help filo666 Excel Programming 4 March 1st 05 08:37 PM
Structure of If...Else in VBA John Wilson Excel Programming 2 July 30th 03 04:33 AM


All times are GMT +1. The time now is 01:28 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"