2

I am trying to write a function in Excel that:

  1. Iterates through each worksheet
  2. Checks if the supplied string exists in the supplied cell
  3. Adds a predetermined cell value to the return value if step 2 evaluates to True

I've been stepping through my function and am getting the correct values until the function has to add the 4th cell to my return value.

Anyone have any idea what's going on? Thanks!


Function Revenue(row As Integer, col As Integer, str As String) As Integer

    Dim i As Integer

        For i = 2 To Worksheets.Count

            If Worksheets(i).Cells(row, col) = str Then

                Revenue = Revenue + Worksheets(i).Cells(21, 2) // Bug occurs on 4th iteration
                Debug.Print Revenue

            End If

        Next i


End Function
4
  • What error are you getting, exactly? Commented Nov 6, 2013 at 19:28
  • 4
    I bet it's Overflow as Revenue is an integer. Maybe someone is making too much money :-) Commented Nov 6, 2013 at 19:31
  • I'm not seeing an error, my code simply stops running and I get a #VALUE as a result. I don't think it's an overflow issue b/c it bugs out on the fourth iteration whether Revenue = 50,000 or 30,000. Is there a debug tool that I'm not using correctly? Thanks! Commented Nov 6, 2013 at 19:37
  • 32767 is the max limit for a signed integer. Commented Nov 6, 2013 at 19:57

1 Answer 1

2

You need to establish values for row and col prior to using them.

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

4 Comments

They are declared in the function signature. It's not in the code formatting block though.
Then check the values in Cells(21,2) for each worksheet to insure they are nice numbers rather than Text.
They are always numbers, never text. I double checked.
Then change all Integer into Double in the function header and Dims

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.