D
D. Stacy
The followng block of code is producing Error Code 91 while dealing with the
variable "Length".
This code is ran against a verticle colum of text data that is either 1, 2,
3 or 4 characters in length and is numbers stored as text. The range
selected is typically the entire column. My ultimate goal here is to add the
necassary code that will add the appropriate number of leading zeros so that
all the data is 4 characters long.
Sub ConfigureTimeData()
' Counts Characters in Text String then runs code depending on Character
Count
Dim FormulaCells As Range, ConstantCells As Range
Dim Cell As Range
Dim Length As Double
Length = Cell.Characters.Count
If TypeName(Selection) <> "Range" Then Exit Sub
Application.ScreenUpdating = False
' Create subsets of original selection to avoid processing empty cells
On Error Resume Next
Set FormulaCells = Selection.SpecialCells(xlFormulas, xlNumbers)
Set ConstantCells = Selection.SpecialCells(xlConstants, xlTextValues)
On Error GoTo 0
' Process the formula cells
If Not FormulaCells Is Nothing Then
For Each Cell In FormulaCells
If Cell.Value < 2 Then
Cell.Interior.Color = RGB(0, 0, 0)
Else
Cell.Interior.Color = RGB(122, 100, 0)
End If
Next Cell
End If
' Process the constant cells
If Not ConstantCells Is Nothing Then
For Each Cell In ConstantCells
Select Case Length
Case 0
Exit Sub
Case 1
Cell.Interior.Color = RGB(255, 0, 0)
Case 2
Cell.Interior.Color = RGB(0, 255, 0)
Case 3
Cell.Interior.Color = RGB(0, 0, 255)
Case 4
Cell.Interior.Color = RGB(50, 0, 0)
Case Is > 5
Cell.Interior.Color = RGB(255, 0, 255)
End Select
Next Cell
End If
End Sub
variable "Length".
This code is ran against a verticle colum of text data that is either 1, 2,
3 or 4 characters in length and is numbers stored as text. The range
selected is typically the entire column. My ultimate goal here is to add the
necassary code that will add the appropriate number of leading zeros so that
all the data is 4 characters long.
Sub ConfigureTimeData()
' Counts Characters in Text String then runs code depending on Character
Count
Dim FormulaCells As Range, ConstantCells As Range
Dim Cell As Range
Dim Length As Double
Length = Cell.Characters.Count
If TypeName(Selection) <> "Range" Then Exit Sub
Application.ScreenUpdating = False
' Create subsets of original selection to avoid processing empty cells
On Error Resume Next
Set FormulaCells = Selection.SpecialCells(xlFormulas, xlNumbers)
Set ConstantCells = Selection.SpecialCells(xlConstants, xlTextValues)
On Error GoTo 0
' Process the formula cells
If Not FormulaCells Is Nothing Then
For Each Cell In FormulaCells
If Cell.Value < 2 Then
Cell.Interior.Color = RGB(0, 0, 0)
Else
Cell.Interior.Color = RGB(122, 100, 0)
End If
Next Cell
End If
' Process the constant cells
If Not ConstantCells Is Nothing Then
For Each Cell In ConstantCells
Select Case Length
Case 0
Exit Sub
Case 1
Cell.Interior.Color = RGB(255, 0, 0)
Case 2
Cell.Interior.Color = RGB(0, 255, 0)
Case 3
Cell.Interior.Color = RGB(0, 0, 255)
Case 4
Cell.Interior.Color = RGB(50, 0, 0)
Case Is > 5
Cell.Interior.Color = RGB(255, 0, 255)
End Select
Next Cell
End If
End Sub