×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Loop within INDIRECT function

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
 

RE: Loop within INDIRECT function

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)

RE: Loop within INDIRECT function

(OP)
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

RE: Loop within INDIRECT function

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

RE: Loop within INDIRECT function

(OP)
Hello!

Thanks. That did the trick.

:)

Best wishes,

XLNew
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources