Forum Discussion

MegWH's avatar
MegWH
Copper Contributor
Nov 26, 2020

VBA Copy and Paste between workbooks

Hi,

 

I am trying to write a VBA to embed into a source workbook (this gets renamed for each job), which copies the same row each time (Row 5 on the Data Worksheet) and adds it into the next available row on the Machine Schedule workbook.

 

So far this code works the first time that you run it. But when you run it a second time, it re-writes row 5, and adds other information on the next available row down, pulled from different sheets on the source workbook. They're not at all mentioned in the code, so I can't understand why it would do this.

 

Any help would be greatly appreciated.

 

3 Replies

  • NikolinoDE's avatar
    NikolinoDE
    Platinum Contributor

    MegWH 

     

    Here something on the fast untested (but should work):

    Sub copy()

    With Sheets("1")

    .Range(.Cells(6, 2), .Cells(Rows.Count, 5)

    .End(xlUp)).copy End With Sheets("2")

    .Activate lastrow = Cells(Rows.Count, 2)

    .End(xlUp).Row Range("B" & lastrow + 1)

    .Select Selection.PasteSpecial xlPasteValues = xlPasteValues

    End Sub

     

    You can probably find what you're looking for here:

    https://www.rondebruin.nl/win/s9/win005.htm

     

    I would be happy to know if I could help.

     

    Nikolino

    I know I don't know anything (Socrates)

     

    * Kindly Mark and Vote any reply if it helps please, as it will be beneficial to more Community members reading here.

     

     

    • MegWH's avatar
      MegWH
      Copper Contributor

      NikolinoDE 

       

      Thank you so much for your response. I have just managed to get it working by specifying the PasteSpecial as PasteValues, as you have mentioned below. It turns out that it was quite an easy fix in the end! 

      Thanks again.

      • NikolinoDE's avatar
        NikolinoDE
        Platinum Contributor

        MegWH 

         

        You are welcome 🙂

         

        It has give me joy to be able to help you

         

        Wish you a nice day.

         

        Nikolino

        I know I don't know anything (Socrates)

Resources