![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Mar 2002
Location: Horley Surrey England
Posts: 7
|
This code works for me to total the coloured cells in the selection.
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range) Dim total As Long For Each cell In Selection If cell.Interior.ColorIndex <> xlNone Then _ total = total + cell.Value Next MsgBox "The total of the coloured cells is " & total End Sub Does anybody know what I need to add to make it do a count as well, is it another (IF) or is there (undoubtably) a better way. Thanks |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Sunny, spring-like Hull
Posts: 3,339
|
Just add a Dim x As Long to the top of your code, and split your If.. Then statement as follows:
If cell.Interior.ColorIndex <> xlNone Then total = total + cell.Value x=x+1 End If Finally, you could change your message to: - MsgBox "There were " & x " coloured cells, totalling " & total |
|
|
|
|
|
#3 |
|
New Member
Join Date: May 2002
Location: Leicester, UK
Posts: 40
|
Hi Akroid
Updated code: Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range) Dim total As Long, Count as long For Each cell In Selection If cell.Interior.ColorIndex <> xlNone Then _ total = total + cell.Value count = count + 1 End if Next MsgBox "The total of the coloured cells is " & total & chr(13) & "The count of the coloured cells is " & count End Sub Hope this helped Cheers JayKay |
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Sunny, spring-like Hull
Posts: 3,339
|
Jay- you need to remove your underscore after 'Then' otherwise your code will throw up an error.
|
|
|
|
|
|
#5 |
|
New Member
Join Date: May 2002
Location: Leicester, UK
Posts: 40
|
Yo Mudface
Good call! JayKay |
|
|
|
|
|
#6 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Sunny, spring-like Hull
Posts: 3,339
|
Yo yourself
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|