Dynamic Arrays and sorting

haleybusche

New Member
Joined
Mar 7, 2011
Messages
13
I am not great with excel macros, so what I think I need is a dynamic array.

I have created a function that assigns the color of a cell to a number, and I need to store that number somewhere so that I can sort by it later.

The number of cells with colors that need to be assigned a number and stored, is dynamic.

Below is the function that assigns the color of the cell to a number

Code:
Function GetColor(rngIndex As Range, rngSource As Range) As Long
    Dim lngColor As Long
    Dim J As Integer

    Application.Volatile
    lngColor = rngSource.Interior.ColorIndex

    GetColor = 99       'Set to default color
    For J = 1 To rngIndex.Count
        If rngIndex(J).Interior.ColorIndex = lngColor Then
            GetColor = J
        End If
    Next J
End Function

I need help with code that will store that number. The color of the cell can also change, so I need the number to be updated when the color changes and be restored.

I also need help with the code that will then allow me to sort those numbers.

I know this is a little different, so if further discussion is necessary please feel free.

Thanks

Haley
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Here's a sample. The Microsoft article (above) gives some tips on how to sort the dictionary by key or by data.

Hope it helps.

Gary

Code:
Public Sub Test()

'Requires reference to Microsoft Scripting Runtime scrrun.dll
Dim oDict As Scripting.Dictionary 'Perhaps best Declared as Module Public

Set oDict = New Scripting.Dictionary

Dim oCell As Range
Dim oColorTest As Range
Dim vNode As Variant

Set oColorTest = ActiveSheet.Range("A1:A16")

'Fill range with random colors
For Each oCell In oColorTest
    oCell.Interior.ColorIndex = Int((56 * Rnd) + 1)
Next oCell

'Load dictionary: Cell address = key, Data = Color number
For Each oCell In oColorTest
    If oDict.Exists(oCell.Address) Then ' If key (cell address) exists, update color number
        oDict(oCell.Address) = oCell.Interior.ColorIndex
    Else
        oDict.Add oCell.Address, oCell.Interior.ColorIndex 'Add new key & color number
    End If
Next oCell

'Print dictionary contents
For Each vNode In oDict
    Debug.Print vNode & vbTab & oDict(vNode)
Next vNode

'Change a few colors and update the dictionary contents
ActiveSheet.Range("$A$7").Interior.ColorIndex = 3
oDict("$A$7") = 3
ActiveSheet.Range("$A$8").Interior.ColorIndex = 3
oDict("$A$8") = 3
ActiveSheet.Range("$A$9").Interior.ColorIndex = 3
oDict("$A$9") = 3

'Print dictionary contents
For Each vNode In oDict
    Debug.Print vNode & vbTab & oDict(vNode)
Next vNode

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,820
Members
452,946
Latest member
JoseDavid

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