Loop for multiple lines of copying and pasting

Phippsy

New Member
Joined
Jan 27, 2017
Messages
7
Hi all,

Looking for some help on this. My colleague and I have developed a pricing tool that we use for costing component parts. Often these component parts are very similar in terms of manufacture with the only difference being that the size of the component alters. We've set the spreadsheet up so that all we need to do is enter the line number and this alters the size of the component automatically and gives a price, which we then copy back to the corresponding line in our data schedule. The only problem with this is that often we're doing this for 100's of lines which can become quite monotonous.

This code for one line is basically what we'd like the routine to do for all lines:

Code:
Sub AUTOCOPYPASTE()'
' AUTOCOPYPASTE Macro
'


'
    Sheets("Imported Lines").Select
    Range("A2").Select
    Selection.Copy
    Sheets("Routes").Select
    Range("B8").Select
    ActiveSheet.Paste
    Sheets("BoM Report").Select
    Range("AA80").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Imported Lines").Select
    Range("G2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

End Sub


What we'd like to be able to do if possible, is to run a loop that would perform the above for every line until it finds a blank cell in column A in "imported lines". So A2 becomes A3 and the number in this cell gets copied to "routes" B8. Then the result in "BoM Report" AA80 is copied and pasted to G3 in "imported lines" and so forth.

Apologies if this is unclear, I've written it a few times to try to make it as concise as possible without making it nonsensical! let me know if you need any more info, happy to supply as much info as is needed

Thank you very much in advance

Tony
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You can simplify that code and add a loop like this:

Code:
Sub AUTOCOPYPASTE()
   Dim lRow as long
    With Sheets("Imported Lines")
       for lrow = 2 to .cells(.rows.count, "A").End(xlUp).row
           .Cells(lRow, "A").Copy Sheets("Routes").Range("B8")
           .Cells(lRow, "G").Value2 = Sheets("BoM Report").Range("AA80").Value2
       next lrow
    End With

End Sub
 
Upvote 0
Hi Rory,

I was wondering if you might be able to assist us one step further with this. Is there a straightforward way, as part of the loop, that the spreadsheet could automatically create a pdf of the "bom report" sheet in the same folder as the spreadsheet for each of the lines entered? This would enable us have snapshot price breakdowns of each line. i.e Line 1 - copy price, paste to corresponding column, then create a pdf before moving on to line 2? It could just be saved as "line 1" and so forth......

Thanks again in advance

Cheers

Tony
 
Upvote 0
Yes, just add:

Code:
Sheets("BoM Report").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        ThisWorkbook.Path & "\Line" & lRow - 1 & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:= _
        False, IgnorePrintAreas:=False, OpenAfterPublish:=False

inside the For ... Next loop.
 
Upvote 0
Rory,

Thank you again, you have no idea how much that has helped us. If you're ever down in south wales, I'll have to buy you a few beers!

Cheers

Tony
 
Upvote 0
I'll stop off next time I'm on my way down to Cornwall!! :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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