VBA - opening a new workbook causes #VALUE! error.

CosmoPants

New Member
Joined
Mar 9, 2011
Messages
7
Afternoon,

I've been trying to write a function in VBA to find the last value in a column, but return an offset value relative to it. In the function, the user can specify a worksheet and a column, but also an "offset" value as an integer, so that "0" will return the last value in the column, "1" will return the last-but-one, "2" will return the value two rows before the last value, and so on.

It works ok, but whenever I open a new workbook and switch back to the workbook with this function in it, all the cells containing the function now display a #VALUE! error.

I'm sure I must be missing something pretty obvious. Any help or suggestions greatly appreciated!

The variable "Lock_Worksheet" is a worksheet name, "x" is a column letter, and "y" is the number of rows from the last one you'd like returned.


Code below the line -
The variable "Lock_Worksheet" is a worksheet name, x is a column letter, and y is the number of rows from the last one you'd like returned.

--------------------------------

Option Explicit
Option Base 1

Public Function FIND_RECENT2(Lock_Worksheet As String, x As String, y As Integer)
Application.Volatile

Dim k As String
Dim LastRow

k = Lock_Worksheet & "!" & x & "1000"
LastRow = Lock_Worksheet & "!" & range(k).End(xlUp).Address

FIND_RECENT2 = range(LastRow).Offset(-y, 0)

End Function

----------------------------------

Any thoughts?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

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