Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 57
Default Count merged cells as if unmerged

I had this neat macro that counts how many cells are a certain color:



Public Function CountColors(color As String) As Integer

Dim intCount As Integer

Dim cell As Range



For Each cell In Range("ThisRange").Cells

If cell.Interior.color = 825735 Then intCount = intCount + 1

Next cell

CountColors = intCount

End Function



The problem is, I have merged some cells. How can I get a macro to count
say, 3 cells merged into 1 as three cells, not 1, if they match a certain
color?.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,069
Default Count merged cells as if unmerged

Try this:

Public Function CountColors(ColorNbr As String) As Long
Dim lCount As Long, c As Range
lCount = 0
For Each c In Range("ThisRange")
If c.Interior.color = ColorNbr Then
If c.MergeCells = True Then
lCount = lCount + c.Count
Else
lCount = lCount + 1
End If
End If
Next c
CountColors = lCount
End Function

Sub TEST()
MsgBox CountColors("825735")
End Sub

Hope this helps,

Hutch

"John" wrote:

I had this neat macro that counts how many cells are a certain color:



Public Function CountColors(color As String) As Integer

Dim intCount As Integer

Dim cell As Range



For Each cell In Range("ThisRange").Cells

If cell.Interior.color = 825735 Then intCount = intCount + 1

Next cell

CountColors = intCount

End Function



The problem is, I have merged some cells. How can I get a macro to count
say, 3 cells merged into 1 as three cells, not 1, if they match a certain
color?.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Count merged cells as if unmerged

Count the number of merged cells:

If cell.Interior.color = 825735 Then intCount = intCount +
cell.MergeArea.Cells.Count

cell.MergeArea.Cells.Count will return 1 for cells that are not merged....

HTH,
Bernie
MS Excel MVP


"John" wrote in message
...
I had this neat macro that counts how many cells are a certain color:



Public Function CountColors(color As String) As Integer

Dim intCount As Integer

Dim cell As Range



For Each cell In Range("ThisRange").Cells

If cell.Interior.color = 825735 Then intCount = intCount + 1

Next cell

CountColors = intCount

End Function



The problem is, I have merged some cells. How can I get a macro to count
say, 3 cells merged into 1 as three cells, not 1, if they match a certain
color?.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Count merged cells as if unmerged

Hi Bernie,

The OP's question is highly ambiguous, but wouldn't adding
cell.MergeArea.Cells.Count result in an overly excessive count, where
multiple cells in a MergeArea are looped.

(My guess is the intention of the question is the opposite of what it says,
only count the first cell in the mergarea that matches the condition, but I
may well be wrong!)

Regards,
Peter T

"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Count the number of merged cells:

If cell.Interior.color = 825735 Then intCount = intCount +
cell.MergeArea.Cells.Count

cell.MergeArea.Cells.Count will return 1 for cells that are not merged....

HTH,
Bernie
MS Excel MVP


"John" wrote in message
...
I had this neat macro that counts how many cells are a certain color:



Public Function CountColors(color As String) As Integer

Dim intCount As Integer

Dim cell As Range



For Each cell In Range("ThisRange").Cells

If cell.Interior.color = 825735 Then intCount = intCount + 1

Next cell

CountColors = intCount

End Function



The problem is, I have merged some cells. How can I get a macro to count
say, 3 cells merged into 1 as three cells, not 1, if they match a certain
color?.





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Count merged cells as if unmerged

Peter,

Only the OP knows for sure, but the question was:

How can I get a macro to count say, 3 cells merged into 1 as three cells, not 1


Which will give the wrong result if the selection covers the merged cell, but if the OP is stepping
down a column, for example, then it would work.

HTH,
Bernie
MS Excel MVP


"Peter T" <peter_t@discussions wrote in message ...
Hi Bernie,

The OP's question is highly ambiguous, but wouldn't adding cell.MergeArea.Cells.Count result in an
overly excessive count, where multiple cells in a MergeArea are looped.

(My guess is the intention of the question is the opposite of what it says, only count the first
cell in the mergarea that matches the condition, but I may well be wrong!)

Regards,
Peter T

"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Count the number of merged cells:

If cell.Interior.color = 825735 Then intCount = intCount + cell.MergeArea.Cells.Count

cell.MergeArea.Cells.Count will return 1 for cells that are not merged....

HTH,
Bernie
MS Excel MVP


"John" wrote in message
...
I had this neat macro that counts how many cells are a certain color:



Public Function CountColors(color As String) As Integer

Dim intCount As Integer

Dim cell As Range



For Each cell In Range("ThisRange").Cells

If cell.Interior.color = 825735 Then intCount = intCount + 1

Next cell

CountColors = intCount

End Function



The problem is, I have merged some cells. How can I get a macro to count say, 3 cells merged
into 1 as three cells, not 1, if they match a certain color?.









  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Count merged cells as if unmerged

Indeed that's one (likely) interpretation of the question I didn't think
of.,
- and cells merged vertically in that column, oh I give up <g

Regards,
Peter T

"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Peter,

Only the OP knows for sure, but the question was:

How can I get a macro to count say, 3 cells merged into 1 as three cells,
not 1


Which will give the wrong result if the selection covers the merged cell,
but if the OP is stepping down a column, for example, then it would work.

HTH,
Bernie
MS Excel MVP


"Peter T" <peter_t@discussions wrote in message
...
Hi Bernie,

The OP's question is highly ambiguous, but wouldn't adding
cell.MergeArea.Cells.Count result in an overly excessive count, where
multiple cells in a MergeArea are looped.

(My guess is the intention of the question is the opposite of what it
says, only count the first cell in the mergarea that matches the
condition, but I may well be wrong!)

Regards,
Peter T

"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Count the number of merged cells:

If cell.Interior.color = 825735 Then intCount = intCount +
cell.MergeArea.Cells.Count

cell.MergeArea.Cells.Count will return 1 for cells that are not
merged....

HTH,
Bernie
MS Excel MVP


"John" wrote in message
...
I had this neat macro that counts how many cells are a certain color:



Public Function CountColors(color As String) As Integer

Dim intCount As Integer

Dim cell As Range



For Each cell In Range("ThisRange").Cells

If cell.Interior.color = 825735 Then intCount = intCount + 1

Next cell

CountColors = intCount

End Function



The problem is, I have merged some cells. How can I get a macro to
count say, 3 cells merged into 1 as three cells, not 1, if they match a
certain color?.










  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 57
Default Count merged cells as if unmerged

I appreciate all the help. I decided to unmerge my cells, since I couldn't
take the risk of the cell count being inaccurate.

"Peter T" <peter_t@discussions wrote in message
...
Indeed that's one (likely) interpretation of the question I didn't think
of.,
- and cells merged vertically in that column, oh I give up <g

Regards,
Peter T

"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Peter,

Only the OP knows for sure, but the question was:

How can I get a macro to count say, 3 cells merged into 1 as three
cells,
not 1


Which will give the wrong result if the selection covers the merged cell,
but if the OP is stepping down a column, for example, then it would work.

HTH,
Bernie
MS Excel MVP


"Peter T" <peter_t@discussions wrote in message
...
Hi Bernie,

The OP's question is highly ambiguous, but wouldn't adding
cell.MergeArea.Cells.Count result in an overly excessive count, where
multiple cells in a MergeArea are looped.

(My guess is the intention of the question is the opposite of what it
says, only count the first cell in the mergarea that matches the
condition, but I may well be wrong!)

Regards,
Peter T

"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Count the number of merged cells:

If cell.Interior.color = 825735 Then intCount = intCount +
cell.MergeArea.Cells.Count

cell.MergeArea.Cells.Count will return 1 for cells that are not
merged....

HTH,
Bernie
MS Excel MVP


"John" wrote in message
...
I had this neat macro that counts how many cells are a certain color:



Public Function CountColors(color As String) As Integer

Dim intCount As Integer

Dim cell As Range



For Each cell In Range("ThisRange").Cells

If cell.Interior.color = 825735 Then intCount = intCount + 1

Next cell

CountColors = intCount

End Function



The problem is, I have merged some cells. How can I get a macro to
count say, 3 cells merged into 1 as three cells, not 1, if they match
a
certain color?.












Reply
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
How to count merged cells Excel Ella Excel Worksheet Functions 2 April 9th 10 10:28 PM
Count merged cells jlclyde Excel Discussion (Misc queries) 1 October 8th 09 01:08 PM
Count with merged cells Excelerator Excel Discussion (Misc queries) 2 July 23rd 09 03:50 AM
Creating Serial Additions of Numbers Formatted "M-N" In Merged and Unmerged Cells Patient Guy Excel Worksheet Functions 2 May 29th 09 02:56 PM
Retain value of merged cell in all unmerged cells kari Excel Discussion (Misc queries) 3 July 15th 08 12:36 AM


All times are GMT +1. The time now is 08:00 AM.

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

About Us

"It's about Microsoft Excel"