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
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I think you need:

Application.EnableEvents = False

at the begiining of your code, and:

Application.EnableEvents = True

at the end.
 
Upvote 0
How can I have multiple Private Sub Worksheet_Change events...I am getting an ambiguious name error if I list them seperately
 
Upvote 0
You can't - you have to incorporate all the code into one routine (or call separate routines from the Change event)
 
Upvote 0
You can't have more than one Worksheet_Change subroutine per worksheet. Any code that you want to execute on Worksheet_Change has to be placed inside that one subroutine.
 
Upvote 0
How do I combine these routines then into one code


Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Target.Parent.Range("B:B")
If Target.Count > 1 Then Exit Sub
If Intersect(Target, rng) Is Nothing Then Exit Sub
If Target.Value >= 0 Then Target.Offset(0, -1).Value = Now()


End Sub
Private Sub Worksheet_Change(ByVal Target As Excel.Range)

ActiveWorkbook.Save

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Target.Parent.Range("H:H")
If Target.Count > 1 Then Exit Sub
If Intersect(Target, rng) Is Nothing Then Exit Sub
If Target.Value >= 0 Then Target.Offset(0, 4).Value = Now()

End Sub
 
Last edited:
Upvote 0
One way:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    Set rng = Target.Parent.Range("B:B")
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, rng) Is Nothing Then
        If Target.Value >= 0 Then Target.Offset(0, -1).Value = Now()
    End If
    Set rng = Target.Parent.Range("H:H")
    If Not Intersect(Target, rng) Is Nothing Then
        If Target.Value >= 0 Then Target.Offset(0, -1).Value = Now()
    End If
    ActiveWorkbook.Save
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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