PDA

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



abrownin
05-02-2010, 06:05 PM
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

mdmackillop
05-03-2010, 07:42 AM
Welcome to VBAX
Just write the formulae to the whole range

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,'4QTRWk04'!$C$3:$X$201,19,FALSE)))"



BTW extra "" added to correct the formula as written by the code.

abrownin
05-03-2010, 10:12 PM
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).

mdmackillop
05-04-2010, 12:13 AM
Can you post a workbook with sample data? Use Manage Attachments in the Go Advanced reply section.