Solved

Lookup Value from another workbook using VBA

Posted on 2010-11-12
4
477 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
  • 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

770 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