Changing Cell Pattern\Color based on length of text in cell.

dhosi439

Board Regular
Joined
May 13, 2009
Messages
62
I have a shift schedule setup. Each half hour is marked by a single character, either an l or t.

I am trying to use VBA to change the cell pattern/colors. If a user inputs more than one character the code will change the cell formatting. I would also like for it to prompt the user that they have entered invalid characters, not sure where the best spot for this would be.

Here is my code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim C As Range

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("MondaySchedule")) Is Nothing Then
    For Each C In Target
        If Not (Target = 0) Then
            If Len(Trim(C)) > 1 Then
                With C.Interior
                    .Color = RGB(0, 0, 255)
                    .Pattern = xlPatternHorizontal
                    .PatternColor = RGB(0, 255, 0)
                End With
            End If
        
          '  MsgBox "Multiple Characters in Cell!"
          '  Exit Sub
 
        End If
    Next cell
End If

ws_exit:
Application.EnableEvents = True
End Sub

Note: "MondaySchedule" refers to a named range of "C5:S10"

Absolutely nothing happens. I don't know what I am missing. Any help would be appreciated.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I can and I have that working, however I can't setup a prompt in Conditional Formating. This is my primary reason for going with VBA for this issue. I don't even need to format the cells in VBA I just need it to check if there is more then one character in a cell and then tell the user.
 
Upvote 0
something along the lines of

dim c as range, rng
set rng=range("a1:z100")
for each c in rng
if len(c)>1 then
msgbox "morethan 1 character in cell "c.address
end if
next c
 
Upvote 0
I prefer using named ranges and this code does work elsewhere, I may not be referencing my Len formula correctly.

Here is my new code:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim cell As Range

On Error GoTo ws_exit:
Application.EnableEvents = False
If Not Intersect(Target, Me.Range("MondaySchedule")) Is Nothing Then
    For Each cell In Target
        If Not (Target = 0) Then
            If Len(cell) > 1 Then
                MsgBox "Multiple Characters in Cell!"
            End If
        End If
    Next cell
End If

ws_exit:
Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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