Prevent Excel Worksheet_Change Errors When Clearing a Cell
Question details
Prevent a Worksheet_Change macro from processing cleared cells and ensure Excel events are restored after errors.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A worksheet macro converts hhmm entries into time values when cells in an input range are edited.
- Observed behavior
- Clearing an input cell can cause an error when the macro tries to convert the empty value.
Identify the input range and open the worksheet's code module in Excel. Save the workbook as a macro-enabled Excel workbook (.xlsm) and enable macros only if you trust the file.
Check for blank cells and restore Excel events safely
Use a blank-cell check and an error-handling exit to prevent conversion errors and recursive change events.
This example converts single-cell entries such as 930 or 0930 to 09:30. It ignores cleared cells, invalid hhmm values, error values, and edits to multiple cells.
In Excel, open Developer > Visual Basic. In the Project pane, double-click the worksheet that contains the input cells.
Replace the worksheet's existing Worksheet_Change procedure with the code below, or incorporate the checks into it if the existing procedure has other functions. Change Me.Range("A:A") to your actual input range. Private Sub Worksheet_Change(ByVal Target As Range) Dim n As Double On Error GoTo CleanExit If Intersect(Target, Me.Range("A:A")) Is Nothing Then Exit Sub If Target.CountLarge > 1 Then Exit Sub If IsError(Target.Value2) Then Exit Sub If Len(Trim$(CStr(Target.Value2))) = 0 Then Exit Sub If Not IsNumeric(Target.Value2) Then Exit Sub n = CDbl(Target.Value2) If n <> Fix(n) Or n < 0 Or n > 2359 Then Exit Sub If n Mod 100 >= 60 Then Exit Sub Application.EnableEvents = False Target.Value = TimeSerial(n \ 100, n Mod 100, 0) Target.NumberFormat = "hh:mm" CleanExit: Application.EnableEvents = True End Sub
Return to the worksheet and enter 930 in an input cell; it should display 09:30. Clear the cell, then try an invalid entry such as 1260 and confirm it remains unchanged.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and move everyday spreadsheet work to WPS; VBA event support depends on the WPS version and setup.

Frequently Asked Questions
Why does clearing a cell cause a Worksheet_Change error?
Clearing a cell triggers Worksheet_Change, so conversion code may receive an empty value. Check Len(Target.Value2) after confirming Target is a single cell, and exit before converting a blank.
Why should I disable events when the macro changes the cell?
Writing to Target can trigger Worksheet_Change again. Set Application.EnableEvents to False before writing, then restore it in an error-handling exit.
How do I turn Excel events back on if a macro left them disabled?
Open Developer > Visual Basic, open the Immediate window with View > Immediate Window, enter Application.EnableEvents = True, and press Enter.
Will this code convert a range of pasted values?
No. The Target.CountLarge check exits for multi-cell changes. To handle pasted ranges, loop through the relevant cells and validate each value before converting it.




