Solved

Arrays & .formulaR1C1 in excel vba

Posted on 2007-03-30
5
3,741 Views
Last Modified: 2008-01-09
I am not real familiar with arrays, and the formulas are not working. Any ideas why?

With Array(Range(rST.Offset(0, 11).Address), Range(rLT.Offset(0, 11).Address), Range(rTI.Offset(0, 11).Address))
        .FormulaR1C1 = "=RC4/" & NetAssets
        .FormulaR1C1 = "=ROUND(RC[-1],3)"
        .FormulaR1C1 = "=ROUNDDOWN(RC[-2],3)"
End with

   
0
Comment
Question by:tiehaze
5 Comments
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 18824617
What are you trying to do? I can only guess that that should be:
rST.Offset(0, 11).FormulaR1C1 = "=RC4/" & NetAssets
rLT.Offset(0, 11).FormulaR1C1 = "=ROUND(RC[-1],3)"
rTI.Offset(0, 11).FormulaR1C1 = "=ROUNDDOWN(RC[-2],3)"
?
Regards,
Rory
0
 

Author Comment

by:tiehaze
ID: 18824719
Sorry, I want this:

rST.Offset(0, 11).FormulaR1C1 = "=RC4/" & NetAssets
rLT.Offset(0, 11).FormulaR1C1 = "=RC4/" & NetAssets
rTI.Offset(0, 11).FormulaR1C1 = "=RC4/" & NetAssets
rST.Offset(0, 12).FormulaR1C1 = "=ROUND(RC[-1],3)"
rLT.Offset(0, 12).FormulaR1C1 = "=ROUND(RC[-1],3)"
rTI.Offset(0, 12).FormulaR1C1 = "=ROUND(RC[-1],3)"
rST.Offset(0, 13).FormulaR1C1 = "=ROUNDDOWN(RC[-2],3)"
rLT.Offset(0, 13).FormulaR1C1 = "=ROUNDDOWN(RC[-2],3)"
rTI.Offset(0, 13).FormulaR1C1 = "=ROUNDDOWN(RC[-2],3)"
... there are actually more formulas, but I excluded them for the simplicity of the question
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 18824791
You can't work with arrays like that. Try this:

   Dim Target As Range
   For Each Target In Array(Range(rST.Offset(0, 11).Address), Range(rLT.Offset(0, 11).Address), Range(rTI.Offset(0, 11).Address))
      With Target
        .FormulaR1C1 = "=RC4/" & NetAssets
        .FormulaR1C1 = "=ROUND(RC[-1],3)"
        .FormulaR1C1 = "=ROUNDDOWN(RC[-2],3)"
       End with
   Next Target

Kevin
0
 
LVL 81

Expert Comment

by:byundt
ID: 18827698
I think you need two nested loops:

Dim frmla As Variant, Target As Variant
Dim i As Long
For Each frmla In Array("=RC4/" & NetAssets, "=ROUND(RC[-1],3)", "=ROUNDDOWN(RC[-2],3)")
    For Each Target In Array(rST.Offset(0, 11 + i).Address, rLT.Offset(0, 11 + i).Address, rTI.Offset(0, 11 + i).Address)
        Range(Target).FormulaR1C1 = frmla
    Next Target
    i = i + 1
Next frmla

0
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
ID: 18827919
Oh my! Thanks Brad...I really goofed that one...

You could also do it this way:

   Dim Target As Range
   Set Target = Union(rST, rLT, rTI)
   Target.Offset(0, 11).FormulaR1C1 = "=RC4/" & NetAssets
   Target.Offset(0, 12).FormulaR1C1 = "=ROUND(RC[-1],3)"
   Target.Offset(0, 13).FormulaR1C1 = "=ROUNDDOWN(RC[-2],3)"
   ...

Kevin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vbModal 12 30
FormulaArray VBA Issue 6 17
ActiveX Listbox Multi Select in Excel 2010 8 16
File not loading into PowerPivot 4 9
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now