+ Reply to Thread
Results 1 to 5 of 5

New to VBA, need to do Vlookup after a value is entered

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-11-2016
    Location
    Atlanta
    MS-Off Ver
    2016
    Posts
    104

    New to VBA, need to do Vlookup after a value is entered

    I have a couple VBA questions as this is my first time taking on a project involving VBA.

    What I want to happen:
    1. The user presses the button and an inputBox appears asking to input an auction number and an auction seller. These values are stored under variables AuctionNum and AuctionSeller.
    2. Today's date is updated to the current date once the button is pressed and is stored in the variable TodayDate. This value is placed in the format mm/dd/yy in cell H2.
    3. Vlookup formulas search through the database tab in cells B3:F30 to return the auction date in cell c7 and the total cars in cell c9. These values are stored in the variable AuctionDate and TotalCars.



    The code I have been working on is currently under module 1 and i apologize if the formatting or anything is wrong. This is my first crack at something like this and am trying to learn.

    I hope this makes sense and will try to answer any questions promptly.

    Thank you,

    Matt

    **I'm using excel 2016 for Mac
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    06-11-2016
    Location
    Atlanta
    MS-Off Ver
    2016
    Posts
    104

    Re: New to VBA, need to do Vlookup after a value is entered

    Still having trouble with this. I'm getting an error box after i run the code that says 400.

    Dim AuctionDate as String
    Dim AuctionNum as String
    Dim ws1, ws2, ws3 As Worksheet
        Set ws1 = ThisWorkbook.Sheets("Daily")
        Set ws2 = ThisWorkbook.Sheets("Database")
        Set ws3 = ThisWorkbook.Sheets("Data")
    
    AuctionNum = InputBox("Please enter the auction number", "Next Auction Data")
    Range("c6").Value = AuctionNum
    
    AuctionDate = Application.WorksheetFunction.VLookup(AuctionNum, ws2.Range("B3:F30"), 2, False)
    MsgBox ("value is " & AuctionDate)
    I just need to use the inputed value in the input box for a Vlookup function. That's all I'm trying to do.
    Last edited by Buzz1126; 10-21-2016 at 10:32 PM.

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    12,967

    Re: New to VBA, need to do Vlookup after a value is entered

    1. VLookup finds a match in a column and returns a value from a column to the right not the left.

    2. In order to use VLookup, the auction numbers must be in the first column of the database, not the second. As it is, you are looking for the auction number string in a column of dates (excel numbers).

    3. Your textbox is returning a text string, say "50", while the auction numbers in the database are Excel number values. So, even if the first column were auction numbers, VLookup would fail because "50" is not 50. So, in addition to being the first column, the auction number column must be formatted as text not general. Alternatively, you can convert the textbox value from string to integer before the lookup.

    4. To calculate the total cars for an auction# you will have to use Countif()
    Ben Van Johnson

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: New to VBA, need to do Vlookup after a value is entered

    What Ben said. Try something like this...

    Note: format cell C7 with any date format you like.

    Sub Run_Button()
        
        Dim RowMatch As Variant      'Row number of the matched Auction Number
        Dim AuctionNum As Integer    'the auction number
        
        'updates the date with todays date
        Range("H2").Value = Date
        
        'prompts the user to enter the auction number then stores it in the variable AuctionNum,
        'the value is then placed in cell c6 of the workbook
        AuctionNum = Application.InputBox("Please enter the auction number", "Next Auction Data", Type:=1)
        If AuctionNum = 0 Then Exit Sub  'User canceled
        Range("C6").Value = AuctionNum
        
        'Match Auction Number and return the row number of 1st match
        RowMatch = Application.Match(AuctionNum, Sheet2.Range("C:C"), False)
        
        If Not IsError(RowMatch) Then   'Test if match was found
            Range("C7").Value = Sheet2.Range("B" & RowMatch).Value 'Return Value from column B of the matched row
            Range("C8").Value = Sheet2.Range("D" & RowMatch).Value 'Return Value from column D of the matched row
            Range("C9").Value = Application.CountIf(Sheet2.Range("C:C"), AuctionNum) 'Count rows that have the auction number
        Else
            MsgBox "No match found for " & AuctionNum, , "Auction Number Not Found"
        End If
        
    End Sub
    Last edited by AlphaFrog; 10-22-2016 at 01:47 AM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    Forum Contributor
    Join Date
    06-11-2016
    Location
    Atlanta
    MS-Off Ver
    2016
    Posts
    104

    Re: New to VBA, need to do Vlookup after a value is entered

    Worked perfectly, thank you both so much.

+ 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. Transform entered Text into a clickable Hyperlink via VLOOKUP
    By stonegate in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-15-2016, 08:01 PM
  2. Use VLOOKUP to Apply the Formula Entered in the Matched Cell
    By BobGears in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-18-2016, 02:19 PM
  3. [SOLVED] Return last entered data via VLOOKUP over multiple cells.
    By realrookie in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-06-2016, 03:09 PM
  4. [SOLVED] MsgBox Text Based on Value entered in UserForm (Vlookup?)
    By silentcarl in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-18-2015, 08:14 AM
  5. Replies: 2
    Last Post: 09-27-2014, 02:51 PM
  6. Replies: 3
    Last Post: 04-21-2013, 02:17 AM
  7. VLOOKUP only works on data manually entered.
    By Mike Rogers in forum Excel General
    Replies: 6
    Last Post: 04-25-2006, 02:20 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