Solved

Lookup Value from another workbook using VBA

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Outlook Free & Paid Tools
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now