Consulting

Results 1 to 4 of 4

Thread: Use VBA to populate a VLOOKUP formula - then move that worksheet to another workbook.

  1. #1
    VBAX Newbie
    Joined
    May 2010
    Posts
    4
    Location

    Use VBA to populate a VLOOKUP formula - then move that worksheet to another workbook.

    Hi All,

    I'm using a an excel "program" which looks up weekly information, filters and formats it. I then "move as a copy" to the previous week's file, appending it to the end as a new worksheet.

    Currently I update a few columns with information about that particular entry [row]. Each week I populate those columns MANUALLY with a VLOOKUP function to the previous week's worksheet (I have to change the worksheet name each time and then drag the formula down all the rows in that column), then I copy/pastespecial values and add any new updates, or update any new entries.

    What I want to do is add to the program a way to populate these columns with the formula automatically - finding the name of the previous week's worksheet is not a problem.

    What I have found though is that when it populates a VLOOKUP formula, it immediately opens a window for me to browse to the file where the worksheet is contained, since it doesn't exist in the program's workbook.

    I would really appreciate it if someone could help me with this. I'm hoping that there's some sort of format or option which can be set which prevents the program looking for the specified worksheet if it's not in the workbook... or maybe there's another way to do it?

    In case it helps, here's the formula I use:

    =IF(ISERROR(VLOOKUP($C3,'4QTRWk04'!$C$3:$X$201,19,FALSE)),"",IF(VLOOKUP($C3,'4QTRWk04'!$C$3:$X$201,19,FALSE)="","",VLOOKUP($C3,'4QTRWk04'!$C$3:$X$201,19,FALSE)))
    So it's looking up the 4QTR worksheet from Wk04, matching column

    C and returning what is in column 19.

    And I would just use this piece of VBA code to put it in a cell and copy it down:

    Range("V2").Select
        ActiveCell.Formula = "=IF(ISERROR(VLOOKUP($C3,'4QTRWk04'!$C$3:$X$201,19,FALSE)),"",IF(VLOOKUP($C3,'4QTRWk04'!$C$3:$X$201,19,FALSE)="","",VLOOKUP($C3,'4QTRWk04'!$C$3:$X$201,19,FALSE)))"
    Selection.Copy
        Range("V3:V" & LastRow).Select
        Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,478
    Location
    Welcome to VBAX
    Just write the formulae to the whole range
    [vba]
    Range("V2:V" & lastrow).Formula = "=IF(ISERROR(VLOOKUP($C3,'4QTRWk04'!$C$3:$X$201,19,FALSE)),""""," & _
    "IF(VLOOKUP($C3,'4QTRWk04'!$C$3:$X$201,19,FALSE)="","",VLOOKUP($C3,'4QTRWk0 4'!$C$3:$X$201,19,FALSE)))"

    [/vba]

    BTW extra "" added to correct the formula as written by the code.
    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'

  3. #3
    VBAX Newbie
    Joined
    May 2010
    Posts
    4
    Location
    Hi,

    I managed to do what you said, and in the macro while it copied the formula into the range of cells I set Application.DisplayAlerts to False, then when it finished set it back to True.

    This prevented any window popping up to ask where the worksheet was located.

    Then I right-clicked on the spreadsheet and said "Move or Copy..." and selected "Make a Copy". I told it to copy the spreadsheet into another workbook I had open. This is the workbook that contains the spreadsheet which I want the VLOOKUP to work on.

    That went successfully. However, the VLOOKUP formula did not produce any results. All the cells stayed blank. I had to manually edit a cell and press the ENTER key for it to actually calculate the formula.

    Calculations are set to automatic in the options, and even clicking "Calc Now" or "Calc Sheet" did not refresh them.

    Does anyone have any suggestions as to how to get around this? This is a program other people need to use, so I can't just tell everyone they're going to have to manually input formulas every week (that wouldn't go down well).

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,478
    Location
    Can you post a workbook with sample data? Use Manage Attachments in the Go Advanced reply section.
    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
  •