Home » excel » How to create a macro to copy data from one excel worksheet to the next line in another worksheet

How to create a macro to copy data from one excel worksheet to the next line in another worksheet

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m a bit stumped with this and wondered if anyone could help please? I’ve created a macro in excel to copy some data from one worksheet to another. It works fine, but obviously each time I run the macro, it just completes the top line (as I pasted to whilst recording). I actually want it work so that every time I run the macro, it copies the data to a new line.

If it helps, I can paste a link to the document. I’m sure it’s just a case of pasting some VBA code into the macro. At the moment, the code looks like this:

Sub Macro1()
'
' Macro1 Macro
'

'
    Sheets("Quotation System").Select
    Range("K9").Select
    Selection.Copy
    Sheets("Confirmed Bookings").Select
    Range("A2").Select
    ActiveSheet.Paste
    Range("B2").Select
    Sheets("Quotation System").Select
    Range("K11").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Confirmed Bookings").Select
    ActiveSheet.Paste
    Sheets("Quotation System").Select
    Range("K13").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Confirmed Bookings").Select
    Range("C2").Select
    ActiveSheet.Paste
    Sheets("Quotation System").Select
    Range("K15").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Confirmed Bookings").Select
    Range("D2").Select
    ActiveSheet.Paste
    Columns("D:D").EntireColumn.AutoFit
    Columns("D:D").EntireColumn.AutoFit
    Columns("D:D").ColumnWidth = 10.86
    Sheets("Quotation System").Select
    Range("K17").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Confirmed Bookings").Select
    Range("E2").Select
    ActiveSheet.Paste
    Range("F2").Select
    Sheets("Quotation System").Select
    Range("K19").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Confirmed Bookings").Select
    ActiveSheet.Paste
    Columns("F:F").ColumnWidth = 8.57
    Sheets("Quotation System").Select
    Range("K21").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Confirmed Bookings").Select
    Range("G2").Select
    ActiveSheet.Paste
    Range("H2").Select
    Sheets("Quotation System").Select
    Range("K23").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Confirmed Bookings").Select
    ActiveSheet.Paste
    Range("I2").Select
    Sheets("Quotation System").Select
    Range("K25").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Confirmed Bookings").Select
    ActiveSheet.Paste
    Sheets("Quotation System").Select
    Range("K7").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Confirmed Bookings").Select
    Range("J2").Select
    ActiveSheet.Paste
    Range("K2").Select
    Sheets("Quotation System").Select
    Range("G29").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Confirmed Bookings").Select
    ActiveSheet.Paste
    Columns("K:K").EntireColumn.AutoFit
    Columns("K:K").ColumnWidth = 6
    Columns("K:K").ColumnWidth = 7
    Range("K2").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "='Quotation System'!R[27]C[-4]"
    Range("K3").Select
    Columns("J:J").EntireColumn.AutoFit
    Range("I19").Select
    Columns("A:A").ColumnWidth = 8.43
End Sub
Sub Booking_confimred()

End Sub

Many thanks!

How to&Answers:

Because you have a very situation-specific macro recorded, I would like to show you just a basic method to improve this macro and place those copies to a specific position:

You start with this

Public Sub Macro1()

  Application.CutCopyMode = False

then, this is what macro-recoder uses to copy:

Sheets("Quotation System").Select
Range("K9").Select
Selection.Copy

This is what you can use instead:

Sheets("Quotation System").Range("K9").Copy

This is what macro-recorder uses to paste:

Sheets("Confirmed Bookings").Select
Range("A2").Select
ActiveSheet.Paste

This is what you can use instead:

Sheets("Confirmed Bookings").Range("A2").Paste

And in order to paste it to a new line, it would be like

   With Sheets("Confirmed Bookings")
     .Cells(.UsedRange.Columns(1).Rows.Count + 1, 1).Paste
   End With

However, this is not the only way to solve this, and sureley not the best one, but it might help you at your level of understanding VBA.

If you want a neater solution, you might want to search around on SO, as there are many similar questions/problems and good solutions to them here.