VBA Excel Macro to subtract cell from different sheet using VLOOKUP - vba

I'm try to write a macro that will auto update stock by taking the ammount used from one cell, subtracting it from a total in a different sheet.
Sheet view:
So in this instance - I want to subtract "kgs" which is cell range 'I17:I24' from what is "kgs available" which is held in the sheet "Malt" and updated from a VLOOKUP defined from the "Batch Code" in the Brew Sheet.
I'm guessing the code would probably have to VLOOKUP that batch code in the Malt sheet and subtract the "kgs" in the "Brew Sheet" sheet from the "kgs available" in the "Malt" sheet (Column L)
What also might contribute complications is if the cells in range E17:24, which contain the Batch Codes for the VLOOKUP have no values. Would that throw up errors, if though I may not need to use all of the cells in that range? (If that makes sense - sometimes, I may only need to use 1/2 of the 8 cells in the range)
Apologises in advance if my question is vague. My coding knowledge is very limited.
Thanks for you help.

you need to get the sum of KGs in range E17:24 where the batch code matched the batch code on the malt sheet
=sumif(Malt!$E$17:$E$24,L17,Malt!$I$17:$I$24)
Then all you need to do is subtract that from the available value in column I
=I17-sumif(Malt!$E$17:$E$24,L17,Malt!$I$17:$I$24)
Not sure if I have the columns correct.

Related

Vba email generator, subject from excel spreadsheet

I have a vba code that generates an email. I would like the subject to be the data from the first and last cells in my list. The thing is, my list isnt of a set length, sometimes it contains 5 pieces of data sometimes 8 etc. How do i tell vba to pick the first and last cell no matter the length of the list?
thanks
For me, best practice is to just have cells on your sheet that calculate the first and last row (different ways you can do that), then give those cells a range name such as FirstRow and LastRow. In your vba then you refer to these cells to make your code dynamic.
e.g:
firstRow = Range("FirstRow)
lastRow = Range("lastRow")
test = range(cells(firstRow,lastRow))
-- Note I have not written VBA in many many years so am writing the above from memory so it may be not be exact.
Of course you can do it all entirely in VBA using the xlDown method mentioned previously but I prefer the transparency of it being on the main page so that easily spot if something breaks.
Range("A1").End(xlDown).Value
Where the cell is where you want to start and the End part moves all the way to the end

SUMIFS returns 0 using dynamic criteria, with criteria range and sum range on another sheet

Anyone,
I've chatted with and called excel customer service with no luck. I used the formula builder (please see attached screenshot) to make sure each element of the formula is correct and returns the value for the criteria I'm trying to reference.
Everything is accurate, but it returns a value of 0. When I do the same thing in the actual sheet the data is stored in (and click a criteria cell within the criteria range) it returns the accurate value?! I'm not sure why it won't work on the other sheet. The values I am using to select are dynamic and change with a drop down. I have another, advanced, workbook (I did not create) that does the same thing and completes an even more complicated formula, but actually works so I'm not sure why this is returning a 0 value.
Photos and code/syntax: Dynamic Selection, Example 2 of it working, Example 1 of it working, Formula Builder, CountIFs, Advanced Spreadsheet working, VLOOKUP
=SUMIFS('GFEBS Pull'!Q:Q,'GFEBS Pull'!G:G,FMCOP!$C$20,'GFEBS Pull'!H:H,FMCOP!B23)
or:
=SUMIFS('GFEBS Pull'!Q:Q,'GFEBS Pull'!G:G,'FMCOP'!$C$20,'GFEBS Pull'!H:H,'FMCOP'!B23)
When I type ' around FMCOP sheet name, they disappear? I've also tried to lock the columns on the 'GFEBS Pull' sheet with no luck. Cell B23 is not locked because I'm going to copy the formula down to reference other cells. Any help is appreciated!
In this screenshot you can clearly see that both FMCOP!C20 ansd FMCOP!B23 have prefacing spaces; e.g. " HHC".
Since " HHC" will never match "HHC", fix the data returned from 'the lower table in the same screenshot'.
A Text-to-Columns, Fixed Width, Finish should do this. You could adjust the original formula like,
=SUMIFS('GFEBS Pull'!Q:Q, 'GFEBS Pull'!G:G, TRIM(FMCOP!$C$20), 'GFEBS Pull'!H:H, TRIM(FMCOP!B23))
I would caution against the latter 'bandaid' fix. Fix the original data; do not apply bandaids on-the-fly.

percentile formula error in VBA

I'm trying to apply a formula through VBA for a particular range. This is the code in my VBA editor:
Sheets("WBR45").Range("AE105").Formula = "=PERCENTILE.INC(TP!$A$3:$A$30:$B$3:$B$30:$C$3:$C$30:$E$3:$E$30,50%)*24"
And the below formula gets updated in the destination cell when this is run:
=PERCENTILE.INC(TP!$A$3:$A$30:$B$3:$B$30:$C$3:$C$30:$E$3:$E$30,50%)*24
But I get an error in the destination cell as #VALUE!.
And when I click on "Show Calculation steps", only this part of the formula is underlined :
TP!$A$3:$A$30:$B$3:$B$30
I have no idea what is wrong with this simple formula. Can someone please take a look
Honestly I have no clue about what you're doing with this, but this may fix it:
"=PERCENTILE.INC(TP!$A$3:$A$30:TP!$B$3:$B$30:TP!$C$3:$C$30:TP!$E$3:$E$30,50%)*24"
You appear to have three errors in your formula:
You are using : to separate ranges instead of ,
You are not specifying which sheet the second, third and fourth ranges refer to, therefore it is defaulting to the sheet on which the formula occurs (i.e. sheet "WBR45")
Multiple ranges will need to be enclosed within brackets (...) in order to be passed as a single range.
If you are trying to have your function operate on the range A3:C30 together with the range E3:E30 (i.e. A3:E30 but ignoring column D), with those ranges being on the "TP" worksheet, I believe that you need to change your formula to
Sheets("WBR45").Range("AE105").Formula = "=PERCENTILE.INC((TP!$A$3:$A$30,TP!$B$3:$B$30,TP!$C$3:$C$30,TP!$E$3:$E$30),50%)*24"
or, slightly simplified
Sheets("WBR45").Range("AE105").Formula = "=PERCENTILE.INC((TP!$A$3:$C$30,TP!$E$3:$E$30),50%)*24"

Looping a formula through numerous spreadsheets

I have the following three Excel (2010) formulas which perform exactly what I want them to:
=IF([NoData]Sheet1!C4=ISBLANK(TRUE)," ",[NoData]Sheet1!A4)
=IF([NoData]Sheet1!C4=ISBLANK(TRUE)," ",[NoData]Sheet1!C4)
=IF([NoData]Sheet1!C4=ISBLANK(TRUE)," ",[NoData]Sheet1!K4)
I want these three formulas to run through data in several worksheets contained within a given workbook. The cells that they will operate on will remain the same for each worksheet.
How can I run these three formulas through 11 or 12 different worksheets within the given workbook?
You could use VBA to achieve this, but given the state of your original formulae I wouldn't suggest taking that route.
Use the indirect function to vary the name of the sheet that your are putting into your formula:
=IF(ISBLANK(INDIRECT(CONCATENATE("[NoData]Sheet",A1,"!A1")))," ",INDIRECT(CONCATENATE("[NoData]Sheet",A1,"!K4")))
In cell A1 on the sheet for the formula above, put the sheet number that you want the formula to target.
I will reiterate though, ISBLANK(TRUE) always resolves to FALSE, so in your formula you are checking if the value in C4 is FALSE, not if it is blank.

Excel VBA loop through columns in range and copy columns

I am new to Excel VBA and need some help writing a macro.
From Worksheet1 of Workbook1 I need to copy each column of range D1:Z100, one at a time, and pasteValue it to cells B1:B100.
This triggers a calculation in Worksheet2 of Workbook1. Here I need to copy cells A1:B200 into a new workbook.
This new workbook is to be renamed with the text string in Worksheet1, cell B1. The new workbook is to be saved into the same folder as Workbook1.
The loop is then to continue with the next column from Worksheet1, and continue until all columns in the range have been treated this way.
I have used two days searching the net to find an answer without any luck....
Start the way most of us did:
Record new macro.
Manually perform all the steps you want to automate.
Stop recording.
Go to the Developer tab and inspect the auto-generated code.
Write your own VBA based on what you learn in Step 4.
You will have to figure out a few things on your own, or come back here and ask specific questions.