+ Reply to Thread
Results 1 to 15 of 15

check and Copy columns from other workbook in a loop

  1. #1
    Registered User
    Join Date
    01-29-2013
    Location
    The Netherlands
    MS-Off Ver
    2016 x64
    Posts
    48

    check and Copy columns from other workbook in a loop

    Hi,
    I have a workbook into which I want to import data from a second workbook.
    I am able to open the second workbook. However I am stuck at the pointy where I copy data into the first (master) workbook.

    The challenge that I have is that in the master workbook I have a range of columns and that I want to check the existence of that same column in the import workbook. If that column exists, then I want to have that column copied to the master workbook. and so on...

    I am able to identify the row of the header in the import workbook, however, when I try to check the existence of headers, my code fails.
    I attached the master and the import workbooks as examples.

    Thanks, Daniel

    Excel 2013 64bits
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi ! A demonstration as a beginner starter …


    Paste this code to the Target worksheet module, update F variable for the import file full path :

    PHP Code: 
    Sub Demo1()
         
    Dim F$, R&, VC%, W
             F 
    ThisWorkbook.Path Application.PathSeparator "import .xlsx"
             
    If Dir(F) = "" Then Beep: Exit Sub
        With Me
    .UsedRange.Rows
             R 
    = .Count 1
             V 
    = .Item(3).Value2
        End With
             Application
    .ScreenUpdating False
        With GetObject
    (F).Worksheets(1).[A4].CurrentRegion.Rows
            
    For 1 To UBound(V2)
                
    Application.Match(V(1C), .Item(1), 0)
                If 
    IsNumeric(WThen .Item("2:" & .Count).Columns(W).Copy Cells(RC)
            
    Next
                
    .Parent.Parent.Close False
        End With
             Application
    .ScreenUpdating True
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  3. #3
    Registered User
    Join Date
    01-29-2013
    Location
    The Netherlands
    MS-Off Ver
    2016 x64
    Posts
    48

    Re: Hi ! A demonstration as a beginner starter …

    Hi Marc L, I have tried the code. However I immediately get an error "invalid use of me keyword".
    besides that, I have trouble understanding the code. Can you add some comment text?

    Thanks

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: check and Copy columns from other workbook in a loop


    You did not follow the direction just before the code as you did not paste the code to the appropriate place …
    Last edited by Marc L; 05-19-2019 at 07:28 PM.

  5. #5
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: check and Copy columns from other workbook in a loop

    Possibly...
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    01-29-2013
    Location
    The Netherlands
    MS-Off Ver
    2016 x64
    Posts
    48

    Re: check and Copy columns from other workbook in a loop

    Hi Dangelor,
    Thanks. This looks very good. I love the way you make the code shorter than I did. It is almost what I need. I see that you validate the existence of a column in Target against each column in Data. Can you do the validation the other way around?
    The columns in Target are the mandatory ones and each of these should be available in Data.

  7. #7
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: check and Copy columns from other workbook in a loop

    Try this...
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    01-29-2013
    Location
    The Netherlands
    MS-Off Ver
    2016 x64
    Posts
    48

    Re: check and Copy columns from other workbook in a loop

    Yes, perfect
    I managed to further fine tune the logic to how my actual workbook works.
    Your help is much appreciated

  9. #9
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: check and Copy columns from other workbook in a loop

    You're welcome.

  10. #10
    Registered User
    Join Date
    01-29-2013
    Location
    The Netherlands
    MS-Off Ver
    2016 x64
    Posts
    48

    Re: check and Copy columns from other workbook in a loop

    Hi, after I integrated the code into my actual worksheet and did some more testing I found a strange behavior in the code....

    The idea is that the header in the master sheet is found in the import sheet and if there is a match, then copy the data from the import sheet to the master sheet.
    However, even when just the first part of the header finds a match the data is copied. So there is not a 100% match....

    example:
    header in Master: Project St
    header in Import: Project Status
    in this case there is not a 100% match and still data is copied....

    I believe the issue is in this part of the code...
    Please Login or Register  to view this content.
    I uploaded the example files..

    Thanks
    Attached Files Attached Files

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    As my post #2 code yet works even with your v2 files …

  12. #12
    Registered User
    Join Date
    01-29-2013
    Location
    The Netherlands
    MS-Off Ver
    2016 x64
    Posts
    48

    Re: check and Copy columns from other workbook in a loop

    Hi Marc L,
    I'm sorry to say, but I run into the same error as in post #3 ("invalid use of Me keyword")

    Please Login or Register  to view this content.
    However, I do see why your code might better work.
    The difference I see is the use of the FIND vs the MATCH function.

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: check and Copy columns from other workbook in a loop


    Sorry to say again you didn't copy the code to the right place ‼ Just well read at least post #2 and apply directions …

  14. #14
    Registered User
    Join Date
    01-29-2013
    Location
    The Netherlands
    MS-Off Ver
    2016 x64
    Posts
    48

    Re: check and Copy columns from other workbook in a loop

    ah... yes. My bad :-(
    you are right now it does work. Thanks

  15. #15
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Yes it works … since the 19th May !

    As a relatively new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given.
    By doing so you can add to the reputation(s) of those who helped.

    So thanks to click on bottom left star icon « Add Reputation » of my code post !

+ 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. loop through files find columns and copy/paste them as columns in master workbook
    By Michal1111 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-18-2018, 10:40 PM
  2. [SOLVED] Loop through a range and delete duplicate rows. (Check data in two columns)
    By spikedemike in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-15-2015, 07:44 AM
  3. Check for string match in one column, then loop through other columns
    By sarah07282 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-07-2015, 05:28 PM
  4. Vba to loop to check value and copy results over
    By bkeat in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-09-2013, 08:32 AM
  5. [SOLVED] Loop to check for "yes" then copy the IDs with "yes" and paste to other workbook
    By Hallet in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-11-2012, 12:41 PM
  6. Loop workbook find and copy data columns
    By Zaeguzah in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-22-2010, 06:10 AM
  7. Replies: 0
    Last Post: 07-20-2010, 11:42 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