Help with Call function not returning correct result

NewtVBA

New Member
Joined
Feb 23, 2017
Messages
4
I am very new to VBA and have written a function that returns a number from a range. My issue is that I want to write several other similar functions and the use the Call command to return the results of the functions. However, when I try to call the function I have already written (which works on its own) the result that shows up when I test it is 0.

Can anyone help by letting me know what I am doing wrong and how to fix it? Here is the first function I have written:

Code:
Function B02SP8(grade, step)
    If grade = "B02" And step = 1 Then
    B02SP8 = Sheets("Sheet2").Range("F55")
    ElseIf grade = "B02" And step = 2 Then
    B02SP8 = Sheets("Sheet2").Range("G55")
    ElseIf grade = "B02" And step = 3 Then
    B02SP8 = Sheets("Sheet2").Range("H55")
    ElseIf grade = "B02" And step = 4 Then
    B02SP8 = Sheets("Sheet2").Range("I55")
    ElseIf grade = "B02" And step = 5 Then
    B02SP8 = Sheets("Sheet2").Range("J55")[/INDENT]
[INDENT]    Else
    B02SP8 = "Grade/Step not included in function"
    End If
    B02SP8 = Application.Round(B02SP8, 2)
End Function[/INDENT]
[INDENT]
However, when I try to Call it using:
Code:
Function HR(grade, step)
    Call B02SP8(grade, step)
End Function

The result always returns a value of 0 instead of the number in the range the Called function should reference. My ultimate goal is to call several similar functions at the same time.

Any advice would be much appreciated!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Welcome to the forum.

You need to return a value from your HR function by assigning the result to HR like this:

Code:
Function HR(grade, step)
    HR = B02SP8(grade, step)
End Function
 
Upvote 0
Thank you so much! I knew it was going to be a simple fix, I just could not figure it out. Thanks again.
 
Upvote 0
If you are still around, I have a follow up question. As I mentioned in my original post I am hoping to have HR call several functions. Right now with your tip I have:
Code:
Function HR(grade, step)
    HR = B02SP8(grade, step)
    HR = S04T10(grade, step)
    HR= F01R08(grade, step)
    HR = C01(grade, step)
End Function
However, as I am sure you can imagine, it only works for the last line. Is there some way I can have it test the first functions for the correct entries and move on the next one if they don't exist (and it therefore comes up with no answer)? If you look back at my original post, the first function is a string of "if, then" statements, the new functions are nearly identical with only the values being different (i.e. the "step" value will continue to increase and the "grade" value changes several times as well).

Thanks for your help!
 
Upvote 0
If each function either returns a number or an error string, you could use something like this:

Code:
Function HR(grade, step)
    HR = B02SP8(grade, step)
    If not Isnumeric(HR) Then
        HR = S04T10(grade, step)
        if not isnumeric(HR) then
            HR= F01R08(grade, step)
            If not isnumeric(HR) then HR = C01(grade, step)
        end if
    end if
End Function

By the way, your first function will error if the values aren't matched because the last line will try and round a string.
 
Upvote 0
You are brilliant! Your suggestion worked perfectly, I was about to try another series of "if,then" statements but your solution is so much more elegant. You have saved me hours of work, thank you so much!
 
Upvote 0

Forum statistics

Threads
1,216,036
Messages
6,128,432
Members
449,452
Latest member
Chris87

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