+ Reply to Thread
Results 1 to 3 of 3

Storing vlookup as variable in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    08-05-2010
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    36

    Storing vlookup as variable in VBA

    Hello all,

    I'm looking to store different values to a variable that would be holding a vlookup function. I'm storing the variable as a double, but one of the criteria that my macro checks is to make sure that the vlookup is actually succesful, (i.e. if iserror(application.vlookup(BLAHBLAH)) then).

    Here's the example I'm talking about:

    'VPL Pricing
        VPL = Application.VLookup(Cells(50 + x, 1), Sheets("CATALOGFORMACRO").Range("$A:$N"), 9 + y, False)
        'Pallet Pricing
        PAL = Application.VLookup(Cells(50 + x, 1), Sheets("CATALOGFORMACRO").Range("$B:$N"), 8 + y, False)
    
        x = 0
        c = 1
        y = 0
        r = 0
        
        Do While (Not Cells(50 + x, 1) = "Fin")
        
            Do While (y < 6)
            
                If IsError(VPL) Then
                
                    Cells(50 + x, 6) = "DISC"
                    x = x + 1
                    y = 6
                    
                Else
                    
                    If (Cells(50 + x, 5) < VPL) Then
                        y = y + 1
                    
                    Else
                    
                        If VPL = "" Then
                            
                            'Check for Master
                            If VPL = "" And PAL <> "" Then
                                
                                If  --------- so on and so forth
    Is this something that is allowed in VBA? Do I have to declare the variable as a certain type?

    Any help would be great. Thank you very much

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Storing vlookup as variable in VBA

    If I understand correctly, I think you have to declare it As Variant.

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Storing vlookup as variable in VBA

    Your code isn't doing what you think. It's doing the lookup once, prior to the loop, and then using the result repeatedly.
    Entia non sunt multiplicanda sine necessitate

+ 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