Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel Macro Question - Autofill formula

Posted on 2010-09-17
3
Medium Priority
?
634 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

972 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