Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Loop within INDIRECT function

Status
Not open for further replies.

XLnew

Computer
Dec 21, 2008
12
0
0
US
Hello All,

I have a rather strange problem in VBA.

I have a simple loop, something like:

'+++++++++++++++++++++++
For i=1 To 10
.. 'statements
.. 'statements
Range("C20").Select
ActiveCell.FormulaR1C1 = "=INDIRECT(cells(8,i))"
Next i

'+++++++++++++++++++++++

Unfortunately, it looks like VBA does not like to loop within INDIRECT command - Why so?

What is the way around this problem?

Many thanks everyone & have a wonderful year ahead.

Best Wishes,

XLNew
 
Replies continue below

Recommended for you

The following line is your problem:

ActiveCell.FormulaR1C1 = "=INDIRECT(cells(8,i))"

This line of code will put =INDIRECT(cells(8,i)) into the formula of the active cell. Type that formula into a cell and it won't work. I'm really not sure what you are wanting, but if you want the argument of INDIRECT to be the content of the cell referenced by Cells(8, i), VBA won't evaluate Cells(8, i) if it's inside the quotes. You need to use concatenation to build the string properly, as:

"=INDIRECT(" & Cells(8,i) & ")"

-handleman, CSWP (The new, easy test)
 
Hi:

Well you have understood my problem correctly - i.e I simply want to loop over columns using the INDIRECT function.

However,

> "=INDIRECT(" & Cells(8,i) & ")"

Does not work. Something like

"=INDIRECT(" & Cells(8,2) & ")"

does not work either.


The problem is for example, on Cells(8,2), I have a link to another work sheet and a set of results.

Cells(8,2) reads something like: mySheet.xls!myResult

Similarly, Cells(8,3) has a different sheet and results and therefore, I would like to loop over this.

Best,

XLNew
 
Status
Not open for further replies.
Back
Top