Copy Paste Data from one Excel Worksheet to Another Using VBA

How to copy and paste data from one Excel Worksheet to Another Using VBA

I want to search the whole spreadsheet for the product ‘Car’ and paste the whole row (not just the cell). So in this instance it would be row a2, a5, a8 and a10 from sheet 1 to sheet2.
If I have to use macros to automate the whole process what would be the vba code?

Sub mycar()
‘Let’s start at row 2. Row 1 has headers
x = 2
‘Start the loop
Do While Cells(x, 1) <> “”
‘Look for data with ‘Car’
If Cells(x, 1) = “Car” Then
‘copy the row if it contains ‘Car’
Worksheets(“Sheet1”).Rows(x).Copy
‘Go to sheet2. Activate it. We want the data here
Worksheets(“Sheet2”).Activate
‘Find the first empty row in sheet2
erow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
‘Paste the data here
ActiveSheet.Paste Destination:=Worksheets(“Sheet2”).Rows(erow)
End If
‘go to sheet1 again and actvate it
Worksheets(“Sheet1”).Activate
‘Loop through the other rows with data
x = x + 1
Loop
End Sub

Watch the Microsoft Excel training video below to see how you can use the macro (vba code) to copy and paste specific data from Sheet 1 to Sheet 2:


17 thoughts on “Copy Paste Data from one Excel Worksheet to Another Using VBA”

  1. I want to copy a file from a page,in a page from another file
    ex, file A sheet1 in file B sheet1
    If you can help
    thanks

    1. You’re absolutely right. It is a waste of your time and energy to hunt up after someone when thr;1#82&7yee not even polite to you. It’s amazing how some people will be in a serious relationship with someone as discourteous and rude like that. But I sometimes give the impression that I am snobby. I’m shy and usually can’t think of much to say in the first couple of encounters. Takes me a while to warm up. I wonder if that’s why I have so many extroverted friends …

  2. hi, i’m loving your page.
    in addition to this, same case but instead i have 2 or more worksheets and i need to copy all rows containing “cars” (following this example) from these sheets to one master file and how can i run this via hit of a button and will refresh data every hit. thanks a lot

  3. Dear Sir,
    I followed the above mentioned code to copy certain number of row which do I want to paste another work sheet. I have written the following code but it is not working. Can you please guide me where is wrong coding.

    Sub Car()
    Dim X
    X = 2
    Do While Cells(X, 2) “”
    If Cells(X, 2) = “Hero” Then
    Worksheets(“Sheet2”).Rows(X).Copy
    Worksheets(“Sheet3”).Activate
    erow = Sheet3.Cells(Row.Count, 1).End(xlUp).Offset(1, 0).Row
    ActiveSheet.Paste Destination:=Worksheets(“Sheet3”).Row(erow)
    End If
    Worksheets(“Sheet2”).Activate
    X = X + 1
    Loop
    End Sub

    1. Just replace 6th last statement and write

      Activesheet.Past Destination:=Worksheet(“Sheet3”).Row(erow,1)

      Now it will work

      Thanks Dinesh Sir for giving us an opportunity to learn VBA .

        1. Hi, Can u sent the coding so that i can help you out, but in my office excel version 2013 it’s working. can u share the codding then only I can suggest you, where it is wrong.

          Thanks

  4. Hi Dinesh

    I am using the code below to copy and paste data from multiple worksheets into a master file
    It works just fine except it only copies over the values of the cells rather than all the formats i.e. cell pattern/colour, can you help?

    Sub OpenFiles()
    Dim MyFolder As String
    Dim MyFile As String
    Dim erow

    MyFolder = “C:\Users\Zoe\Desktop\ZAFiles2 – Working Copies1 – VJC”
    MyFile = Dir(MyFolder & “\*.xlsx”)

    Do While MyFile “”

    Workbooks.Open Filename:=MyFolder & “\” & MyFile

    Sheets(“Posts”).Select
    Range(“A2:AB28”).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Application.DisplayAlerts = False
    ActiveWorkbook.Close

    erow = Range(“A” & Rows.Count).End(xlUp).Offset(1).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    MyFile = Dir
    Loop
    End Sub

  5. Hi I want to copy the entire row based on criteria , cell value start “C” or “E” and from letf and secound criteria in Another cell value is “C” or “D” then copy the entire row on paste on next visible row
    and applyed this code but is not working plz help

  6. Hi. I want to do a search of multiple values and paste it on the new worksheet.

    Example:

    Instead of just Car, I want to search for Ship, Boat, or even more than 1 value (Ship, Boat or Ship, Car or Car, Boat ) with a comma (,) in between.

    How do search for part of the word and not the exact word?

  7. I have a workbook with information tracking profit loss. The worksheet has 15 columns and the data starts in A7. There are column headers. I need to copy and paste all of the information to a new spreadsheet for reporting. I’m trying to write a macro so the report will run and take all of the information listed and put into a new workbook.

    The issue that I may run into is the entries are tracked by booking numbers. The numbers can be listed numberous time if there are multiple errors in a booking so there will be duplicates. But they run these reports weekly and I don’t want the data to be exported multiple times.

    There are separate tabs for each month. I would like it to stay that way.

  8. hi sir
    This code (in video link) is not working – it is showing a ‘Run time error’ and saying “Object required”
    can you please kindly help & suggest. thanks

  9. Thanks for this, it was very useful.

    Can i ask how you would include paste special rather than just paste. Specifically im looking for values and number formats. I have tried to amend it but doesn’t seem to like anyway i try.

    Thanks

  10. Thank you for some very interesting and informative videos.

    Unfortunately, when I cut and paste the above code into an Excel 2016 Module in an effort to duplicate your video training, I get an “out of range” error at this line:

    Worksheets(“Sheet1”).Activate

    I have not been able to figure out a solution. Can you help with this.

  11. Hello,
    Your videos are very helpful! thanks for taking the time.
    I am trying to write a similar code, however I’d like the “search criteria” to be a variable in sheet2.

    using your example above, I will push the headers to row2, and will have an empty cell A1 in sheet2 and a button with command. when inputting the word “car” into A1 in sheet2, it will go to Sheet1, search for entries in column B in Sheet1, copy the whole row, and paste it to the next available row in Sheet2.
    Then, if I go back to Sheet2, and type in the word “Seats” in cell A1, it will replace the rows with the new search results, meaning, the full rows from Sheet1.
    most importantly, I just want to use this as a “filter” so I do not want the information in Sheet1 to be deleted at any point.
    is that possible?
    would you please assist?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.