Solved

Excel Macro Question - Autofill formula

Posted on 2010-09-17
3
615 Views
Last Modified: 2012-05-10
Hey everyone,

I am working on a macro in excel and I am having some issues with an autofill function.  I am trying to autofill a formula for all rows that have a value in column C.  My issue with my current macro is this....
when i recorded the macro, the macro recorded that i wanted to autofill the formula for cells D2:D539.  
VBA code example:
ActiveCell.FormulaR1C1 = "=IF(ISBLANK(RC[-2]),R[-1]C,(RC[-2]))"
    Range("D2").Select
    Selection.AutoFill Destination:=Range("D2:D539")


The number of rows will change each time I run this macro, could be more, could be less.  That is why I would like to apply this formula to only cells where the Same row column C has a value.  Is there a code i can type into this VBA string to help?
Thanks!
0
Comment
Question by:Hags2Rags
3 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 33704247
If you mean fill down to the last row occupied in column C then:

ActiveCell.FormulaR1C1 = "=IF(ISBLANK(RC[-2]),R[-1]C,(RC[-2]))"
    Range("D2").Select
    Selection.AutoFill Destination:=Range("D2:D" & ActiveSheet.Range("C" & ActiveSheet.Rows.Count).End(xlUp).Row)

Chris
0
 
LVL 8

Expert Comment

by:ragnarok89
ID: 33704330

for r = 2 to 5000 'assumes you'll always have less than 5000 rows
   if range("C" & r).value <> "" then range("D" & r).FormulaR1C1 = "=IF(ISBLANK(RC[-2]),R[-1]C,(RC[-2]))"
0
 

Author Closing Comment

by:Hags2Rags
ID: 33704372
Dude, Chris.  Thanks for the quick response.  This is exactly what I was looking to do!  
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

758 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

20 Experts available now in Live!

Get 1:1 Help Now