Consulting

Results 1 to 5 of 5

Thread: Solved: Use Vlookup in VBA Code

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    Solved: Use Vlookup in VBA Code

    I want to use a worksheet change event that will execute certain vlookups only when a certain condition in a cell is met. I want to do this because I do not know of any other way to keep the formulas from being wiped out as I will be setting the cells I currently have the lookups in to zero in some instances hence wiping out the formula.

    Can someone provide an example?
    Peace of mind is found in some of the strangest places.

  2. #2
    VBAX Newbie
    Joined
    Jul 2010
    Posts
    5
    Location
    Quote Originally Posted by austenr
    ........the lookups in to zero in some instances........

    try to convert the 'some instances' to <condition> and use vlookup() in if(<condition>,vlookup(),0) or if(<condition>,0,vlookup())

  3. #3
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    I got this far but it doesnt like the "1" in the formula. I get an "expected end of statement" error on compile.

    [VBA]Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Sheets("CP1").Select

    If Range("D12").Value = "X" Then
    Range("E25").formula = "=IF(TRIM(MEDCVG)="1",VLOOKUP(YearsOfService,Indemnity,IF(Age<=65,7,14),FAL SE),0)/12"
    End If
    End Sub
    [/VBA]
    Peace of mind is found in some of the strangest places.

  4. #4
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Fixed it. Needed double quotes around the 1.
    Peace of mind is found in some of the strangest places.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Austen
    My tip for complicated (or simple) quotes.
    Put your formula in the worksheet, delete the = sign, record a macro inserting the = and you should get a working VBA formula.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •