Option Explicit
' ============================== S H E E T M A P =============================
' shg 2010-09, 2010-11, 2013-01
' Creates a sheet map to characterize the contents of each cell with a color,
' and, for non-empty cells, a two-character code
' Color:
' Dark Grey Empty
' Light Grey Formula
' Yellow A number or date stored as text
' Red An error
' White None of the above
' First character => formula or literal:
' L A literal
' F A formula
' < A formula the same as that at left
' ^ A formula the same as that above
' + A formula the same as those above and left
' Second character => type of value:
' $ String (from the type declaration character)
' @ Currency (from the type declaration character)
' # Double (from the type declaration character)
' D Date
' E Error
' B Boolean
Sub SheetMap()
Dim wksInp As Worksheet
Dim wksOut As Worksheet
Dim vZoom As Variant
Dim cell As Range
Dim avCellType As Variant
Dim bSU As Boolean
Dim iCalc As XlCalculation
Dim sWks As String
With Application
bSU = .ScreenUpdating
.ScreenUpdating = False
iCalc = .Calculation
.Calculation = xlCalculationManual
End With
Set wksInp = ActiveSheet
vZoom = ActiveWindow.Zoom
sWks = "Formula map for " & wksInp.Name
On Error Resume Next
Application.DisplayAlerts = False
Worksheets(sWks).Delete
Application.DisplayAlerts = True
ActiveWorkbook.Worksheets.Add(After:=wksInp).Name = sWks
Set wksOut = ActiveSheet
ActiveWindow.Zoom = vZoom
wksOut.Cells.Interior.Color = RGB(144, 144, 144)
For Each cell In wksInp.UsedRange
avCellType = CellType(cell)
With wksOut.Range(cell.Address)
.Value = avCellType(0)
.Interior.Color = avCellType(1)
End With
Next cell
#If True Then
wksInp.Rows(1).Copy
wksOut.Range("A1").PasteSpecial Paste:=xlPasteColumnWidths
#Else
wksOut.Columns.AutoFit
#End If
With Application
.ScreenUpdating = bSU
.Calculation = iCalc
End With
End Sub
Function CellType(r As Range) As Variant
Dim sTyp As String
Dim iCol As Long
Dim iCase As Long
Dim sFrm As String
With r(1)
If .HasFormula Then
sFrm = .FormulaR1C1
If .Column > 1 Then
If .Offset(0, -1).FormulaR1C1 = sFrm Then iCase = iCase + 1
End If
If .Row > 1 Then
If .Offset(-1, 0).FormulaR1C1 = sFrm Then iCase = iCase + 2
End If
sTyp = Mid("F<^+", iCase + 1, 1)
iCol = RGB(229, 229, 229)
Else
sTyp = "L"
iCol = vbWhite
End If
Select Case VarType(.Value)
Case vbEmpty
sTyp = vbNullString
iCol = RGB(144, 144, 144)
Case vbDouble
sTyp = sTyp & "#"
Case vbString
If IsNumeric(.Value2) Then
sTyp = sTyp & "#"
iCol = vbYellow
ElseIf IsDate(.Value2) Then
sTyp = sTyp & "D"
iCol = vbYellow
Else
sTyp = sTyp & "$"
End If
Case vbCurrency
sTyp = sTyp & "@"
Case vbDate
sTyp = sTyp & "D"
Case vbError
sTyp = sTyp & "E"
iCol = vbRed
Case vbBoolean
sTyp = sTyp & "B"
Case Else
Stop ' that would be a problem
End Select
End With
CellType = Array(sTyp, iCol)
End Function