VBA code to apply conditional formatting to newly created cell

CSS2018

New Member
Joined
Feb 5, 2018
Messages
5
I have a timekeeping sheet that has a macro to insert a new employee and employee number. I want to apply conditional formatting to cell C10 that basically says. If the Employee number they entered was blank, highlight red. And then once they enter the 6 digit employee number it returns to no fill.


Code:
Sub New_Employee()
'
' New_Employee Macro
'


'
    Dim EmployeeName As String
    EmployeeName = InputBox("Enter Employee Name:" & vbCrLf & "[Firstname Lastname]", "", "")
    
    If EmployeeName = "" Then
        MsgBox "No employee was added."
        Exit Sub
    End If
    
    On Error Resume Next
    Dim EmployeeNumber As Variant
    EmployeeNumber = InputBox("Please enter employee number.", "", "")
    
    
    Rows("10:10").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("B10").Value = EmployeeName
    Range("C10").Value = EmployeeNumber
    
    Range("B10").Select
    ActiveWorkbook.Worksheets("Project DATA").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Project DATA").Sort.SortFields.Add Key:=Range( _
        "B10"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Project DATA").Sort
        .SetRange Range("B10:C999")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Rather than using CF, why not do this
Code:
    EmployeeNumber = InputBox("Please enter employee number.", "", "")
    If Len(EmployeeNumber) < 6 Then
      Range("C10").Interior.Color = vbRed
      Exit Sub
   Else
      Range("C10").Interior.Color = xlNone
   End If
 
Upvote 0
This won't allow the user to enter a name at all. I want to let the user to be able to enter the employee anyway even if they don't know the employee number.
 
Upvote 0
In that case simply remove the Exit Sub line
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,986
Members
449,058
Latest member
oculus

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