Create VBA function (UDF) in Excel 2003

H

Hershmab

How do I code a function
CellVal(rownum, colnum [, sheetname])
that will be the equivalent of the worksheet formula:
INDIRECT(ADDRESS(rownum, colnum [, sheetname]))
that will return the value of the specified single cell?

My knowledge of VBA is not good enough for me to find how to specify a
single-cell RANGE by its co-ordinates.
 
J

Jacob Skaria

Try the below. Sheetname is optional..

Function CellVal(lngRow As Long, lngColumn As Long, _
Optional strSheet As String) As Variant
If strSheet = vbNullString Then strSheet = Application.Caller.Worksheet.Name
CellVal = Sheets(strSheet).Cells(lngRow, lngColumn).Value
End Function

If this post helps click Yes
 
C

Charles Williams

Needs to be Volatile

Function CellVal(lngRow As Long, lngColumn As Long, _
Optional strSheet As String) As Variant

Application.Volatile
on Error GoTo Fail:
If strSheet = vbNullString Then strSheet = Application.Caller.Parent.Name
CellVal = Sheets(strSheet).Cells(lngRow, lngColumn).Value
exit function
Fail:
CellVal=cverr(xlerrna)
End Function

Charles
___________________________________
The Excel Calculation Site
http://www.decisionmodels.com

Jacob Skaria said:
Try the below. Sheetname is optional..

Function CellVal(lngRow As Long, lngColumn As Long, _
Optional strSheet As String) As Variant
If strSheet = vbNullString Then strSheet =
Application.Caller.Worksheet.Name
CellVal = Sheets(strSheet).Cells(lngRow, lngColumn).Value
End Function

If this post helps click Yes
---------------
Jacob Skaria


Hershmab said:
How do I code a function
CellVal(rownum, colnum [, sheetname])
that will be the equivalent of the worksheet formula:
INDIRECT(ADDRESS(rownum, colnum [, sheetname]))
that will return the value of the specified single cell?

My knowledge of VBA is not good enough for me to find how to specify a
single-cell RANGE by its co-ordinates.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top