PC Review


Reply
Thread Tools Rate Thread

Color Count Macro discrepency

 
 
Whois Clinton
Guest
Posts: n/a
 
      29th Jul 2008
Hi all,

I am running macros to count the number of various colored cells in a range.
Some are merged cells counted as one. I have a successful macro to count
red and yellow. However, when I change to blue or green there is an error.

Here is the successful macro counting red cells:

Option Explicit
Sub zxVisualRed()
Dim c As Range
Dim MyRange As Range
Dim arrRng() As String
Dim redCells As Long
Dim N As Long
Dim M As Long

Set MyRange = Range("H32:K58")
ReDim arrRng(1 To MyRange.Count)

For Each c In MyRange
If c.Interior.ColorIndex = 3 Then
If c.MergeCells Then
N = N + 1
For M = 1 To N
If c.MergeArea.Address = arrRng(M) Then
Exit For
End If
Next
If M > N Then
redCells = redCells + 1 'CAUSING ERROR
arrRng(N) = c.MergeArea.Address
End If
Else
redCells = redCells + 1 'CAUSING ERROR
End If
End If
Next
MsgBox redCells, vbOKOnly, "Visual Red" 'Does NOT cause error to chang
MsgBox text Red to green
Set c = Nothing
Set MyRange = Nothing
End Sub


When updating to green I change the color index number. That alone works
but when I update the CAUSING ERROR areas to "greencells" the g is
automatically capitalized and the error reads.
"Compile Error : Variable not defined" Highlighting the indicated areas
above. I was able to move from red to yellow doing this method but any other
color is ending in the same error.

Can I just leave the redcell text changing the colorindex alone even though
the text is incorrect?

Thansk in advance for any tips.

 
Reply With Quote
 
 
 
 
John Bundy
Guest
Posts: n/a
 
      29th Jul 2008
You have Option explicit turned on, that means you need to dimension
greenCells as Long as you did with redCells
--
-John
Please rate when your question is answered to help us and others know what
is helpful.


"Whois Clinton" wrote:

> Hi all,
>
> I am running macros to count the number of various colored cells in a range.
> Some are merged cells counted as one. I have a successful macro to count
> red and yellow. However, when I change to blue or green there is an error.
>
> Here is the successful macro counting red cells:
>
> Option Explicit
> Sub zxVisualRed()
> Dim c As Range
> Dim MyRange As Range
> Dim arrRng() As String
> Dim redCells As Long
> Dim N As Long
> Dim M As Long
>
> Set MyRange = Range("H32:K58")
> ReDim arrRng(1 To MyRange.Count)
>
> For Each c In MyRange
> If c.Interior.ColorIndex = 3 Then
> If c.MergeCells Then
> N = N + 1
> For M = 1 To N
> If c.MergeArea.Address = arrRng(M) Then
> Exit For
> End If
> Next
> If M > N Then
> redCells = redCells + 1 'CAUSING ERROR
> arrRng(N) = c.MergeArea.Address
> End If
> Else
> redCells = redCells + 1 'CAUSING ERROR
> End If
> End If
> Next
> MsgBox redCells, vbOKOnly, "Visual Red" 'Does NOT cause error to chang
> MsgBox text Red to green
> Set c = Nothing
> Set MyRange = Nothing
> End Sub
>
>
> When updating to green I change the color index number. That alone works
> but when I update the CAUSING ERROR areas to "greencells" the g is
> automatically capitalized and the error reads.
> "Compile Error : Variable not defined" Highlighting the indicated areas
> above. I was able to move from red to yellow doing this method but any other
> color is ending in the same error.
>
> Can I just leave the redcell text changing the colorindex alone even though
> the text is incorrect?
>
> Thansk in advance for any tips.
>

 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      29th Jul 2008
John has explained why you get the compile error.

If there are many merged areas involved the following should be a bit
quicker

For Each c In MyRange
If c.Interior.ColorIndex = 3 Then
If c.MergeCells Then
If c.Address = c.MergeArea(1).Address Then
redCells = redCells + 1
End If
Else
redCells = redCells + 1
End If
End If
Next

Regards,
Peter T

"Whois Clinton" <(E-Mail Removed)> wrote in message
news:7B2B397F-93FB-4828-BE12-(E-Mail Removed)...
> Hi all,
>
> I am running macros to count the number of various colored cells in a
> range.
> Some are merged cells counted as one. I have a successful macro to count
> red and yellow. However, when I change to blue or green there is an
> error.
>
> Here is the successful macro counting red cells:
>
> Option Explicit
> Sub zxVisualRed()
> Dim c As Range
> Dim MyRange As Range
> Dim arrRng() As String
> Dim redCells As Long
> Dim N As Long
> Dim M As Long
>
> Set MyRange = Range("H32:K58")
> ReDim arrRng(1 To MyRange.Count)
>
> For Each c In MyRange
> If c.Interior.ColorIndex = 3 Then
> If c.MergeCells Then
> N = N + 1
> For M = 1 To N
> If c.MergeArea.Address = arrRng(M) Then
> Exit For
> End If
> Next
> If M > N Then
> redCells = redCells + 1 'CAUSING ERROR
> arrRng(N) = c.MergeArea.Address
> End If
> Else
> redCells = redCells + 1 'CAUSING ERROR
> End If
> End If
> Next
> MsgBox redCells, vbOKOnly, "Visual Red" 'Does NOT cause error to chang
> MsgBox text Red to green
> Set c = Nothing
> Set MyRange = Nothing
> End Sub
>
>
> When updating to green I change the color index number. That alone works
> but when I update the CAUSING ERROR areas to "greencells" the g is
> automatically capitalized and the error reads.
> "Compile Error : Variable not defined" Highlighting the indicated areas
> above. I was able to move from red to yellow doing this method but any
> other
> color is ending in the same error.
>
> Can I just leave the redcell text changing the colorindex alone even
> though
> the text is incorrect?
>
> Thansk in advance for any tips.
>




 
Reply With Quote
 
Whois Clinton
Guest
Posts: n/a
 
      29th Jul 2008
Thanks to both of you. Not sure how I missed that text. So obvious once you
are not in it. Thanks also for the shorter version, however this is short
ranges at a time so the result is already immediate.

"Whois Clinton" wrote:

> Hi all,
>
> I am running macros to count the number of various colored cells in a range.
> Some are merged cells counted as one. I have a successful macro to count
> red and yellow. However, when I change to blue or green there is an error.
>
> Here is the successful macro counting red cells:
>
> Option Explicit
> Sub zxVisualRed()
> Dim c As Range
> Dim MyRange As Range
> Dim arrRng() As String
> Dim redCells As Long
> Dim N As Long
> Dim M As Long
>
> Set MyRange = Range("H32:K58")
> ReDim arrRng(1 To MyRange.Count)
>
> For Each c In MyRange
> If c.Interior.ColorIndex = 3 Then
> If c.MergeCells Then
> N = N + 1
> For M = 1 To N
> If c.MergeArea.Address = arrRng(M) Then
> Exit For
> End If
> Next
> If M > N Then
> redCells = redCells + 1 'CAUSING ERROR
> arrRng(N) = c.MergeArea.Address
> End If
> Else
> redCells = redCells + 1 'CAUSING ERROR
> End If
> End If
> Next
> MsgBox redCells, vbOKOnly, "Visual Red" 'Does NOT cause error to chang
> MsgBox text Red to green
> Set c = Nothing
> Set MyRange = Nothing
> End Sub
>
>
> When updating to green I change the color index number. That alone works
> but when I update the CAUSING ERROR areas to "greencells" the g is
> automatically capitalized and the error reads.
> "Compile Error : Variable not defined" Highlighting the indicated areas
> above. I was able to move from red to yellow doing this method but any other
> color is ending in the same error.
>
> Can I just leave the redcell text changing the colorindex alone even though
> the text is incorrect?
>
> Thansk in advance for any tips.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
I tried to get around the problem of the pivot table field settingdefaulting to Count instead of Sum by running a macro of change the settingfrom Count to Sum. However, when I tried to run the Macro, I got error messageof run time error 1004, unable Enda80 Microsoft Excel Misc 1 3rd May 2008 10:52 AM
I tried to get around the problem of the pivot table field settingdefaulting to Count instead of Sum by running a macro of change the settingfrom Count to Sum. However, when I tried to run the Macro, I got error messageof run time error 1004, unable Enda80 Microsoft Excel Worksheet Functions 0 3rd May 2008 01:04 AM
I tried to get around the problem of the pivot table field settingdefaulting to Count instead of Sum by running a macro of change the settingfrom Count to Sum. However, when I tried to run the Macro, I got error messageof run time error 1004, unable Enda80 Microsoft Excel Programming 0 3rd May 2008 01:03 AM
Number discrepency Ian Microsoft Excel Programming 2 8th Jun 2007 02:58 PM
Re: how do i count rows, and count them by color or a highlight? blue. Frank Kabel Microsoft Excel Worksheet Functions 0 9th Sep 2004 10:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:11 PM.