Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8,520
Default Macro to merge cells of the same colour/pattern index

I have changed Selection to ActiveSheet.Used Range.....OR change it to a
definite range to suit your requirement.


Sub MergebyColorIndex()

Dim arrRange() As Range, arrIndex() As Variant
Dim cell As Range, blnFound As Boolean, intTemp As Integer

ReDim arrRange(0): ReDim arrIndex(0)

For Each cell In ActiveSheet.UsedRange
If cell.Interior.ColorIndex < xlColorIndexNone Then
blnFound = False

For intTemp = 1 To UBound(arrIndex)
If arrIndex(intTemp) = cell.Interior.ColorIndex Then
Set arrRange(intTemp) = Union(arrRange(intTemp), cell)
blnFound = True: Exit For
End If
Next

If blnFound = False Then
ReDim Preserve arrRange(intTemp)
ReDim Preserve arrIndex(intTemp)
arrIndex(intTemp) = cell.Interior.ColorIndex
Set arrRange(intTemp) = cell
End If

End If
Next

For intTemp = 1 To UBound(arrRange)
arrRange(intTemp).Merge
Next

End Sub

--
Jacob (MVP - Excel)


"Gary Capindale" wrote:

Hi guys,

I am hoping that some one here can help me,

First off i have a very, very basic knowledge of excel and vba so please
bear with me and i will try to describe what it is i am trying to do as best
as possible.

I have a worksheet that has a list of equipment down the left hand side and
week numbers going across the top 1 to 52. The spreadsheet is essentially a
plan of what equipment is being used by certain processes, represented by
coloured or patterned formatted cells in the weeks they will be in use. Kind
of like a gantt chart. Currently i have a spreadsheet were i copy the current
plans colours across to a template spreadsheet to tidy it up. I need to merge
and centre justify the cells in order to give each block a process name.

I am trying to go from left to right from weeks 1 to 52 and merge any cell
of the same colour or pattern if they are situated in direct proximity to one
another. I would also like to merge top to bottom as well as some processes
use multiple sets of equipment so would require to be merged across rows as
well.

I have tried to figure this out by recording macros, but they are massive
and don't work quite right. Obviously some kind of loops would be more
efficient. But i have no knowledge of how these things work really.

Thanks.

Gary.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Macro to merge cells of the same colour/pattern index

Wow, thanks for the swift reply. It has really helped me. It works perfectly
as a solve. As it is only a small spreadsheet i think.

How would i go about setting up a finite range as the cells go from E5 to
BD38 and will always remain this way, i would also like the merge cells to be
automatically set up with centred text.

Thanks Jacob your a regular excel guru and a life saver.

"Jacob Skaria" wrote:

I have changed Selection to ActiveSheet.Used Range.....OR change it to a
definite range to suit your requirement.


Sub MergebyColorIndex()

Dim arrRange() As Range, arrIndex() As Variant
Dim cell As Range, blnFound As Boolean, intTemp As Integer

ReDim arrRange(0): ReDim arrIndex(0)

For Each cell In ActiveSheet.UsedRange
If cell.Interior.ColorIndex < xlColorIndexNone Then
blnFound = False

For intTemp = 1 To UBound(arrIndex)
If arrIndex(intTemp) = cell.Interior.ColorIndex Then
Set arrRange(intTemp) = Union(arrRange(intTemp), cell)
blnFound = True: Exit For
End If
Next

If blnFound = False Then
ReDim Preserve arrRange(intTemp)
ReDim Preserve arrIndex(intTemp)
arrIndex(intTemp) = cell.Interior.ColorIndex
Set arrRange(intTemp) = cell
End If

End If
Next

For intTemp = 1 To UBound(arrRange)
arrRange(intTemp).Merge
Next

End Sub

--
Jacob (MVP - Excel)


"Gary Capindale" wrote:

Hi guys,

I am hoping that some one here can help me,

First off i have a very, very basic knowledge of excel and vba so please
bear with me and i will try to describe what it is i am trying to do as best
as possible.

I have a worksheet that has a list of equipment down the left hand side and
week numbers going across the top 1 to 52. The spreadsheet is essentially a
plan of what equipment is being used by certain processes, represented by
coloured or patterned formatted cells in the weeks they will be in use. Kind
of like a gantt chart. Currently i have a spreadsheet were i copy the current
plans colours across to a template spreadsheet to tidy it up. I need to merge
and centre justify the cells in order to give each block a process name.

I am trying to go from left to right from weeks 1 to 52 and merge any cell
of the same colour or pattern if they are situated in direct proximity to one
another. I would also like to merge top to bottom as well as some processes
use multiple sets of equipment so would require to be merged across rows as
well.

I have tried to figure this out by recording macros, but they are massive
and don't work quite right. Obviously some kind of loops would be more
efficient. But i have no knowledge of how these things work really.

Thanks.

Gary.

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
Macro to merge cells of the same colour/pattern index Jacob Skaria Excel Programming 0 May 27th 10 09:30 PM
colour pattern Veeshal Excel Discussion (Misc queries) 1 October 22nd 09 03:22 AM
macro for formatting/colour cells Mayte Excel Programming 2 January 22nd 09 02:46 PM
Colour and Pattern Formatting Cells Dave M[_2_] Excel Programming 3 March 29th 06 04:31 PM
How can I count cells of a specific colour (pattern)? Nick@Durham Excel Worksheet Functions 1 November 29th 05 10:18 AM


All times are GMT +1. The time now is 08:42 PM.

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"