+ Reply to Thread
Results 1 to 10 of 10

Macro to search external file for a value and copy each row of data found

  1. #1
    Registered User
    Join Date
    10-16-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    9

    Macro to search external file for a value and copy each row of data found

    I'm a newbie to the forum and pretty weak with VBA. I've already found lots of great examples here which have been very helpful. I've been working on this current problem for too long now so I'm hoping someone will help me out.

    I am creating a report/form that pulls various bits of data from an external Excel file based on one input value. One of the sheets (LatestBOM) in the external data file has values in the first column that can repeat several times. I want to copy each row that starts with a value matching the one typed into the form. Hopefully the attached files explain it better.

    My strategy... Rather than loop through every row I tried to use the Match function to find the first row that the value shows up. Then my plan was to copy that row's data and use a Where Loop to look at the next row to see if the value still matched, then copy that row and so on. I can't seem to get it to work and my code is too messy to post. Any help is appreciated.

    SampleData.xlsm
    SampleForm.xlsm

    - Ian

  2. #2
    Forum Contributor
    Join Date
    09-30-2013
    Location
    Toronto, ON
    MS-Off Ver
    Excel 2007
    Posts
    229

    Re: Macro to search external file for a value and copy each row of data found

    following will find data in criteria cell in LatestBOM and loop through rows in LatestBOM
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    10-16-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Macro to search external file for a value and copy each row of data found

    Thank you so much for your reply. I should be able to figure out what to put in the Do While loop.

    I noticed that the SampleData.xlsm file must be open. How can the file be accessed by the macro in a way that's invisible to the user? Of course in the real Excel files the data file is much bigger and the forms will be used 1000's of times, so I want to cut down on steps.

    Thanks!

    - Ian

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to search external file for a value and copy each row of data found

    Hello noxidnai,

    Welcome to the Forum!

    If you are interested, I have modified your 2 original workbooks to use only Excel formulas. The Data workbook contains 2 dynamic named ranges:BOMs and BOM_Data. These are used in the formulas for the Form workbook to fill in the table.
    You will notice the extension is now XLSX instead of XLSM. This will allow others to use the workbooks without restriction. Let me know if this might be useful for you.
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    10-16-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Macro to search external file for a value and copy each row of data found

    Amazing! I would have never thought it could be done without VBA. I had to read up on dynamic name ranges, but I think I get it.

    It appears that you still need to open the SampleData.xlsx file for it to work, correct?

    Also, I like that a macro needs to be run by the user so data doesn't update automatically. I was planning to add a button to run the macro. Any ideas on this? I'm pretty sure that switching calculations from automatic to manual is a global setting... not just the workbook. Are Workbook Connections useful in this situation?

    - Ian

  6. #6
    Forum Contributor
    Join Date
    09-30-2013
    Location
    Toronto, ON
    MS-Off Ver
    Excel 2007
    Posts
    229

    Re: Macro to search external file for a value and copy each row of data found

    This will check to see if SampleData is open, and if not, open it.
    I am having an issue on trying to close SampleData when you close SampleForm. You don't want to close SampleData at the end of the macro or it takes too much time.
    Perhaps someone else can assist on this. I think that it should be in the Workbook_BeforeClose event, but I can't get it to work.
    You need to change sDataFile to the appropriate path/filename

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    10-16-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Macro to search external file for a value and copy each row of data found

    hectop,

    Thank you again. I am still wondering if the file can be opened and closed silently.

    - Ian

  8. #8
    Registered User
    Join Date
    10-16-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Macro to search external file for a value and copy each row of data found

    Leith,

    I decided to use the non-macro method you gave above. It looks like the source file (sampledata.xlsx) must be open for this to work. Is this correct or did I miss something?

    - Ian

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro to search external file for a value and copy each row of data found

    Hello Ian,

    You are correct. The source workbook must be open for the formulas to work correctly. Is that an issue?

  10. #10
    Registered User
    Join Date
    10-16-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: Macro to search external file for a value and copy each row of data found

    It is a bit of an issue but I think I worked around it. I changed the formulas such that the Names are not used and it seems to work without opening the source file. Oh, and I also got ride of the Countif() formula as it seemed like it required the source file to be open too.

    Thanks for all your help! I'm sure I will reply if I get stuck again.

    - Ian

+ 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. Macro to search and copy data from the latest file on network computer
    By kishoremcp in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-19-2012, 06:55 AM
  2. Search Data from Multiple Sheets and copy found data to new workbook
    By xenith1988 in forum Excel Programming / VBA / Macros
    Replies: 27
    Last Post: 06-26-2012, 12:58 PM
  3. Search cell (external book) and delete row if found
    By Zaeguzah in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-22-2009, 03:47 AM
  4. Macro to Search column and if found, copy a cell within the worksheet
    By SKooT1027 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-16-2008, 01:12 PM
  5. Replies: 1
    Last Post: 02-10-2005, 07:06 PM

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