Solved

Copy Formula to last row

Posted on 2014-02-13
9
330 Views
Last Modified: 2014-02-14
I continually have problems with copying a formula down to last empty row.  This is what I have but it does not copy down. I would appreciates someone helping me.  Would look at C7 and count used rows.


 Sheets("Block").Select
   
   Range("K7").FormulaR1C1 = "= ""#RET"""
    Range("K7").Select
    Selection.Copy
    Range("K7").Select
    Selection.End(xlDown).Select
0
Comment
Question by:leezac
  • 4
  • 3
  • 2
9 Comments
 
LVL 33

Expert Comment

by:Norie
Comment Utility
If you want to copy the formula down from row 7 in column K down based on the populated rows in column C try this.
With Sheets("Block")
    .Range("K7", Range("C" & Rows.Count).End(xlUp).Offset(, 8)).FormulaR1C1 = "= ""#RET"""
End With

Open in new window

0
 
LVL 12

Accepted Solution

by:
Harry Lee earned 500 total points
Comment Utility
Well, I have no idea what does imnorie's code do.

but

This would work.

Sub pasteformula()
Dim RwCnt As Long
RwCnt = Cells(Rows.Count, 3).End(xlUp).Row
If RwCnt < 7 Then
    MsgBox ("No Data Found!")
    Exit Sub
End If
With Sheets("Block")
    .Range("K7:K" & RwCnt).FormulaR1C1 = "= ""#RET"""
End With
End Sub

Open in new window

0
 
LVL 33

Expert Comment

by:Norie
Comment Utility
Harry

Do you know what the code I posted will do?
0
 
LVL 12

Expert Comment

by:Harry Lee
Comment Utility
imnorie,

Looks like pretty much doing the same thing.

The way you do it is instead of using 1 part range like A1:A5, you are using 2 parts range Range(Cell1, cell2).
While Cell1 = K7, and Cell2 is C& row count, but offset to column K.
So basically the range is K7 to K whatever row count of column c.
Well, and the rest, the formula part is simple, isn't it?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 33

Expert Comment

by:Norie
Comment Utility
Harry

They are similar but the code I posted uses the sheet 'Block' to find the last row of data in column C.

Your code will use the active sheet for the last row.

If the sheet 'Block' is active that's fine but if it isn't there could be some unexpected results.
0
 
LVL 12

Expert Comment

by:Harry Lee
Comment Utility
You can change the macro this way.

Sub pasteformula()
Dim RwCnt As Long, WS As WorkSheet
Set WS = Sheets("Block")
RwCnt = WS.Cells(Rows.Count, 3).End(xlUp).Row
If RwCnt < 7 Then
    MsgBox ("No Data Found!")
    Exit Sub
End If
    WS.Range("K7:K" & RwCnt).FormulaR1C1 = "= ""#RET"""
End With
End Sub
0
 

Author Closing Comment

by:leezac
Comment Utility
The both where similar but Harry's worked best for what I was trying to do.
0
 

Author Comment

by:leezac
Comment Utility
OK - thanks
0
 
LVL 33

Expert Comment

by:Norie
Comment Utility
leezac

How did the code I posted not work?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

772 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

11 Experts available now in Live!

Get 1:1 Help Now