Link to home
Start Free TrialLog in
Avatar of Steve Brown
Steve BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Excel Function works on one sheet but not another

I have written a short function in Excel to find a number in a range in column AZ and return the row number.  If the number is not found in then finds the next number following.  So if the list contains 070, 071, 073 then looking for 070 would find the row for 070 whereas looking for 072 would return the row for 073.  There is a similar function to work in reverse, so if the list contains 070, 071, 073 then looking for 073 would find the row for 073 whereas looking for 072 would return the row for 071.  The function works fine on the worksheet where I created it, but will not return a value when called from a different worksheet.  The function code is:

Function Start_Range(StartNum As String, CC As String)
    Dim Search_String As String
    Dim SearchNum As Integer
   
    SearchNum = Val(StartNum)
    Search_String = Format(CStr(SearchNum), "000") + CC
    Do While IsNumeric(Application.Match(Search_String, Worksheets("RDB1CY").Range("AZ:AZ"), 0)) = False
        SearchNum = SearchNum + 1
        Search_String = Format(CStr(SearchNum), "000") + CC
    Loop
    Start_Range = Application.Match(Search_String, Range("AZ:AZ"), 0)
End Function

When called from Worksheet RDB1CY the function operates as expected.  When it is called from any other worksheets it returns #N/A.

Can anybody point me to where I am going wrong?
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steve Brown

ASKER

Thanks.  I should have looked a bit more carefully at why the code was failing.  On the 1st example you gave me it was the revised Line 3, I hadn't set the return value to reference the worksheet name, hence the error.

Note to self, don't try writing code at 22:00!!
Don't worry it happens sometime with everyone.
Glad you identified the error in your code and you will remember not to repeat the same mistake in your future codes.

Bottom Line: Mistakes are important to learn to do things better in future. :)