search and find location of colorindex = 16

abenitez77

Board Regular
Joined
Dec 30, 2004
Messages
149
I have several locations in my spreadsheet that have merged cell location that has the colorindex of 16. I want to save the address of the different locations in my sheet that have colorindex of 16 to several variables or an array.. How can i do this?

thanks
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this. Adr16 is the array you want. Here its printed to the Immediate Window, but you can replace that line with code to do whatever you want with the array.
Code:
Sub Sweet16()
Dim V As Variant, Adr16 As Variant, i As Long
With ActiveSheet.UsedRange
    V = .Cells.Value
    With Application
        .FindFormat.Clear
        .ReplaceFormat.Clear
        .FindFormat.Interior.ColorIndex = 16
    End With
    .Replace "*", "#N/A", SearchFormat:=True, ReplaceFormat:=False
    On Error Resume Next
    Adr16 = Split(.SpecialCells(xlCellTypeConstants, xlErrors).Address(0, 0), ",")
    For i = LBound(Adr16) To UBound(Adr16)
        Debug.Print Adr16(i)
    Next i
    .Cells.Value = V
End With
With Application
    .FindFormat.Clear
    .ReplaceFormat.Clear
End With
End Sub
 
Last edited:
Upvote 0
Try this. Adr16 is the array you want. Here its printed to the Immediate Window, but you can replace that line with code to do whatever you want with the array.
Code:
Sub Sweet16()
Dim V As Variant, Adr16 As Variant, i As Long
With ActiveSheet.UsedRange
    V = .Cells.Value
    With Application
        .FindFormat.Clear
        .ReplaceFormat.Clear
        .FindFormat.Interior.ColorIndex = 16
    End With
    .Replace "*", "#N/A", SearchFormat:=True, ReplaceFormat:=False
    On Error Resume Next
    Adr16 = Split(.SpecialCells(xlCellTypeConstants, xlErrors).Address(0, 0), ",")
    For i = LBound(Adr16) To UBound(Adr16)
        Debug.Print Adr16(i)
    Next i
    .Cells.Value = V
End With
With Application
    .FindFormat.Clear
    .ReplaceFormat.Clear
End With
End Sub

This worked, thanks! I have another thing I needed help with. The first section that it found was the range (L12:Y12). Within that section, there are 2 merged sections (first one is L12:R12, second one is S12:Y12). How do I get the 2 merged locations after finding the color location of L12:Y12 ?
 
Upvote 0
You are welcome. The modification below - modified the code already pasted and added a procedure for identifying merged ranges - will address your added request. The array ma contains the merged areas.
Code:
Sub Sweet16()
Dim V As Variant, Adr16 As Variant, i As Long
With ActiveSheet.UsedRange
    V = .Cells.Value
    With Application
        .FindFormat.Clear
        .ReplaceFormat.Clear
        .FindFormat.Interior.ColorIndex = 16
    End With
    .Replace "*", "#N/A", SearchFormat:=True, ReplaceFormat:=False
    On Error Resume Next
    Adr16 = Split(.SpecialCells(xlCellTypeConstants, xlErrors).Address(0, 0), ",")
    Debug.Print "Ranges with colorindex 16"
    For i = LBound(Adr16) To UBound(Adr16)
        Debug.Print Adr16(i)
    Next i
    .Cells.Value = V
End With
With Application
    .FindFormat.Clear
    .ReplaceFormat.Clear
End With
Call FindMergedCells
End Sub
Sub FindMergedCells()
Dim c As Range, Rws As Long, Cols As Long, mA() As String, ct As Long
Application.DisplayAlerts = False
For Each c In ActiveSheet.UsedRange
    If c.MergeCells Then
        ct = ct + 1
        Rws = c.MergeArea.Rows.Count
        Cols = c.MergeArea.Columns.Count
        ReDim Preserve mA(1 To ct)
        mA(ct) = c.Resize(Rws, Cols).Address(0, 0)
        c.UnMerge
    End If
Next c
If ct > 0 Then
    Debug.Print "Merged Ranges"
    For i = LBound(mA) To UBound(mA)
        Debug.Print mA(i)
        Range(mA(i)).Merge
    Next i
End If
End Sub
 
Upvote 0
JoeMo, the code works but it shows me all the merged cells found in the sheet. I only want to know the address of the merged cells for the colors found. Also, I noticed that when the color is found, it replaces the value in that merged cell for "#N/A". I need the value in that cell to save it in a variable for later.

thanks
 
Last edited:
Upvote 0
The #N/A is temporary unless you have failed to let the code run all the way through. This line:

.Cells.Value = V

restores the original data.

If you just want merged cells that have a colorindex 16 fill, then use this modification:
Code:
Sub Sweet16()
Dim V As Variant, Adr16 As Variant, i As Long
With ActiveSheet.UsedRange
    V = .Cells.Value
    With Application
        .FindFormat.Clear
        .ReplaceFormat.Clear
        .FindFormat.Interior.ColorIndex = 16
    End With
    .Replace "*", "#N/A", SearchFormat:=True, ReplaceFormat:=False
    On Error Resume Next
    Adr16 = Split(.SpecialCells(xlCellTypeConstants, xlErrors).Address(0, 0), ",")
    Debug.Print "Ranges with colorindex 16"
    For i = LBound(Adr16) To UBound(Adr16)
        Debug.Print Adr16(i)
    Next i
    .Cells.Value = V
End With
With Application
    .FindFormat.Clear
    .ReplaceFormat.Clear
End With
Call FindMergedCells
End Sub
Sub FindMergedCells()
Dim c As Range, Rws As Long, Cols As Long, mA() As String, ct As Long
Application.DisplayAlerts = False
For Each c In ActiveSheet.UsedRange
    If c.MergeCells And c.Interior.ColorIndex = 16 Then
        ct = ct + 1
        Rws = c.MergeArea.Rows.Count
        Cols = c.MergeArea.Columns.Count
        ReDim Preserve mA(1 To ct)
        mA(ct) = c.Resize(Rws, Cols).Address(0, 0)
        c.UnMerge
    End If
Next c
If ct > 0 Then
    Debug.Print "Merged Ranges"
    For i = LBound(mA) To UBound(mA)
        Debug.Print mA(i)
        Range(mA(i)).Merge
    Next i
End If
End Sub
 
Upvote 0
Sorry, you are correct, as I stepped thru to the end it did restore the original data.

Thanks again for your help, this worked perfectly for what I needed.
 
Upvote 0
Sorry, you are correct, as I stepped thru to the end it did restore the original data.

Thanks again for your help, this worked perfectly for what I needed.
You are welcome - thanks for the reply.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top