Report

Macro to copy/paste down X times (where X is the number of rows) [Solved]

Ask a question JW32 8Posts Friday January 4, 2013Registration date February 21, 2013 Last seen - Latest answer on Sep 22, 2016 07:45AM
Hello,
I could sure use some help on a Macro.
I need to copy the value in E1 and paste down to all cells in column E (E2 to E??)
The issue is - the number of rows will change.
One time it might need to copy down 5 times. Another it might need to copy down 40 times.
I would also need it to do nothing if there is only 1 row (Nothing to copy if the source is the only row!!)
I case it matter you - I would repeat the exact process for Column F. Copy F1 and paste down to all cells in column F (F2 to F??).
We can NOT copy the row as the information in A to D must not be manipulated.
Thank you very much for your time!
Jeanine
See more 
Helpful
+5
moins plus
Hi JW32,

OK so you want to copy down the values in column E and F down a variable number of times.

How will I know what the variable number is?

Or are columns A to D completely filled till say row 100 and columns E and F are only filled in row 2, 10, 55. And now you wish to copy E2:F2 and paste it to E3:F9 etc.?

Please shine some light on the confusion.

Best regards,
Trowa
Josh- Mar 20, 2015 08:37PM
Hi TrowaD,

Could you help me with a similar question...The difference for me is that I want to paste a specific amount of times (100).

So as of now I have,

If Application.WorksheetFunction.CountA("B:B") = 0 Then
[B1].Select
Else
On Error Resume Next
Columns(1).SpecialCells(xlCellTypeBlanks)(1, 1).Select
If Err <> 0 Then
On Error GoTo 0
[B65536].End(xlUp)(2, 1).Select
End If
On Error GoTo 0
End If

Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=NOW()"

And I would like the formula to be pasted the same way overt the next 99 cells so I have 100 cells all in a row with the date and time.
Reply
TrowaD 1988Posts Sunday September 12, 2010Registration date ModeratorStatus September 19, 2016 Last seen - Mar 23, 2015 12:41PM
Hi Josh,

To repeat the code you have until you reach row 100, you can add "Do" at the start and "Loop Until ActiveCell.Row = 100" at the end.

If this doesn't suffice then please explain your query in full detail.

Best regards,
Trowa
Reply
Add comment
Helpful
+0
moins plus
Hi!

It is always so hard to describe things. What was clear in my mind - is obviously not clear to another!

If I can get this to work - this macro will actually be in the middle of a much larger macro that actually works (except this one important step)!

Sheet 1 has data. What shows up on Sheet 1 is a result of some look ups. Not part of a Macro at all. It is dog information. Name and Owner (and a few other bits). So the user keys in a number and up pops all the information from the database about that dog. How many lines pop up could be 1 or any number up to about 250.

The Macro I have copies these lines to another sheet. Sheet 2. Sheet 2 will have columns A to G. And as I said - an undetermined number of lines. As many lines as there are records for the dog in the database.

Sheet 2 only has the lines copied from Sheet 1. Meaning there are no lines with spaces or blanks. Only the results from page 1 are on page 2. They get copied in by my Macro.

So now we are on sheet 2. Lets for my example say we copied in 11 rows.

I need the value in E1 and F1 to be copied down 10 times (11-1). So that all 11 rows have the same value in Column E and F.

The values in the other columns can not be change.

How many times do we have to copy? The same number of times as there are rows in sheet 2.

To illustrate. Below are the values in E and F only (example 4 rows)

Suzy John
Harry Max
Shevy Twinkle
Max Rover

The above is what came to sheet 2 via a copy.

I want the result to be:

Suzy John
Suzy John
Suzy John
Suzy John

Each time the Macro runs will have a different number of rows to copy down. And of course if there is only one row (which is possible!!) there is not need to copy anything as the correct information is in that row.

I hope this is a better explanation.

Thank you for your time!!
Jeanine
Add comment
Helpful
+0
moins plus
Hi Jeanine,

Things got a lot clearer!

Good to tell this is part of a code.
I don't know on which sheet you are when this part of the code is activated, so I started out with referring to the correct sheet.

Second, when only 1 row exists nothing should be done. Normally I would exit sub, but now the code will skip the autofill part and continue with the code.

So just make sure the variable lRow isn't used before and/or is ok to be changed and this code is safe to implement as part of your code (of course without the Sub and End Sub parts).

Sub CopyValueDown()
Dim lRow As Integer

Sheets("Sheet 2").Select
lRow = Range("A" & Rows.Count).End(xlUp).Row
If lRow = 1 Then GoTo NextPartOfCode
Range("E1:F1").AutoFill Destination:=Range("E1:F" & lRow)
NextPartOfCode:
End Sub


Good luck with your code and let me know if more assistance is desired.

Best regards,
Trowa
Add comment
Helpful
+0
moins plus
Trowa!!

This is great! It is almost perfect. Actually it was perfect until I just ran for the 14th time.

As it turns out there are some records where the last character in E1 has a #.

Such as:

Suzy3 John

When I use your code - if it says only Suzy John it works perfectly.

If it Says Suzy3 John it increments the last digit.

So I ended up with:

Suzy3 John
Suzy4 John
Suzy5 John
Suzy6 John

When I needed:

Suzy3 John
Suzy3 John
Suzy3 John
Suzy3 John

I am so sorry I didn't realize this might happen. The data has about 12K lines so I didn't notice that some records end with a number.

Is there a way to adjust for this oddity?

Thank you!
Jeanine
rizvisa1 4482Posts Thursday January 28, 2010Registration date ContributorStatus February 12, 2016 Last seen - Feb 23, 2013 07:50AM
see if changing this line
Range("E1:F1").AutoFill Destination:=Range("E1:F" & lRow)

to
Range("E1:F1").AutoFill Destination:=Range("E1:F" & lRow), Type:=xlFillSeries
helps
Reply
TrowaD 1988Posts Sunday September 12, 2010Registration date ContributorStatus September 19, 2016 Last seen - Feb 25, 2013 10:32AM
Hi Jeanine,

Change the line:
Range("E1:F1").AutoFill Destination:=Range("E1:F" & lRow)
into:
Range("E1:F1").AutoFill Destination:=Range("E1:F" & lRow), Type:=xlLinearTrend

This should prevent the numbers from going up.

Oddity solved, right?

Best regards,
Trowa
Reply
Diddy- Sep 22, 2016 07:45AM
I have a very similar question. I am trying to do a sort of a burn. I need the cell value to be copied linearly across an x amount of rows, and the number or rows that it needs to be copied out lives in another table. How can this be done?
Reply
Add comment

Members get more answers than anonymous users.

Being a member gives you detailed monitoring of your requests.

Being a member gives you additional options.

Not a member yet?

sign-up, it takes less than a minute and it's free!