+ Reply to Thread
Results 1 to 12 of 12

Copy data based on on specific header "name"

  1. #1
    Registered User
    Join Date
    09-09-2013
    Location
    MAL
    MS-Off Ver
    Excel 2007
    Posts
    22

    Copy data based on on specific header "name"

    Hi,
    I’m using Excel 2007 and I have 2 files, source.xlsx and destination.xls. I have attached the sample files here.

    I’m only interested in only a couple columns of data in “Working” worksheet in source.xls file. They are under the header “Part Number”, “In”, and “Category”. There are other columns in between these 3. However the number of column in this source file is not always the same. For example, the file this week may have only 2 random columns in between “Part Number” and “In”. Next week, the number of random columns in between them maybe 10. So it’s important for me to identify the data by the exact column header name.

    I want to copy all data under header “Part Number”, “In”, and “Category” in source.xlsx, and then paste them to destination.xls (to be saved as Excel 97-2003 workbook). In “destination.xls”, the data should go to the row (insert new row) under the header “*Item Name”, “Qty Count”, and “Cat”. Also, under the header “New Name”, it will take the value from “*Item Name” then append a string “-NEW” to it.

    Does anyone here know how to do this with vba code?
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Copy data based on on specific header "name"

    Copy this to a code module in source.xlsx. You'll need to save it as a .xlsm.

    Please Login or Register  to view this content.
    Acts 4:12
    Salvation is found in no one else, for there is no other name under heaven given to mankind by which we must be saved.

  3. #3
    Registered User
    Join Date
    09-09-2013
    Location
    MAL
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Copy data based on on specific header "name"

    The code works perfectly!!! Appreciate your effort and time. Many thanks

  4. #4
    Registered User
    Join Date
    09-09-2013
    Location
    MAL
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Copy data based on on specific header "name"

    Since the source.xlsx is provided by another person, I don't want to repaste your code everytime he sends a new source file. I created a new A.xlsm and copied your code there. When I run the code from A.xlsm I want to be able to just select where the source.xlsx and destination.xls files are. I added these lines on top of your code.

    Please Login or Register  to view this content.
    But I'm stuck at this line:

    Please Login or Register  to view this content.
    I try to change it, but it doesn't work.
    Please Login or Register  to view this content.
    Can you help?

  5. #5
    Registered User
    Join Date
    09-09-2013
    Location
    MAL
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Copy data based on on specific header "name"

    I managed to fix the error by adding this line before it.

    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Copy data based on on specific header "name"

    I would need to see the entire subroutine to evaluate.

  7. #7
    Registered User
    Join Date
    09-09-2013
    Location
    MAL
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Copy data based on on specific header "name"

    Here is the code.

    Please Login or Register  to view this content.
    I find that if the sequence of heading in the source.xlsx changes, e.g. “Category” comes before “In”, the code will fail. Is it possible to use something like .Cells.Find("In", LookAt:=xlWhole) to solve this problem?

    2 other things I’m trying to achieve here. I remove formatting before pasting the data to destination.xls, and try to process the category data by adding a string “_PASS_OK” to it in the same cell. The remove formatting part works but not for adding a suffix to category. I get Type mismatch error.

    Also, I try to clear the selection in both source and destination file. I tried a few examples I found on the web, but this too doesn’t work.

  8. #8
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Copy data based on on specific header "name"

    What you did with shtSource.Activate is a good solution. Here's the other way, just so the problem is clarified, but it's messier:

    Please Login or Register  to view this content.
    An even better way might be to reverse the order in which you open the workbooks. By opening shtSource last, it would already be the active workbook. That's up to you though.

    Regarding the column sequence, that is resolved by resetting the value of SrcCol back to 3 before starting the search.

    You can't concatenate a value (_PASS_OK) to a range of values with a single command. You have to loop through the range and add it to each cell.

    By "clear the selection", I assume you to mean that you want to end up with a single cell selected, rather than a range, in both sheets. You can only select a cell and/or range on a sheet that is the active sheet. In your last line, above, you're trying to select a cell in shtTarget, but shtSource is the active sheet.

    The code below will hopefully resolve all issues mentioned. I also updated the file open procedures to allow for cancelling the open.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    09-09-2013
    Location
    MAL
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Copy data based on on specific header "name"

    Wow! You solved all the issues. Thank you!
    I made one change to your code to fix the "_PASS_OK" part.

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    09-09-2013
    Location
    MAL
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Copy data based on on specific header "name"

    Just something extras to make the code even better.

    Sometimes, the source file comes with an additional column "Class". I want to add a check for this heading. If found, the whole range from source file under Class heading is copied to column 5 in target file. If the heading is not found, it'll skip the copy. I use Goto command.

    Please Login or Register  to view this content.
    In shtTarget the column 2 "New Name" shows "100A21-NEW" but it actually stores a formula e.g =A12&"-NEW". To change them to value instead of formula. I added this code below the .formula line to reselect the range, copy, then paste as value only.

    Please Login or Register  to view this content.
    Lastly I want to make sure the column 3 "Qty Count" is rounded and has no decimal point. If value is 20.1, save it as 20. If value is 20.9, save it as 21. Again, I use the sample you make for adding "_PASS_OK" .

    Please Login or Register  to view this content.
    Is there a better way to make the code more efficient?
    Last edited by Efjoker; 09-15-2014 at 03:21 AM.

  11. #11
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Copy data based on on specific header "name"

    Looks like you're handling it pretty well.

  12. #12
    Registered User
    Join Date
    09-09-2013
    Location
    MAL
    MS-Off Ver
    Excel 2007
    Posts
    22

    Re: Copy data based on on specific header "name"

    I have a very good start, all thanks to you.

+ 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. [SOLVED] :confused: Auto pick "specific data" and put it in "specific cells" with date
    By pipsmultan in forum Excel Formulas & Functions
    Replies: 29
    Last Post: 08-27-2014, 03:31 AM
  2. [SOLVED] Code that copies row of data to another sheet based on text "Complete"/"Delete"
    By Dremzy in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 04-20-2014, 05:51 PM
  3. Copy data from multiples sheets to specific cells in "summary"
    By sweetboy02125 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-17-2010, 10:05 AM
  4. Replies: 0
    Last Post: 07-17-2006, 09:45 AM
  5. Copy a specific of data of texts containing "IT"
    By new_to_vba in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-08-2006, 04:00 AM

Tags for this Thread

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