Option Explicit
Const sfx = "_col"
Dim cArr
Dim Csaved As Boolean
Sub saveColors()
If Csaved Then
If vbNo = MsgBox("Colors are already saved. Do you want to erase the saved colors?", vbYesNo + vbQuestion + vbDefaultButton2) Then
Exit Sub
End If
End If
On Error Resume Next
Csaved = False
Erase cArr
Dim wb As Workbook
Set wb = ThisWorkbook
Dim sh As Worksheet, sh2 As Worksheet
Set sh = wb.ActiveSheet
Dim rng As Range, cc As Range
Set rng = sh.UsedRange
Dim i As Long, j As Long
ReDim cArr(0 To 2, 0 To 0)
For Each cc In rng
With cc
If .Interior.ColorIndex <> xlNone Then
ReDim Preserve cArr(0 To 2, 0 To i)
cArr(0, i) = .Parent.Name
cArr(1, i) = .Address(0, 0)
cArr(2, i) = .Interior.Color
.Interior.ColorIndex = xlNone
i = i + 1
End If
End With
Next cc
If i > 0 Then Csaved = True Else cArr = Null
ep:
On Error Resume Next
Set wb = Nothing
Set sh = Nothing
Set rng = Nothing
Set cc = Nothing
End Sub
Sub ResetColors()
If Not IsArray(cArr) Then
MsgBox "No colored cells are saved. Cannot restore them."
Exit Sub
End If
On Error Resume Next
Dim wb As Workbook
Set wb = ThisWorkbook
Dim sh As Worksheet, sh2 As Worksheet
Set sh = wb.ActiveSheet
Dim rng As Range, cc As Range
Set rng = sh.UsedRange
Dim i As Long, j As Long
With sh
For i = LBound(cArr, 2) To UBound(cArr, 2)
.Range(cArr(1, i)).Interior.Color = cArr(2, i)
Next i
End With
Erase cArr
Csaved = False
ep:
On Error Resume Next
Set wb = Nothing
Set sh = Nothing
Set rng = Nothing
Set cc = Nothing
End Sub