VLookup - Visual Basic - how to set a range - excel

I have a Vlookup formula set in cell D5 that checks some data in cell E5, does the Vlookup below, and returns the result:
=VLOOKUP(E5,StockCodesBBG!A:J,2,FALSE)
What I would like to do is to have a way of automatically counting the number of rows I have populated from E5 down, and then running the Vlookup formula for each of those corresponding cells.
For example, say I have data in E5,E6,E7, I would like the formula to apply itself into D5,D6,D7.
I can just Autofill this down by copying the formula, but it leaves a load of messy #N/A values where there is nothing in the E column to reference. How do I avoid the in cell error messages?

You can AutoFill it but at the same time handle the #N/A so that you do not see those obnoxious values :)
If using xl2003, use this
=if(ISERROR(VLOOKUP(E5,StockCodesBBG!A:J,2,FALSE)),"",VLOOKUP(E5,StockCodesBBG!A:J,2,FALSE))
or
=if(ISNA(VLOOKUP(E5,StockCodesBBG!A:J,2,FALSE)),"",VLOOKUP(E5,StockCodesBBG!A:J,2,FALSE))
If using xl2007+, use this
=IFERROR(VLOOKUP(E5,StockCodesBBG!A:J,2,FALSE),"")
Check the Excel help on ISERROR,ISNA,IFERROR

Related

Excel table auto-fills incorrect formula when inserting rows

I have an Excel 2019 Workbook in which I have a sheet with a range of cells defined as a table. Table columns are A to H, where columns A and C contains formulas and the others have to be manually compiled with information by users. The sheet is protected, with the cells in columns without formulas unlocked, so that users cannot alter formulas, but they are allowed to add rows (in the middle of the table, not at its end) as this can be required.
The problem is that when adding rows, the formula auto-filled in column A for the row below the new one is incorrect.
Detailed explanation
Column A is used to assign an unique number to what si written in column B, so that if an element in column B is repeated, the corresponding number in column A is repeated as well
The first cell of the table in column A (A4) contains the value "1"
Cell A5 contains the formula "=IF(B5=B4;A4;A4+1)"
Cell A6 contains the formula "=IF(B6=B5;A5;A5+1)", and so on
If I add a row between 5 and 6, the formula in the new cell A6 is correct, but the formula in cell A7 becomes "=IF(B7=B5;A5;A5+1)", instead of the correct "=IF(B7=B6;A6;A6+1)"
Formulas in all the cells below A7 are correct
If I manually fill the formula in A7 form another cell by using the fill handle, the formula corrects itself
I know there is a similar thread MS Excel Auto-Fills Incorrect Formula When Adding Rows and I already tried some workarounds described there such as clear the column and then add the formula back again, but without results.
My situation seems to be different because the formula is not present in the entire column because of the "1" in A4.
Even though the problem can be manually addressed with little effort via the fill handle, this is not a suitable solution since users may not correct the formula, which is needed for some other calculations in another sheet of the Workbook.
The behaviour you describe is normal and nothing to do with tables. If you insert a row in between a cell and another cell it references directly in a formula, that formula will not adjust to refer to the new cell. In other words if A6 contains =A5 and you insert a row between rows 5 and 6, that =A5 will not suddenly alter to be =A6.
If you need that behaviour, you would need to use something like INDEX or INDIRECT - for example:
=INDEX(A:A,ROW()-1)
will always refer to the cell in the row above the formula cell.

Excel - Double press of the Fill Handle is filling to the end of column, not the end of adjacent dataset

Summary:
I am currently trying to fill a formula to a whole column in Excel to the end of an adjacent dataset (A2:A14500). Instead of the usual behaviour where Excel will flash-fill it to the end of the dataset in the adjacent column B2:B14500 for example, it instead flash fills it to the whole column (B2:B1048576), even past the end of the dataset in column A.
Use Case:
Column A is just a =FILTER command to pull from another sheet, excluding the blanks (in my live sheet, cell A1 is =FILTER('Raw'!A:A,'Raw'!A:A<>""). All other columns contain headers in cell 1:1. Cell B2 contains a basic 2 part =IF statement with an error catchall: =IF(ISERROR(VLOOKUP(A2,'Lookup'!A:B,2,FALSE)),"No",IF(VLOOKUP(A2,'Lookup'!A:B,2,FALSE)="Error","Yes","No"))
In my current example, the =FILTER in column A produces 14500 rows of data. I am attempting to use the fill handle to populate B3:B14500 with the =IF statement currently in cell B2. When I double press the fill handle, it fills the whole column of B3:B1048576, which causes Excel to crash (I am trying to do the same for 32 columns (B2:AG14500), so it is flash-filling over 33 million formulas instead of 600K).
Expected result when double clicking flash handle in formula column:
Data
Formula
1
Yes
2
Yes
3
No
4
Yes
Actual current result when double clicking flash handle in formula column:
Data
Formula
1
Yes
2
Yes
3
No
4
Yes
No
No
No
No
No
Final Comment:
Usually, when double pressing the fill handle on a formula in column B, I'd expect it to match the end of the dataset in column A (If column A contains data in cells A2:A100, I'd expect double pressing a formula in cell B2 to copy that formula to B2:B100, not B2:B1048576).
I know that copy-pasting is an option, as is manually dragging the fill handle down, but this involves me using weird Ctrl/Shift/Arrowkey combos to select the right range (this is supposed to be user friendly by others who don't use Excel much), or a long time holding the fill handle as there's 14K rows of data. Additionally, I am aware of Home>Fill>Down and Data>Flash Fill but neither of these work for me (Fill Down replaces the formula in B2 with the header in B1 and only copies down to B2, and Flash Fill tells me it can't see a pattern, despite providing it 1/2/5/10 cells of example data).
Any assistance with understanding why the double-click fill handle isn't working (I suspect it's due to the way it handles the =FILTER in column A), or how to overcome this would be appreciated. Currently using MSO365. Thanks!
PS. The reason I haven't filled the formulas manually is the number of cells in column A will change for each report generated with this spreadsheet, so the number of cells it needs to populate will vary.
=IF(ISERROR(VLOOKUP(A2#,'Lookup'!A:B,2,FALSE)),"No",IF(VLOOKUP(A2#,'Lookup'!A:B,2,FALSE)="Error","Yes","No"))
Adding # to the cell range that results in the spill will reference the whole spill. In case of lookup it'll spill the result of the lookups from the first to the last of the cells from the spill range.
PS since you own Office 365 you might want to look into XLOOKUP instead of VLOOKUP. XLOOKUP already has an embedded value if not found.

Is there a formula or method of automatically Multiplying Data on a particular sheet and across diffterent sheets

I am creating a report document which will automatically calculate the total marks earned by a student in a classroom. The values to be multiplied are in cell B22 and E22 of Sheet1, Sheet2, Sheet3 and to display the result in Sheet4. Sheet1, Sheet2 and Sheet 3 have the same formatting and design. What is the method or Formula that can be used to multiply;
B22*E22 in Sheet1 which will display the result in sheet4,
and when i use the auto fill handle on the cell containing the result in sheet4, excel should follow that pattern to multiply the same cells across the various sheets automatically;
let's say
B22*E22 in Sheet2
B22*E22 in Sheet3
and so on...
I have tried calculating it manually;
Let's say on Sheet4 in Cell A1, i used
='Sheet1'!B22*'Sheet1'!E22
in Cell A2, ='Sheet2'!B22*'Sheet2'!E22
in Cell A3, ='Sheet3'!B22*'Sheet3'!E22
and it worked. But the problem here is that, this work is purely manual and time consuming since i have a lot of sheets (about 62 sheets) and a lot of other cell calculations to make similar to that above (in other workbooks).
When i input the formula ='Sheet1'!B22*'Sheet1'!E22 in cell A1,
i tried using the auto fill handle to drag and fill the cells A2 and A3.
But it didn't work as excel keeps changing the cell references.
This is the formula i used which works on Sheet4
in A1 ='Sheet1'!B22*'Sheet1'!E22
When i use the auto fill handle, to fill the formula and have the values in
A2, this is what it gives ='Sheet1'!B23*'Sheet1'!E23
A3, this gives ='Sheet1'!B24*'Sheet1'!E24
I want a formula or method through which when i use the field handle, excel should Maintain the Cells reference for calculation (Multiplication), that is,
B22 and E22
and rather change the sheets instead. That is,
On Sheet4, in cell A1: ='Sheet1'!B22*'Sheet1'!E22
A2: ='Sheet2'!B22*'Sheet2'!E22
A3: ='Sheet3'!B22*'Sheet3'!E22.
The formulae above is the result i expect to have.
But the actual output is
On Sheet4, in cell
A1: ='Sheet1'!B22*'Sheet1'!E22
A2: ='Sheet1'!B23*'Sheet1'!E23
A3: ='Sheet1'!B24*'Sheet1'!E24.
Any help/clue will be highly appreciated!
Thanks in advance.!
=INDIRECT("'Sheet"&ROW(A1)&"'!B22")*INDIRECT("'Sheet"&ROW(A1)&"'!E22")
Place the above formula in an empty cell then copy down.
The INDIRECT function converts text with references to an excel reference. Something to be aware of though is that INDIRECT is a volatile function. This is not the end of the world. A volatile function simply means it recalculates anytime anything in the workbook changes. A regular function will only recalculate when something that affects it changes. In other words, if you have a workbook full of volatile functions you may notice some performance issues with the workbook due to all the calculation happening anytime a cell was changed
Update
Apparently I should listen to my own write up. INDIRECT converts references. Apparently it will not work with the math operator. So use indirect for each reference and keep your math operators out of the indirect.
The above image shows a copy and paste of the above formula. IF the sheet does not exist it will toss an error as it does not have an address to find.
So lots of options shown working, B22 left blank to prove calculation works.
Used multiply instead of add as that was what you had, also changed "" to 0 to get the correct functionality.

Formula to Search for Data in One Column, then Apply Formula

I am working on a personal budget sheet in excel, and it's formatted based on my pay dates, to provide more drilled-down information. I have attached an example of it below for reference.
I would like to put a formula into J2, J3, and J4 which will take the data in cells C9:C26 and H9:H16, match it to the date in cells D2:D4, then subtract the expenses in D9:D26 and I9:I16 from E2, E3, and E4.
As you can see, I have just individually summed the cells; however, I would like a formula to be able to adjust as I change the value in cells C9:C26 and H9:H16.
I have found that I can do it with ONE cell, but not multiple or a range. This is the formula I used, and I cannot find a way to make it apply to the entire range of cells: =IF(C14=D3,E3-D14)
I've also tried: =IF(C9:C25=D3,E3-D9:D25) -- I know this formula doesn't work and why. I cannot figure out how to get column C to correspond with column D.
The Budget Sheet
You just need to use SumIf().
In cell J2, put this formula: =SumIf($C$9:$C$25,$I2,$D$9:$D$25)+SumIf($H$9:$H$25,$I2,$I$9:$I$25) and drag down the three cells.
With that, you can add E2-[formula] to subtract all that from E2. Or of course, just do e2-J2 instead. I think that should do what you're looking for. If it's not quite it, let me know and I can tweak.
If you plan to have more than 1 criteria go with SUMIFS
Yes, with S

Expanding an Excel formula without referencing the previous cell

I am attempting to use an IF statement to check whether the sum of two cells from another Excel sheet is greater than or equal to 1.
For a sheet called Test1 with the values of interest in column C, this is what I have so far, which works fine:
=IF((Test1!C1+Test1!C2>=1),1,0)
In column B on a second sheet that I'll call Test2, I want to copy this formula down 200,000 rows. However, if the aforementioned formula is in cell B1, for the formula in B2 I would like the formula to read:
=IF((Test1!C3+Test1!C4>=1),1,0)
I want to copy the formula down the column so that the second cell reference in the formula in the first row does not become the first cell reference in the formula in the second row (eg. it would go C1+C2, then C3+C4, C5+C6, etc.).
I have tried manually entering the formula for a few rows, highlighting those, and copying them down but can't get the desired cell reference pattern. If I highlight and drag these first three formulae down another three rows, C4 and C5 are repeated and not in the correct pair.
=IF((Test1!C1+Test1!C2>=1),1,0)
=IF((Test1!C3+Test1!C4>=1),1,0)
=IF((Test1!C5+Test1!C6>=1),1,0)
=IF((Test1!C4+Test1!C5>=1),1,0)
=IF((Test1!C6+Test1!C7>=1),1,0)
=IF((Test1!C8+Test1!C9>=1),1,0)
I have tried using OFFSET() within this formula but couldn't get it to work. I am basically just wanting to add 1 to each of the cell references in the formula, as compared to the previous row (but not to actually add 1 to the value of that cell, as would happen with C1+1 for example).
Any insight would be greatly appreciated!
If you plan on copying this down 200K rows then you will want the absolute simplest formula that accomplishes the stagger. Avoid the volatile OFFSET function or be prepared to spend a lot of time waiting for random calculation cycles to complete. A volatile function will recalculate whenever anything in the workbook changes; not just when something changes that involved the formula in the cell.
=--(SUM(INDEX(Test1!C:C, (ROW(1:1)-1)*2+1), INDEX(Test1!C:C, (ROW(1:1)-1)*2+2))>=1)
The following formula should do the trick:
=(SUM(INDIRECT("C"&ROW()*2-1);INDIRECT("C"&ROW()*2))>=1)*1
And that's the version using IF:
=IF(SUM(INDIRECT("C"&ZEILE()*2-1);INDIRECT("C"&ROW()*2))>=1;1;0)
You say I am basically just wanting to add '1' to each of the cell references in the formula but appear to be incrementing by 2, so I am confused but an option might be to apply you existing formula to 400,000 rows, together with =ISODD(ROW()) in another column, then filter on that other column to select and delete those showing FALSE.
Excel's autofill won't do the 2-cell shift that you're looking for. You can use the functionality that is there.
Put =IF((Test1!C1+Test1!C2>=1),1,0) in the top cell and drag a copy to the second row (it will be =IF((Test1!C2+Test1!C3>=1),1,0) but that's okay). Now, put 'A' and 'B' in the next column. Select all 4 cells and copy them down 400k rows.
Use filter to delete rows flagged with 'B' and delete the blank rows.
(Select blank rows with [F5] click Special and select Blanks, then right-click and delete)
Here is all you need. It's fast and nonvolatile.
=--(SUM(INDEX(Test1!C:C,ROW(1:1)*2-2):INDEX(Test1!C:C,ROW(2:2)*2-2))>=1)
Copy it down as far as you like.

Resources