+ Reply to Thread
Results 1 to 2 of 2

vlookup with VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    01-12-2011
    Location
    Victoria, BC
    MS-Off Ver
    Excel 2003
    Posts
    42

    vlookup with VBA

    Hello,
    I would like to add the following ability to my spreadsheet. When there is a unit number populated in a cell, i would like to either double click on it, or hover the mouse over the unit number and be able to see information on the unit in a quick flash message. I do NOT want to populate the information in the spreasheet, just be able to refer to it and keep working.

    I have the information I want to display in another workbook and it would be simple enough to do a vlookup on the unit number and find unit type, unit year, unit status. However I want the information to come up in the form of a msgbox or Control tip text.

    Can anyone coach me on how to do a lookup in VBA?

    Thanks,
    Todd

  2. #2
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: vlookup with VBA

    here is some code that you might start with. You can't activate an event in Excel on a hover or a double click, but you can capture a selection change. This code opens a reference workbook the first time a specific sheet is actuivated, and then wherever the selection is changed in that sheet, it does the lookup on the reference table and display the message. Note that there are a number of assumptions abou the structure of the reference workbook and the location of the unit numbers.
    Put this code in VBA on the sheet object that you want the lookup to apply to. See if it gets close to what you want and we will go from there.
    Option Explicit
    ' Global reference sheet name
    Dim ReferenceSheet As Worksheet
    
    ' the name of the reference sheet to use
    Const ReferenceBookName As String = "Reference.xls"
    Dim ReferenceBook As Workbook
    
    ' make sure the reference workbook is open
    Private Sub Worksheet_Activate()
    
        ' if the reference book has not yet been opened, then open it and address the reference sheet
        If (ReferenceBook Is Nothing) Then
            On Error Resume Next
            Set ReferenceBook = Workbooks.Open(Filename:=ReferenceBookName, ReadOnly:=True)
            If (Err.Number = 0) Then
                Set ReferenceSheet = ReferenceBook.Worksheets(1) ' assumes the reference sheet is sheet1
            Else ' can't open the reference book
                Set ReferenceSheet = Nothing
            End If
            On Error GoTo 0
        End If
        
        ' reactive this sheet
        Me.Activate
    
    End Sub
    
    ' produces a popup message containing data from a table
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        ' skip this is there is no reference sheet
        If (ReferenceSheet Is Nothing) Then
            Exit Sub
        End If
    
        ' assumes that the lookup is to occur on values in column A
        ' select only column A and ignore the header row
        If (Target.Column = 1 And Target.Row > 1) Then
    
            ' lookup the entry in the lookup table which is located in the on the reference sheet
            On Error Resume Next
            Dim theRow As Long
            
            ' assume the reference table is in column A
            theRow = Application.WorksheetFunction.Match(Target.Value, ReferenceSheet.Range("A:A"), 0)
            If (Err.Number = 0) Then
            
                'get the data from the rest of the row
                Dim theType As String
                Dim theYear As String
                Dim theStatus As String
                theType = ReferenceSheet.Cells(theRow, "B").Value
                theYear = ReferenceSheet.Cells(theRow, "C").Value
                theStatus = ReferenceSheet.Cells(theRow, "D").Value
                MsgBox "Unit = " & Target.Value & vbCr & _
                       "Type = " & theType & vbCr & _
                       "Year = " & theYear & vbCr & _
                       "Status = " & theStatus
            End If
            On Error GoTo 0
        End If
    
    End Sub
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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