+ Reply to Thread
Results 1 to 7 of 7

Compare column names and copy data from several sheets only where column exists else blank

Hybrid View

henste1967 Compare column names and copy... 01-08-2014, 11:20 AM
alansidman Re: Compare column names and... 01-08-2014, 11:29 AM
henste1967 Re: Compare column names and... 01-08-2014, 11:33 AM
henste1967 Re: Compare column names and... 01-08-2014, 11:46 AM
henste1967 Re: Compare column names and... 01-08-2014, 01:52 PM
alansidman Re: Compare column names and... 01-08-2014, 12:56 PM
alansidman Re: Compare column names and... 01-08-2014, 01:56 PM
  1. #1
    Registered User
    Join Date
    08-16-2013
    Location
    rugby
    MS-Off Ver
    Excel 2003
    Posts
    4

    Compare column names and copy data from several sheets only where column exists else blank

    Hello absoute beginner at VBA!

    I have a spreadsheet where I would like to copy data from tabs ATPMD, STPPMD, and SBMD to tab Core, I would like the code to copy in row data where the column name matches on the above tabs.

    Thank you so much
    Attached Files Attached Files

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    25,002

    Re: Compare column names and copy data from several sheets only where column exists else b

    I'm not understanding the requirements. Could you give some examples of which data should be copied and the logic. I don't see any columns with the same names as the sheet names for the three sheets. Please clarify.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    08-16-2013
    Location
    rugby
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Compare column names and copy data from several sheets only where column exists else b

    Thanks for responding Alan

    Basically where say Material exists in STPPMD I would like to copy row data as below
    113358
    113447
    602186
    1002672
    1004127
    To be copied under material in core, and also where material decription is found, it's data to be copied and so on.

    Many thanks

  4. #4
    Registered User
    Join Date
    08-16-2013
    Location
    rugby
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Compare column names and copy data from several sheets only where column exists else b

    Hi Alan,

    I hope my explanation helped!

    Steve

  5. #5
    Registered User
    Join Date
    08-16-2013
    Location
    rugby
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Compare column names and copy data from several sheets only where column exists else b

    Alan

    Firstly thank you so much for your time, I ran the code, it ran and left me on tab SBMD but no data was in Core, maybe I am doing something wrong, I received a message saying it was complete but no data.

    Thanks again.

    Steve

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    25,002

    Re: Compare column names and copy data from several sheets only where column exists else b

    Steve:

    Give this a try:

    Sub Core()
        Dim w1 As Worksheet
        Dim w2 As Worksheet
        Dim w3 As Worksheet
        Dim w4 As Worksheet
        Set w1 = Sheets("STPPMD")
        Set w2 = Sheets("ATPMD")
        Set w3 = Sheets("SBMD")
        Set w4 = Sheets("Core")
        Dim lr1 As Long
        lr1 = w1.Range("B" & Rows.Count).End(xlUp).Row
        Dim lr2 As Long
        lr2 = w2.Range("B" & Rows.Count).End(xlUp).Row
        Dim lr3 As Long
        lr3 = w3.Range("B" & Rows.Count).End(xlUp).Row
        Dim lr4 As Long
        Dim i As Long
        Application.ScreenUpdating = False
        
        w1.Activate
        For i = 2 To lr1
        w1.Activate
        lr4 = w4.Range("C" & Rows.Count).End(xlUp).Row
        If Range("B" & i) <> "" Then
        Range("B" & i & ":C" & i).Copy
        w4.Activate
        w4.Range("C" & lr4 + 1).Select
        Selection.PasteSpecial Paste:=xlPasteValues
        End If
        Next i
        Application.CutCopyMode = False
        
        w2.Activate
        For i = 2 To lr2
        w2.Activate
        lr4 = w4.Range("C" & Rows.Count).End(xlUp).Row
        If Range("F" & i) <> "" Then
        Range("F" & i & ":G" & i).Copy
        w4.Activate
        w4.Range("C" & lr4 + 1).Select
        Selection.PasteSpecial Paste:=xlPasteValues
        End If
        Next i
        Application.CutCopyMode = False
        
        w3.Activate
        For i = 2 To lr3
        w3.Activate
        lr4 = w4.Range("C" & Rows.Count).End(xlUp).Row
        If Range("E" & i) <> "" Then
        Range("E" & i & ":F" & i).Copy
        w4.Activate
        w4.Range("C" & lr4 + 1).Select
        Selection.PasteSpecial Paste:=xlPasteValues
        End If
        Next i
        Application.CutCopyMode = False
        
        Application.ScreenUpdating = True
        MsgBox ("Completed")
        
    End Sub

  7. #7
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2509 Win 11
    Posts
    25,002

    Re: Compare column names and copy data from several sheets only where column exists else b

    Attached is my copy of your workbook with my code. It worked for me. Scroll down the Core page. I wonder if you have some data in that page that makes the last row be something other than row 2.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copying row of data to new sheets if specific value exists in column
    By raz333 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-26-2013, 03:53 PM
  2. [SOLVED] Compare 2 pair of columns then copy value of third column over different sheets
    By a3des in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-13-2012, 09:11 AM
  3. if partial data exists in one colum, copy to another column
    By Gunther in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-16-2011, 09:23 AM
  4. Compare column B - If exists then update date, else copy to end
    By jimbob121 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-06-2011, 02:21 PM
  5. VBA - Compare one column in two sheets & copy unmatched to one sheet
    By TreasureCat in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-19-2007, 11:18 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1