+ Reply to Thread
Results 1 to 21 of 21

VBA that uses a list to pull data from another sheet.

  1. #1
    Registered User
    Join Date
    11-04-2022
    Location
    Missouri
    MS-Off Ver
    2010
    Posts
    4

    VBA that uses a list to pull data from another sheet.

    Hello,
    I have two Excel workbooks, BOOK A contains a master list of ID Numbers and corresponding address information. BOOK B has ID Numbers but no address information. I am looking for a VBA that will use a list of ID Numbers from BOOK B, find them in BOOK A, copy the address information from cells B,C,D,&E in Book A, and drop them next to the ID Number from the list in Book B. Any suggestions?
    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

    Question Re: VBA that uses a list to pull data from another sheet.


    Hello,

    why not an easy Excel basics worksheet VLOOKUP formula rather than any VBA procedure ?

  3. #3
    Registered User
    Join Date
    11-04-2022
    Location
    Missouri
    MS-Off Ver
    2010
    Posts
    4

    Re: VBA that uses a list to pull data from another sheet.

    Thanks for the response, I was under the assumption that VLOOKUP was only for data contained in a single workbook. My data sets live in two workbooks. Please correct me if I am wrong, my advanced Excel skills are minimal.

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

    Arrow Re: VBA that uses a list to pull data from another sheet.


    As the Excel basics worksheet function VLOOKUP works whatever the workbook, even closed ! …

  5. #5
    Registered User
    Join Date
    11-04-2022
    Location
    Missouri
    MS-Off Ver
    2010
    Posts
    4

    Re: VBA that uses a list to pull data from another sheet.

    That's good to know, any idea what the formula would look like based on the attachments? I'm having trouble wrapping my head around what to key in.

  6. #6
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA that uses a list to pull data from another sheet.

    .
    Hello LIBRARYBEND.

    • If the number of data rows in A and B is small, then using VLookUp is enough for you.
    • If that amount is a bit larger, then it's more efficient to use the pair: Index and Match.
    • And if the number of rows of data is very large, a good macro would be suitable.

    So I ask:
    • How many rows of data do these workbooks have?
    • When you go to do this search for information: Will workbook A be closed or will both workbooks be open?

  7. #7
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,694

    Re: VBA that uses a list to pull data from another sheet.

    Saved both workbooks as macro enabled (.xlsm) workbooks.
    Both workbooks need to be open. If not, let us know which workbook will have the macro in it so code can be changed for that.
    Change references where required.
    As it stands, code can be run from either workbook.
    Please Login or Register  to view this content.
    The inherent weakness of the liberal society: a too rosy view of humanity.

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

    Cool Or try this ...


    In case BOOK A - whatever opened or not - in same path as BOOK B already opened

    so according to the attachment an Excel beginner formula VBA demonstration - can be located in a third workbook - for starters :

    PHP Code: 
    Sub Demo1()
     Const 
    "'[BOOK B.xlsx]Sheet1'!B2"
           
    Evaluate("ISREF(" ")"):  If IsError(VThen V False
        
    If V Then
            With Range
    (":E" Range(C).CurrentRegion.Rows.Count)
                .
    Formula Evaluate("""=IFERROR(VLOOKUP(FIXED(A2,0,TRUE),'" & .Parent.Parent.Path _
                                    
    "\[BOOK A.xlsx]Sheet1'!A:E,""&{2,3,4,5}&"",FALSE),"""""""")""")
                .
    Formula = .Value
            End With
        End 
    If
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 11-05-2022 at 08:58 AM. Reason: optimization ...

  9. #9
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA that uses a list to pull data from another sheet.

    Hello. In this variant:
    • Book A can be open or closed.
    • Book B will contain the macro.

    PHP Code: 
    Sub Test1()
    Dim Conn$, RstCel As Range
    Conn 
    "Data Source=" ThisWorkbook.Path "\Book A.xlsx;Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0; HDR=NO'"
    Set Rst CreateObject("ADODB.Recordset"): [a1].CurrentRegion.Offset(11).ClearContents
    For Each Cel In Range("A1"Cells(Rows.Count1).End(xlUp))
      
    Rst.Open "Select F2,F3,F4,F5 FROM [Sheet1$A1:E] Where F1='" Cel "'"Conn331
      
    If Not Rst.EOF Then Cel(, 2).CopyFromRecordset Rst
      Rst
    .Close
    Next
    End Sub 

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

    Arrow Re: VBA that uses a list to pull data from another sheet.


    Quote Originally Posted by beyond Excel View Post
    Book A can be open or closed.
    As a reminder Book A should be closed to avoid the well known 'data leak' issue with ADODB, rare but may occur …

  11. #11
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA that uses a list to pull data from another sheet.

    Known 'data leakage' issue with ADODB?...
    Not so well known: What do you mean, Mark?

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

    Arrow Re: VBA that uses a list to pull data from another sheet.


    Such issue may occur when a workbook is opened under Excel and reached via ADODB under a VBA procedure
    so the reason why it's better recommanded to use ADODB only on closed workbooks …

  13. #13
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA that uses a list to pull data from another sheet.

    Quote Originally Posted by Marc L View Post
    Such issue may occur when a workbook is opened under Excel and reached via ADODB under a VBA procedure
    so the reason why it's better recommanded to use ADODB only on closed workbooks …
    Mmm...
    Such a "thing" never happened to me: And I hope that in the future it never happens to me!

    Although I did not understand what is the problem that occurs sporadically ...

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

    Arrow Re: VBA that uses a list to pull data from another sheet.


    It's rare indeed. But keep it in mind if one day you face this issue as it is difficult to catch …

  15. #15
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA that uses a list to pull data from another sheet.

    Quote Originally Posted by Marc L View Post

    It's rare indeed. But keep it in mind if one day you face this issue as it is difficult to catch …
    I have a "slight" suspicion that this can happen in the case of writing information and not in the case of reading information, like this query: But we'll see!

    Thanks for the comment.

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA that uses a list to pull data from another sheet.

    Memory leak by ADO connection with open workbook is well known issue.

    LIBRARYBEND,
    I prefer VLookUp formula, it is easy and fast.

    If ADO connection
    Assuming both workbooks are in the same folder.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by jindon; 11-08-2022 at 02:30 AM. Reason: One line added

  17. #17
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA that uses a list to pull data from another sheet.

    Hey Jindon: You're doing a bit of "cheating"! (LOL)

    In the original workbook B, the information in column A are numbers that are left-aligned, while in workbook B that you loaded in your recent proposal, they are numbers but saved as text.

    And that's why the proposal works for you. Otherwise, the following would not work in ADODB: B.`Par ID` = A.`Par ID`, right?...

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA that uses a list to pull data from another sheet.

    If you call it "cheating", it is your idea, so I uploaded the file.

    Mind yours, rather than picking up a tiny dust from other ones code.

  19. #19
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: VBA that uses a list to pull data from another sheet.

    .
    • The workbook is in post #1.
    • And I didn't understand your second comment: I have a feeling that you hasn't been very kind and it doesn't correspond to my technical observation, but... There you are!

  20. #20
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: VBA that uses a list to pull data from another sheet.

    Why people as such can only comment from what they guess...

  21. #21
    Registered User
    Join Date
    11-04-2022
    Location
    Missouri
    MS-Off Ver
    2010
    Posts
    4

    Re: VBA that uses a list to pull data from another sheet.

    This worked brilliantly, thanks for your help.

+ 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. Data Validation - Drop down list to pull the whole sheet
    By Sam D in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 05-26-2018, 07:29 PM
  2. User List to Pull in Excel Data from another Sheet
    By ksikor14 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-03-2018, 07:49 PM
  3. Replies: 5
    Last Post: 07-22-2014, 06:29 AM
  4. Replies: 4
    Last Post: 08-14-2013, 09:14 PM
  5. Replies: 0
    Last Post: 05-06-2013, 08:33 AM
  6. Data pull from other sheet as per selection on DropDown List
    By Abdul Haneef in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-31-2012, 07:31 AM
  7. Replies: 7
    Last Post: 12-06-2010, 10:55 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