dougmarkham
Active Member
- Joined
- Jul 19, 2016
- Messages
- 252
- Office Version
- 365
- Platform
- Windows
Hi Folks,
I am working on a project to highlight cells red if they exceed certain character lengths
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]User ID[/TD]
[TD]Name[/TD]
[TD]Address 1[/TD]
[TD]Address 2[/TD]
[TD]Address 3[/TD]
[TD]Address 4[/TD]
[TD]Town / City[/TD]
[TD]Postcode[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
For instance, I want to use VBA to highlight any cells in the User ID column (col A) if the cells text string > 20 characters; however, if a person then deletes some of the characters so the character string length drops <20, I want the cell's red highlight to disappear. Also, if a person were to clear the contents of a cell that contained >20 (i.e., it's highlighted red already), I want the VBA to remove the red highlight.
I found and adapted some VBA:
The code works, all except the bit that removes highlighting from cells that have their contents cleared (blank cells). Curious thing is that the VBA does its job for cell A2 only. For instance, when you enter a text string of 30 characters, the cell highlights red. When you then clear the contents of cell A2, the red highlight disappears. However, if you do the same process for cell A3 downwards, when you clear the cell contents, the red highlight remains.
Would anybody be able to suggest a way to modify this code (the bit highlighted in red or blue) in order to make it operate in all cells of column A2 downwards?
Kind regards,
Doug.
I am working on a project to highlight cells red if they exceed certain character lengths
[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]User ID[/TD]
[TD]Name[/TD]
[TD]Address 1[/TD]
[TD]Address 2[/TD]
[TD]Address 3[/TD]
[TD]Address 4[/TD]
[TD]Town / City[/TD]
[TD]Postcode[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
For instance, I want to use VBA to highlight any cells in the User ID column (col A) if the cells text string > 20 characters; however, if a person then deletes some of the characters so the character string length drops <20, I want the cell's red highlight to disappear. Also, if a person were to clear the contents of a cell that contained >20 (i.e., it's highlighted red already), I want the VBA to remove the red highlight.
I found and adapted some VBA:
Code:
Private Sub Worksheet_Change(ByVal Target As RANGE)Dim c As RANGE
Dim LR As Integer
Dim numProbs As Long
Dim sht As Worksheet
Set sht = Worksheets("AddressData")
numProbs = 0
LR = sht.Cells(Rows.Count, "A").End(xlUp).row
For Each c In sht.RANGE("$A$2:$A" & LR).Cells
If Len(c.Value) > 20 Then
c.EntireRow.Cells(1).Resize(1, 1).Interior.Color = vbRed
numProbs = numProbs + 1
End If
Next
[COLOR=#b22222][B]For Each c In sht.RANGE("$A$2:$A" & LR).Cells[/B][/COLOR]
[COLOR=#0000cd][B] If c.Value = "" Then[/B][/COLOR]
c.EntireRow.Cells(1).Resize(1, 1).Interior.Color = xlNone
numProbs = numProbs + 1
End If
Next
For Each c In sht.RANGE("$A$2:$A" & LR).Cells
If Len(c.Value) < 21 Then
c.EntireRow.Cells(1).Resize(1, 1).Interior.Color = xlNone
numProbs = numProbs + 1
End If
Next
If numProbs > 0 Then
MsgBox "Character limits: Col A (20) - see red cells"
End If
End Sub
The code works, all except the bit that removes highlighting from cells that have their contents cleared (blank cells). Curious thing is that the VBA does its job for cell A2 only. For instance, when you enter a text string of 30 characters, the cell highlights red. When you then clear the contents of cell A2, the red highlight disappears. However, if you do the same process for cell A3 downwards, when you clear the cell contents, the red highlight remains.
Would anybody be able to suggest a way to modify this code (the bit highlighted in red or blue) in order to make it operate in all cells of column A2 downwards?
Kind regards,
Doug.