Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use indirect reference to select a single cell or range in vba

I need simply a code for selecting a cell, however that cell to select changes. I have a cell in the workbook that will identify what cell it should be. Cell A1 contains the cell # that should be selected.

In this example cell A1 contains the word "P25", so I want the below code to reference A1 for the indirect cell ref to P25, thus select cell P25.

I tried both of these lines separately:

Sub IndirectCellSelect()

     Sheet.Range(INDIRECT(A1)).Select
     Range(INDIRECT(A1)).Select

End Sub

I get the error Sub or Function is not defined, when it gets to the word INDIRECT

like image 859
Bryan Avatar asked Oct 20 '22 07:10

Bryan


1 Answers

A slight alteration to the posted code works:

Range([indirect("a1")]).Select

but I would advise to try either of these instead:

Sheet.Range(Sheet.Range("A1").Value).Select
Range(Range("A1")).Select

the first being more explicit and is recommended in production code.

like image 169
lori_m Avatar answered Oct 22 '22 00:10

lori_m