VBA does not recognize Excel worksheet function "indirect"

A

AJ

I have a piece of Excel code as follows

Public Function Refer(ByVal SheetName, ByVal CellName) As Variant
Dim cellref As String
cellref = "'" & SheetName & "'!" & CellName
Refer = Indirect(cellref)
End Function

When I compile this VBA project, I get a 'function not defined' error on the
Indirect function.

Any ideas? I have ensured all VBA libraries are referenced.
 
L

Leith Ross

AJ;165121 said:
I have a piece of Excel code as follows

Public Function Refer(ByVal SheetName, ByVal CellName) As Variant
Dim cellref As String
cellref = "'" & SheetName & "'!" & CellName
Refer = Indirect(cellref)
End Function

When I compile this VBA project, I get a 'function not defined' error
on the
Indirect function.

Any ideas? I have ensured all VBA libraries are referenced.

Hello AJ,

You're right. VBA doesn't support the worksheet function "Indirect".
You can achieve the this by taking the Range of a Range.

Refer = Range(Range(cellref))

Sincerely,
Leith Ross
 
M

macropod

Hi AJ,

INDIRECT is not one of the Worksheet Functions available to VBA - see "List of Worksheet Functions Available to Visual Basic" in
Excel's help file.
 
P

Peter T

Normally to call a worksheet function in VBA need to do it like this
result = Application.WorksheetFunction.funcName()

However, as macropod has pointed out, Indirect is not an available
WorksheetFunction.

But why not simply -
Refer = Range(cellref).Value

or
Refer = Worksheets(SheetName).Range(CellName).Value

or if you already have a reference to the sheet as is typically the case
Refer = ws.Range(CellName).Value

(using your argument names and assuming all is to refer to the Active
workbook)

Regards,
Peter T
 

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