0

My excel file, has multiple tabs. One of the Sheets is "Employee Access Arranged" and another is "IERWAA Lookup Arranged". Both these sheets have the same data ( Columns A to J ) but having different sources they are pulling from ( which is why they are in two separate sheets). Headers are in Row 1 and number of rows are not fixed.

I have a third tab "Master" in which I want the data from both these tabs to show up appended to each other. Row 1 will have headers, which are the same for all 3 sheets.

I wrote a Macro but it is just copying the header instead of the data.

Dim Sht As Worksheet
For Each Sht In ActiveWorkbook.Worksheets
    If Sht.Name = "Employee Access Arranged" Or Sht.Name = "IERWAA Lookup
    Arranged" Then
    Sht.Select

    LastRow = Range("A6553").End(xlUp).Row
    Range("A2", Cells(LastRow, "J")).Copy
    Sheets("Master").Select
    Range("A6553").End(xlUp).Offset(1, 0).Select
    ActiveSheet.Paste

Else
End If
Next Sh

Could someone please assist as to how I can make it work to copy the data from both the sheets instead of the header. Also, since the number of rows changes, i want it to be dynamic to pick up the last filled row automatically.

Please Note : This is not a duplicate of another question. I do not want just matching values from both sheets, but all the values from both sheets. Furthermore, the problem is unique as the last column "J" might not have values in most cases, but the whole column would still need to be copied

Thank you in advance.

7
  • 1
    I can't reproduce - is the column that you're getting your LastRow value from empty?
    – dwirony
    Commented Jun 6, 2018 at 18:38
  • Ahem, do you have more than 6553 rows of data?
    – SJR
    Commented Jun 6, 2018 at 18:46
  • No I do not have more than 6553 rows of data. But it would take longer to process because of such a large number of rows instead of the actual number, no? dwirony-yes the last column can be empty in some cases as it is more of a comments column. Commented Jun 6, 2018 at 18:49
  • 1
    But it would take longer to process because of such a large number of rows instead of the actual number, no? - no. Try "A" & Rows.Count instead. All you're doing is getting the Range object for one cell, and then invoking .End(xlUp) on it. The number makes no difference, other than whether or LastRow is correct or not given any number of rows. Commented Jun 6, 2018 at 18:53
  • 1
    Don't do sht.Select. Instead of using implicit ActiveSheet references (e.g. Range, Cells), qualify them with that sht variable and do sht.Range, sht.Cells. And instead of Range(...).Select followed by ActiveSheet.Paste, do sht.Range(...).Paste. Avoid Select and Activate, and qualify all worksheet member calls with an explicit Worksheet object. Commented Jun 6, 2018 at 18:57

1 Answer 1

0

dirty runthrough off the top of my head:

dim i as long, lrs as long, lrd as long
for i = 1 to sheets.count
    if not sheets.name="Master" then 
        with sheets(i)
            lrs = .cells(.rows.count,1).end(xlup).row
            .range(.cells(2,"A"),.cells(lrs,"J")).copy
        end with
        with sheets("Master")
            lrd = .cells(.rows.count,1).end(xlup).row
            .cells(lrd+1,1).paste
        end with
    end if
next i

Edit1:

Cleaned up the code to work for the two specified sheets:

Dim i As Worksheet, arr As Variant, lrs As Long, lrd As Long
arr = Array("Employee Access Arranged", "IERWAA Lookup Arranged")
For Each i In Sheets(arr)
    With i
        lrs = .Cells(.Rows.Count, 1).End(xlUp).Row
        .Range(.Cells(2, "A"), .Cells(lrs, "J")).Copy
    End With
    With Sheets("Master")
        lrd = .Cells(.Rows.Count, 1).End(xlUp).Row
        .Range(.Cells(lrd + 1, "A"), .Cells(lrd + 1 + lrs, "J")).PasteSpecial xlValues
    End With
Next i
5
  • You meant With worksheets(i), right? (Not Activesheet) Commented Jun 6, 2018 at 21:24
  • Thank you for your input Cyril. The excel file has multiple other tabs, which are irrelevant to the purpose for which this Macro is being used. If I used your code, it would be adding all the data from the other tabs as well. Commented Jun 7, 2018 at 2:14
  • @chrisneilsen good catch. i was focused on showing the 2x last row declarations and missed that. Will edit.
    – Cyril
    Commented Jun 7, 2018 at 12:48
  • Hey! Works like a charm. I was trying to write an array but was defining the range incorrectly. You did this perfectly! I have voted for your solution, however, it does not show since I do not have enough reputation points. Thank you everyone! PS. I do not know how to get the negative reputation for my question to go away. Commented Jun 7, 2018 at 13:16
  • @SalikGilani You should be able to hit the check-mark below the voting to accept the answer, even if you can't give it a vote, regardless of reputation as the owner of the question. Most folks downvote when there isn't sufficient info in the post originally, and if they dont' come back later to review (or dont' leave a comment so they can be notified of an update), it usually stays. Yes, -1 feels bad, but it's not something to dwell on. Glad that worked for you!
    – Cyril
    Commented Jun 7, 2018 at 14:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.