0

I want to assign the value of a cell in a specific worksheet to the function, but the function is returning a zero. Below is my simple code:

Function TLGV(Modele As String, Pressure As Integer) 'Total Load Grand Vitesse

    If Modele = "DCHC05" Then
        If Pressure = 30 Then
            Worksheets("VC Types").Activate
            TLGV = Range("H4").Value
        End If
    Else

    End If

1 Answer 1

1

You shouldn't be trying to Activate a worksheet from a VBA User Defined function.

Function TLGV(Modele As String, Pressure As Integer)

   'Total Load Grand Vitesse

   If Modele = "DCHC05" Then
       If Pressure = 30 Then
           TLGV = Worksheets("VC Types").Range("H4").Value
       End If
    Else
        'more stuff
    End If

End Function

In fact, you should provide a parent workbook reference as well.

Function TLGV(Modele As String, Pressure As Integer)

   'Total Load Grand Vitesse
   'reference the parent workbook
   with application.caller.parent.parent
     If Modele = "DCHC05" Then
       If Pressure = 30 Then
           TLGV = .Worksheets("VC Types").Range("H4").Value
       End If
      Else
        'more stuff
      End If
    End With

End Function
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.