ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   xlDown going further than data (https://www.excelbanter.com/excel-programming/377421-xldown-going-further-than-data.html)

Astello[_2_]

xlDown going further than data
 
I have a macro in excel that fills a formula in the first cell in a
column, then copies that formula and fills it into the rest of the
column. There is placeholder data in this column, so that the formula
will only be copied to a certain amount of cells. My code looks like
this:

Range("P2").Select
ActiveCell.FormulaR1C1 = "=Miles(RC[-10],RC[-5])"
Range("P2").Select
Selection.Copy
Range("P2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Range("P2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False

Now the code works fine, but for some reason it pastes the formula well
past the data that is in the column (ex. if data is in rows 1-42, the
formula gets copied to rows 1-112) which gives me a bunch of "#N/A"s,
which causes problems when I try to sum up these values. Any idea why
this is happening? I thought putting in "xlDown" was supposed to
prevent this problem...


Don Guillett

xlDown going further than data
 
try

Sub copyfomrula()
lr = Cells(Rows.Count, "P").End(xlUp).Row
Range("P2").FormulaR1C1 = "=Miles(RC[-10],RC[-5])"
Range("p2:p" & lr).FillDown
End Sub

to convert to values
Sub copyfomrula()
lr = Cells(Rows.Count, "P").End(xlUp).Row
Range("P2").FormulaR1C1 = "=Miles(RC[-10],RC[-5])"
With Range("p2:p" & lr)
..FillDown
..Value = .Value
End With
End Sub
--
Don Guillett
SalesAid Software

"Astello" wrote in message
oups.com...
I have a macro in excel that fills a formula in the first cell in a
column, then copies that formula and fills it into the rest of the
column. There is placeholder data in this column, so that the formula
will only be copied to a certain amount of cells. My code looks like
this:

Range("P2").Select
ActiveCell.FormulaR1C1 = "=Miles(RC[-10],RC[-5])"
Range("P2").Select
Selection.Copy
Range("P2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Range("P2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False

Now the code works fine, but for some reason it pastes the formula well
past the data that is in the column (ex. if data is in rows 1-42, the
formula gets copied to rows 1-112) which gives me a bunch of "#N/A"s,
which causes problems when I try to sum up these values. Any idea why
this is happening? I thought putting in "xlDown" was supposed to
prevent this problem...




Jon Peltier

xlDown going further than data
 
If you have a formula that returns "", that is not a blank cell, it contains
a formula. End(xlDirection) goes until it hits a blank, or starting from a
blank, it goes until it hits a non-blank.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"Astello" wrote in message
oups.com...
I have a macro in excel that fills a formula in the first cell in a
column, then copies that formula and fills it into the rest of the
column. There is placeholder data in this column, so that the formula
will only be copied to a certain amount of cells. My code looks like
this:

Range("P2").Select
ActiveCell.FormulaR1C1 = "=Miles(RC[-10],RC[-5])"
Range("P2").Select
Selection.Copy
Range("P2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Paste
Range("P2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False

Now the code works fine, but for some reason it pastes the formula well
past the data that is in the column (ex. if data is in rows 1-42, the
formula gets copied to rows 1-112) which gives me a bunch of "#N/A"s,
which causes problems when I try to sum up these values. Any idea why
this is happening? I thought putting in "xlDown" was supposed to
prevent this problem...





All times are GMT +1. The time now is 07:04 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com