Loop to clear, copy, paste

kubabocz

New Member
Joined
Oct 28, 2015
Messages
34
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 ?
 

Some videos you may like

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
19,993
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
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 !
 

kubabocz

New Member
Joined
Oct 28, 2015
Messages
34
Not possible as the number of rows in sheet 2 will change every time and it needs to go through each one of them
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
19,993
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
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
 

kubabocz

New Member
Joined
Oct 28, 2015
Messages
34

ADVERTISEMENT

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.
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
19,993
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
A loop isnt necessary unless there is rquest for one...assignment maybe??
 

kubabocz

New Member
Joined
Oct 28, 2015
Messages
34

ADVERTISEMENT

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
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
19,993
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
No it cant...but you didnt say anythig about that at the beginning of the thread
 

kubabocz

New Member
Joined
Oct 28, 2015
Messages
34
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 ?
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
19,993
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,126,888
Messages
5,621,430
Members
415,839
Latest member
Pollydooner

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
Top