Solved

Copy Formula to last row

Posted on 2014-02-13
9
334 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
ID: 39857756
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
ID: 39857819
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
ID: 39857957
Harry

Do you know what the code I posted will do?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 12

Expert Comment

by:Harry Lee
ID: 39857991
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
 
LVL 33

Expert Comment

by:Norie
ID: 39859149
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
ID: 39859602
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
ID: 39859606
The both where similar but Harry's worked best for what I was trying to do.
0
 

Author Comment

by:leezac
ID: 39859744
OK - thanks
0
 
LVL 33

Expert Comment

by:Norie
ID: 39859918
leezac

How did the code I posted not work?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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