Repeating my code until the last row of data

KNKN9

Board Regular
Joined
Mar 27, 2017
Messages
92
Hi ,

I have the data set

COL ACOL BCOL CCOL DCOL E
ROW1DATADATADATA
ROW2DATADATADATA
ROW3DATADATADATA
ROW4DATADATADATA
ROW5DATADATADATA
ROW6DATADATADATA
ROW7DATADATADATA

<colgroup><col width="64" span="6" style="width:48pt"> </colgroup><tbody>
</tbody>


At the moment my code copies the information from row 1. However, I need this to happen for the last row of a spreadsheet. As the last row differs from each spreadsheet.

Can someone help amend my code below so it is able to function for data until the last row ?



Code:
Sub data()


'copying projects
ActiveSheet.Range("A3:L3").Copy
Sheets("Sheet4").Range("N1").End(xlDown).Offset(1, -13).PasteSpecial Paste:=xlPasteValues


With Sheets("sheet1")
    Set rng = .Range(.Cells(1, 15), .Cells(1, .Cells(1, .Columns.Count).End(xlToLeft).Column))
    Set rng1 = .Range(.Cells(2, 15), .Cells(2, .Cells(2, .Columns.Count).End(xlToLeft).Column))
    Set rng_3 = .Range(.Cells(3, 13), .Cells(3, .Cells(3, .Columns.Count).End(xlToLeft).Column))
End With


'copying roles and planned hours


rng.Copy
Sheets("Sheet4").Range("N1").End(xlDown).Offset(0, -1).PasteSpecial xlValues, Transpose:=True
rng1.Copy
Sheets("Sheet4").Range("N1").End(xlDown).Offset(1, 0).PasteSpecial xlValues, Transpose:=True


'copy_actual hours
rng_3.Copy
Sheets("Sheet4").Range("N1").End(xlDown).Offset(-51, 1).PasteSpecial xlValues, Transpose:=True




End Sub

Thanks in advanced.




<colgroup><col><col><col><col><col><col><col span="2"><col><col><col><col><col><col><col></colgroup><tbody></tbody>
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Hi ,


At the moment my code copies the information from row 1. However, I need this to happen for the last row of a spreadsheet. As the last row differs from each spreadsheet.

Can someone help amend my code below so it is able to function for data until the last row ?


Are you asking to have this code run against the last row of your sheet or are you asking for this code to run on every line of your sheet, all the way down to the last row?
 
Last edited:
Upvote 0
Thank you for your response and i want the code to run on every line of my sheet, all the way down to the last row

Are you asking to have this code run against the last row of your sheet or are you asking for this code to run on every line of your sheet, all the way down to the last row?
 
Upvote 0
Which column can we look at that will always have data so that we can use that column to determine where the last row of data is?
Will column A always be populated, or will it be some other column?
 
Upvote 0
So then you can find the last row on a sheet like this:
Code:
Dim lr as Long
lr = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

So then if you want to copy columns A:L down to last row, you could make the following change to your code:
Code:
ActiveSheet.Range("A3:L" & lr).Copy
 
Upvote 0
Thanks...

However, I need A3 to change also ( so drop down each row).. What I am trying to do is change my table above from

COL ACOL BCOLCCOL DCOL E
ROW 1 Role 1Role 1Role 1Role 1Role 1
ROW 2String 1String 2String 3String 4String 5
ROW 3project 1valuesvaluesvaluesvaluesvalues
ROW 4project 2valuesvaluesvaluesvaluesvalues
ROW 5project 3valuesvaluesvaluesvaluesvalues
ROW 6project 4valuesvaluesvaluesvaluesvalues

<colgroup><col width="64" span="7" style="width:48pt"> </colgroup><tbody>
</tbody>


to

COL ACOL BCOLCCOL DCOL E
ROW 1 project 1Role 1String 1values
ROW 2Role 1String 2values
ROW 3Role 1String 3values
ROW 4Role 1String 4values
ROW 5Role 1String 5values
ROW 6project 2Role 1String 1values
Role 1String 2values
Role 1String 3values
Role 1String 4values
Role 1String 5values

<colgroup><col width="64" span="6" style="width:48pt"> </colgroup><tbody>
</tbody>



So then you can find the last row on a sheet like this:
Code:
Dim lr as Long
lr = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row

So then if you want to copy columns A:L down to last row, you could make the following change to your code:
Code:
ActiveSheet.Range("A3:L" & lr).Copy
 
Upvote 0
Your code would cause errors, even on the first record, unless there is already something on Sheet4 to start. Otherwise, this row would bomb out, as it goes to the bottom of the page, and then tries to move one row past that:
Code:
Sheets("Sheet4").Range("N1").End(xlDown).Offset(1, -13).PasteSpecial Paste:=xlPasteValues
So, what does Sheet4 look like to start, before this macro runs?
 
Last edited:
Upvote 0
Yes therebis data is sheet4
So
COL COL M COL N
Header 1 header 2 header 3 .....
Value value value
Blank blank value
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,385
Members
448,956
Latest member
JPav

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top