User Definded Function in Worksheet_Change Event

Gregorys05

Board Regular
Joined
Sep 24, 2008
Messages
217
Hi All,
I am trying to get the below code to add a formula to cell after i type a value in, for example.
i type 140 in Cell B2 and the code the puts the formula =Calctime(140) in B2.

I have the code for the Formula:(Below) but i keep getting the error 1004

Also When i just Hit end on the debug the formula is in the right cell with the right value :confused:
Any Ideas??

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
Dim Test As String
Dim T As Variant
If Target.Cells.Count > 1 Then Exit Sub
i = Target.Column
If Intersect(ActiveCell, Range("A1:J13")) Is Nothing Then
        Exit Sub
 
    Else
 
Test = Target.Value
Target.Offset(0, 0).FormulaR1C1 = "=CalcTime(" & Test & ")"
 
 
End If
 
End Sub
Code:
Function CalcTime(Seconds) As String
    Dim tData1, tData2
    If Not IsNumeric(Seconds) Then Exit Function
    If Seconds < 60 Then
        If Len(Seconds) = 1 Then
            CalcTime = "00:0" & Seconds
        Else
            CalcTime = "00:" & Seconds
        End If
    Else
        tData1 = RoundDown(Seconds / 60)
        tData2 = Seconds - tData1 * 60
 
        If Len(tData1) = 1 Then
            tData1 = "0" & tData1
        End If
 
        If Len(tData2) = 1 Then
            tData2 = "0" & tData2
        End If
 
        CalcTime = tData1 & ":" & tData2
 
    End If
End Function
Function RoundDown(Number)
    Dim tData1
    tData1 = InStr(Number, ".")
    If tData1 = 0 Then
        RoundDown = Number
    Else
        RoundDown = Left(Number, tData1 - 1)
    End If
End Function
 
I'd turn events off too:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
If Target.Count > 1 Then Exit Sub
on error resume next
Application.Enableevents = False
    Set rng = Range("B:B")
    If Not Intersect(Target, rng) Is Nothing Then
        If Target.Value >= 0 Then Target.Offset(0, -1).Value = Now()
    End If
    Set rng = Range("H:H")
    If Not Intersect(Target, rng) Is Nothing Then
        If Target.Value >= 0 Then Target.Offset(0, -1).Value = Now()
    End If
   Application.EnableEvents = True
   On Error Goto 0
    ActiveWorkbook.Save
End Sub
 
Upvote 0

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Forum statistics

Threads
1,225,155
Messages
6,183,208
Members
453,151
Latest member
Lizamaison

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