rjplante
Well-known Member
- Joined
- Oct 31, 2008
- Messages
- 574
- Office Version
- 365
- Platform
- Windows
I am running the macro listed below that converts time values entered as 705 or 1335 as 7:05 and 13:35. When I enter a value such as 705, I get an error that reads:
Run-time error '1004': Unable to set the NumberFormat property of the Range class
When the VBA debugger window opens, the line below is highlighted with red text.
Why is this formatting issue running into a problem? It has been working fine for months and now this has started popping up? What do I do to fix the issue?
Thanks for all the help,
Robert
Run-time error '1004': Unable to set the NumberFormat property of the Range class
When the VBA debugger window opens, the line below is highlighted with red text.
Why is this formatting issue running into a problem? It has been working fine for months and now this has started popping up? What do I do to fix the issue?
Thanks for all the help,
Robert
Rich (BB code):
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
'Define the range where you want the code to work (our example is "C:G").
'Change within the " marks
If Intersect(Target, Range("D17:W23")) Is Nothing Then Exit Sub
On Error GoTo errHandler:
With Target
If IsNumeric(.Value) Then
Application.EnableEvents = False
Select Case .Value
Case 0
.NumberFormat = "[h]:mm"
Case 1 To 99
.Value = TimeSerial(0, .Value, 0)
.NumberFormat = "[h]:mm"
Case 100 To 2399
.Value = TimeSerial(Int(.Value / 100), .Value Mod 100, 0)
.NumberFormat = "[h]:mm"
Case 10000 To 235959
.Value = TimeSerial(Int(.Value / 10000), _
Int((.Value Mod 10000) / 100), .Value Mod 100)
.NumberFormat = "[h]:mm:ss"
Case 240000 To 245959
.Value = TimeSerial(0, Int((.Value Mod 10000) / 100), .Value Mod 100)
.NumberFormat = "[h]:mm:ss"
Case Else
End Select
End If
End With
errHandler:
Application.EnableEvents = True
End Sub