Loop within INDIRECT function
Loop within INDIRECT function
(OP)
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
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





RE: Loop within INDIRECT function
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)
RE: Loop within INDIRECT function
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
RE: Loop within INDIRECT function
RE: Loop within INDIRECT function
Thanks. That did the trick.
:)
Best wishes,
XLNew