Solved

Lookup Value from another workbook using VBA

Posted on 2010-11-12
4
484 Views
Last Modified: 2012-05-10
Hi, as a VBA newbie I'm struggling with this challenge.

I have two spreadsheets, a source spreadsheet containing a table of data and a destination spreadsheet into which I need to have data placed at the press of a button.

I have attached each sheet. In the destination s/sheet the user will type the store number, then click a button. This will then look up the matching record in the source spreadsheet and then copy the average balance value into the destination s/sheet (into the yellow box).

All help gratefully received.
Destination.xlsx
Source.xlsx
0
Comment
Question by:jonlake
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
broomee9 earned 500 total points
ID: 34122097
Try this:

Open both workbooks
Enter a Store Number
Click the Get Avg Balance button
Option Explicit

Private Sub cmdGetValue_Click()
    Dim storeNum As String
    Dim avgBalance As Double
    
    storeNum = Range("A4").Value
    
    With Workbooks("Source.xlsx").Sheets("Source")
        On Error Resume Next
        avgBalance = WorksheetFunction.VLookup(storeNum, .Range("A1:D10000"), 4, False)
        On Error GoTo 0
    End With
    
    ThisWorkbook.Activate
    Sheets("Destination").Range("B4").Value = avgBalance
End Sub

Open in new window

Destination.xlsm
Source.xlsx
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 34122102
Why do you need VBA? You could use a formula like this:

=VLOOKUP(A4,Source.xlsx!$A$2:$D$1999,4,FALSE)
0
 

Author Closing Comment

by:jonlake
ID: 34122288
Thanks, this works very well. As a bonus is it possible to have the lookup operate if the source s/sheet were closed?
0
 

Author Comment

by:jonlake
ID: 34122302
Hi StephenJR, unfortunately the s/sheet cannot contain formulas as it is loaded into our accoutning package and will freak out if it contains code. My plan is to use the code currently assigned to a button as an add-in, operated from a ribbon button.

Thanks to all.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question