Loop to clear, copy, paste

kubabocz

New Member
Joined
Oct 28, 2015
Messages
36
Hi all,

I have a question regarding VBA I need to use.

Can you please help.

The macro needs to do the follwing:
1. Go to sheet 1
2. Clear cells A2 to Z2
3. Go sheet 2
4. Copy cells A2 to Z2
5. Go to sheet 1
6. Paste the cells to the range A2 to Z2
7. Loop throguh points 1 to 6 to the last row in the sheet 2

Of course it will have some more things to do after point 6 but I can manage it myself, anybodey can help ?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Have you had a try at this using the macro recorder ??
AND
Why do you need to loop through the rows....simply copy / paste as a block !
 
Upvote 0
Not possible as the number of rows in sheet 2 will change every time and it needs to go through each one of them
 
Upvote 0
VBA Code:
Sub MM1()
Dim lr1 As Long, lr2 As Long, ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
lr1 = ws1.Cells(Rows.Count, "A").End(xlUp).Row
lr2 = ws2.Cells(Rows.Count, "A").End(xlUp).Row
ws1.Range("A2:Z" & lr1).Clear
ws2.Range("A2:Z" & lr2).Copy ws1.Range("A2")
End Sub
 
Upvote 0
Or maybe I just don't know how to do it (the loop part)
VBA Code:
Sub MM1()
Dim lr1 As Long, lr2 As Long, ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
lr1 = ws1.Cells(Rows.Count, "A").End(xlUp).Row
lr2 = ws2.Cells(Rows.Count, "A").End(xlUp).Row
ws1.Range("A2:Z" & lr1).Clear
ws2.Range("A2:Z" & lr2).Copy ws1.Range("A2")
End Sub
This doesn't work.

It copies all the lines in Sheet 2 and paste in sheet 1.

What I wanted to do is to go line by line and copy and paste one after another. This does not loop through lines.
 
Upvote 0
A loop isnt necessary unless there is rquest for one...assignment maybe??
 
Upvote 0
Ok, I will explain further as it may not be explained right.

So the loop should go through each line from sheet2, copy it to sheet1, save the file with the name of the file taken from cell A2 from sheet1, go back to the previous file, go through next line in sheet2, replace the previously inserted line in sheet1, save again with the new value from cell A2, and so on as long as it hits the last row in sheet2.

I don't think it can be done without loop.

Thank you
 
Upvote 0
No it cant...but you didnt say anythig about that at the beginning of the thread
 
Upvote 0
That part I can do myself, that's why I dind write but you are right, it was probably quite important here. Are you able to help ?
 
Upvote 0
Not at the moment. ..i dont have a computer. ...someone else may help....but you will need to provide more information on save type and location
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,557
Latest member
richa mishra

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