Code a huge formula into VBA combining IF and Vlookup - excel

I need to reduce a file I am developing and I know one good way is using VBA. Unfortunately I am not advanced in VBA yet and I am struggling in designing this.
I have a list price organized in three different streams, and I want to combine in one like below:
Stream Site Brand Code Price
Mainstream Boston Brand 01 Formula
Midstream New York Brand 02
Midstream Los Angeles Brand 02
Currently I am using a formula that basically does the following:
=IF(AND(stream="mainstream",Site = "Boston"),vlookup(Brandcode,list 1, 2,0),IF(ANd(stream="midstream", Site = "Boston", vlookup(Brandcode,list 2, 2,0),...))
The formula actually works just fine, the problem is I am testing many other conditions than just this one and thus the file is becoming very heavy, so I wanted to create a VBA code to create either a function or a subroutine, but I am struggling to understand how to do it.
Thanks

This can actually be achieved without using VBA by using named ranges instead. Looking at the formula, it's clear that your lookup range varies based on the combination of stream and site.
You can create a Named Range for each of these lookup ranges. To do so, highlight the range of cells that contains the first lookup group (let's say Midstream New York). Next, press CTRL + F3 to open up the Name Manager. Finally, give this group the name MidstreamNewYork. (Note: you cannot include spaces in the name).
Next, you can update your Vlookup function. You no longer have to include the IF(AND... component because your lookups will be dynamic. Let's say you're inputting a formula on Row 2, the formula would be:
=VLOOKUP(C2,INDIRECT(SUBSTITUTE(A2&B2, " ", "")),2,FALSE)
Let's break down the formula. (1) C2 is just the brandcode (I assumed it was in column C.(2) TheINDIRECTfunction treats aStringas aRange. We are passing in CellsA2andB2, which are "Midstream" and "New York", respectively. We use theSUBSTITUTEfunction to remove the spaces since they aren't allowed. So now, we are looking up a named range calledMidstreamNewYork` (sound familiar?). (3) The rest of the VLOOKUP is standard: lookup the second column, and only match exacts.
Give it a try and let me know if it meets your requirements.

Related

Using excel 2013 extract the country extension from a large series urls

I have a large list of urls that i need to break down by a variety of factors. One factor is if it is for a country other than the US. I have complied a list of all the extensions I'm searching for within the workbook (i.e., .az,.mx,.nl, etc.).
Due to the variety in the urls, simple extraction of TLD won't work on all. For example:
b2 http://www.domainname.in
b3 http://www.domainname.co.in
b4 http://www.domainname.in/un.htm
b5 https://www.domainname.in/wp.content/_input_3_.txt I
I was using RIGHT(b2,LEN(b2)-FIND("*",SUBSTITUTE(b2,".","*",LEN(b2)-LEN(SUBSTITUTE(b2,".","")))))) to extract the TLD for those in the b2 example. However, it will not work for those with other endings. I was considering searching for the third "/" in example b4, however longer urls (like b5) prevent that.
Is there a way to identify if there is a country extension present? Furthermore, would be be possible to have the column next to it list what the country extension is, if one exists.
I currently have no working knowledge of Macros or VBA,
Ok, this is gonna be crazy. So we know that the country code is attached to the domain name, and is usually the last two letters. The first thing I would do is separate out the domain name. The formula would look something like this:
(I put all the URL's in column A so you'd have to fix the column reference)
=TRIM(MID(SUBSTITUTE(SUBSTITUTE($A1,"//","/"),"/",REPT(" ",999)),COLUMNS($A:B)*999-998,999))
What this does is replace the double // with a single / it then uses it as a delimiter and get's the second column which is the domain name.
Next we need to find the country code of the domain which you've already done but I'll use a slightly different formula. If you had just the domain name in column A by itself you could extract it like this.
=TRIM(RIGHT(SUBSTITUTE($A1,".",REPT(" ",LEN($A1))),LEN($A1)))
Now that we have those two formulas we can replace the $A1 reference of the second formula with the first formula so that we aren't using a bunch of cells to extract the code. The combined formula would look like this:
=TRIM(RIGHT(SUBSTITUTE(TRIM(MID(SUBSTITUTE(SUBSTITUTE($A1,"//","/"),"/",REPT(" ",999)),COLUMNS($A:B)*999-998,999)),".",REPT(" ",LEN(TRIM(MID(SUBSTITUTE(SUBSTITUTE($A1,"//","/"),"/",REPT(" ",999)),COLUMNS($A:B)*999-998,999))))),LEN(TRIM(MID(SUBSTITUTE(SUBSTITUTE($A1,"//","/"),"/",REPT(" ",999)),COLUMNS($A:B)*999-998,999)))))
Hope that helps

Use INDEX/MATCH to select formula written as string, and enable it?

I have a pricelist, with currently 5 different categories of products. Each product will have to have two different prices. Depedning of the product and the type of price, the calculation will be different. Therefor I've used INDEX/MATCH to find the formula needed, from a table I created.
Below a screendump, and I wanted to attach the Excel fil, but canøt seem to work out how.
Question: HOW do I then "run" the formula I fetched? -I've tried different suggestions on using EVALUATION, but it doesn't seem to cut it? Also I've tried "Indirect' on the whole formula, without success.
I would like to avoid any VBA for this case.
Can anybody provide some insight?
You could but if I understand properly, the only thing changing in the formulas is the "muliplier" number, then it's better to lookup that number instead of the whole formula. The other method (which would use Evaluate etc) is not be considered "good practice" for a number of reasons.
EDIT:
I didn't see the 2nd varying value (since I was on the SO mobile app) but it's still not an issue since it would a target column. You could be thinking of the opposite: sometimes lookups based on multiple criteria can get complicated, but this a matter of more data, as opposed to adding criteria for the lookup.
VLookup would have been the simplest method, like G2 could have been:
=VLOOKUP(E2, $J$4:$L$8, 2, False)
...to return the second column of range J4:L8 where the first column equals E2. (Then for the next required column, same formula except with 3 instead of 2.)
Since I wasn't sure more columns could be added one day, I allowed for that by, instead of specifying "Column 2 or 3" etc, it finds the column dynamically by name. (So the multiplier/factor used in G2 will change if you change the title in G1 to the name of a different column existing in the target data chart.
For the sake of neatness as well as potential of additional columns like G & H, I moved the lookup table to a separate sheet. It can stay out of the way since you won't need to see or change it very often. (If the same chart was going to be referenced by many workbooks, you could even move it to a separate workbook and point all formulas at that, since it's always best to have one copy of identical data instead of many in different workbooks.
Also to assist with potential future changes (and just to be tidier), instead of referring to the target table range addresses (like "J4:L8" etc) I named two ranges:
the table of multiplier/factor data can be referred to by it's address, or by myMultipliers
the titles of the same table is also called myMultiplierTitles (used to match to the titles of column G & H on the original sheet.
Formula
After those changes, the lookup formula in G2 is:
=INDIRECT(VLOOKUP($E2,myMultipliers,MATCH(G$1,myMultiplierTitles,0),FALSE)&ROW())*VLOOKUP($E2,myMultipliers,MATCH(G$1,myMultiplierTitles,0)+1,FALSE)
INDIRECT returns the value of a cell that you refer to by name (text/string) as opposed to directly (as a range). For example:
=INDIRECT("A1")
returns the same as
=A1
...but with INDIRECT we can get the name from elsewhere (a cell, function or formula). So if x="A1" then =INDIRECT(x) returns the same as the 2 above examples.
Your original plan of storing the entire formula in a table as text would have worked with the help of INDIRECT and/or EVALUATE but I think this way is considered better practice partly because it facilitates easier future expansion.
The formula is longer than it would have been, but that's mostly because it's dynamically reading the field names. And size doesn't matter. :-)

Run a text formula in an adjacent cell

I'm having trouble trying to figure out a way to have two columns, one to show the entire formula that can be editable and the second to actually perform the formula.
Ideally, I would like my sheet to be set up like this:
Formula | Value
=5+2 | 7
=3-2 | 1
I would like to be able to change the Formula column and have it automatically update the Value column.
I've tried using the GetFormula() function but I don't think that's what I want to do as I would end up in a circular reference based on what I want to do. The closest I've got is using a Right() function and Text in the formula column or a space and removing the space. However, I end up with the text and not the solved formula instead.
Using =RIGHT(A2, LEN(A2)-1)
Formula | Value
=5+2 | =5+2
I have also tried using =RIGHT(A2,LEN(A2)-1) but without the "=" and can't figure out how to convert the "5+2" into text that I can use to solve. I'm hoping to do this with a formula and without a macro/VBA.
Here is how to do it.
1
Open the Name Manager. Control-F3 from the worksheet, and then click the New button.
2
For the Name field in the dialog, enter EVALA. I just picked this name; it stands for "Evaluate A". But you can pick whatever name you like.
3
For the Refers to field, enter this
=EVALUATE($A1)
4
Click OK and then Close.
5
In B1 enter this formula:
=EVALA
That's it.
You can now use this formula on any row in the worksheet and it will evaluate whatever is in the column A cell of the row where you enter the formula.
You can make a user defined function easily enough with VBA, but if you don't regularly use VBA then an alternative method is to create a Name object. Name objects can access certain functions not typically available in a cell's formula. One of these functions is "Evaluate" which will evaluate a string as a mathematical expression. Here's a demonstration how to do this.
NOTE: Pay special attention to the use of $. Chances are you don't want any $ in your name definition since that will prevent it from behaving in a relative manner. Also, Sheet1! means that this will not work on another sheet.
Update I just want to give credit for this method to the following sources. This is pretty neat stuff, so for anyone interested give it a read. The last link in particular gives a neat example of creating a chart with no data points.
MSDN Evaluating Defined Names
The power of evaluate (ozgrid)
XL4 Macro Functions in Names - JKP
Evaluate and Indirect
More unique functionality of Defined Names
This is probably the best solution for the OP, since it asks to avoid using VBA; however, this method is somewhat limited. It requires manual set-up on every sheet to be used. Much preferable I think is to create a very simple UDF like this...
Function Eval(Expr As String)
Eval = Application.Evaluate(Expr)
End Function
This can be added to any accessible add-in, making it available to any instance of Excel. A little more set-up, but less maintenance.
Just put a single ' before the formula in your A column:
'=5+2 will show in your cell as "=5+2". Then in the B column, just do the formula as normal, =5+2.

(excel) sumproduct multiplying with another sheet

i have a little problem with final formulas in one of my column. How to start. maybe i will explain what i have a then what i want.
i have an excel worksheet with 3 sheets. i want to record goods and what are these goods made of. first is sheet called Goods where is possible to put number of goods i want to make. In this case i want to make 1x sandwich1 and at the same time 3x sandwich2. i dont want make sandwich3 this time.
Second sheet is Matrix sheet where I record every good and what it is made of. This sheet is basic sheet and all other sheets take list of goods (resp. ingredients) from this sheet. Simply when i want to make sandwich1 i look at matrix and know that i need 1x1pc of egg + 1x5g of cheese. And for 3x sandwiche2 i need 3x10g of sausages.
Final sheet is called Ingredients. It is a list of used ingredients from Matrix sheet (exactly same order) to make these sandwiches. I want to fill formulas into column B which would go through one ingredient ofter ingredient and count needed amount of it. So it would look into matrix in the same row and where there is some number it would multiply with number of items from Goods sheet. The list of goods is also in the same order as in the matrix sheet.
I hope you understand now what i want and will try to help me. I think there will be SUMPRODUCT, SUMIF and maybe INDERECT functions but i am not that skilled in excel
thanks for any suggestions
You can use MMULT function here - it's an "array formula" which you need to enter in a range. You can do that like this:
In Ingredients worksheet enter this formula in B2
=MMULT(Matrix!C2:E4+0;Goods!B2:B4+0)
[I'm assuming you have a European version of Excel where ; is used to separate arguments]
Now select the whole range B2:B4, press F2 key to select formula and hold down CTRL and SHIFT keys and press ENTER. This "array enters" the formula in the range and you should now see curly braces like { and } around the formula and also the correct results.
You cannot change part of that array now, only the whole thing
Note that I'm assuming that the contents of Goods!A2:A4 will be the same as Matrix!C1:E1 and in the same order. You can extend the ranges to be as large as you like as long as that principle still holds
I suspect that this is an issue of "when all you have is a hammer, every problem is a nail". For reasons known only to you you are using a spreadsheet to solve a problem that databases were made to do. Any solution to this problem in a spreadsheet will be entirely dependent on the integrity of your data - add another column or get things out of order and it will fail.
That said, what you have in your link is effectively a pivot table and what you need is the unpivoted version of this - the instructions for getting this are here.
When you have that, you can use the various database functions in excel to get your answer.

EXCEL: Is there a way to call and replicate particular data from a different worksheet dependent on the result of a different formula?

I've got a weather data workbook but with all the background processes on worksheets that are hidden. It currently only supports 4 cities - NYC for example - each city having it's own worksheet and I need to extend it. When the location is chosen (through a drop down menu), the data is called from the respective sheet and goes on from there. I currently use this formula to look up that cities data:
=IF('Choose Variables'!$D$10="NYC - New York",NYC!G9,
IF('Choose Variables'!$D$10="Santiago",Santiago!G9,
IF('Choose Variables'!$D$10="London",London!G9,
IF('Choose Variables'!$D$10="Rio de Janiero",'Rio'!G9,
"Data not available"))))
Then drag this formula down ~4800 and across 3 cells to get the various data needed.
This works fine, but as you can imagine, adding ~50 more cities is going to be arduous to say the least. I've had a google and =Vlookup, =lookup etc won't help me.
TLDR; Is there a way to call and replicate particular data from a different worksheet dependent on the result of a different formula? This will be to pass on quite quickly and I don't fancy learning Macros just for this, but any other suggestions would be great.
Thanks in advance.
Your NYC is problematic because the text "NYC - New York" doesn't match the worksheet name NYC (unlike your other examples) but if you can make all those match you can use INDIRECT function to convert a text value in a cell to a valid reference, e.g.
=IFERROR(INDIRECT("'"&'Choose Variables'!$D$10&"'!G9"),"Data not available")
one problem with INDIRECT is that you make the G9 value into a text value so it isn't "draggable" - if you want G9 to increment when you drag down or across change to this
=IFERROR(INDIRECT("'"&'Choose Variables'!$D$10&"'!"&CELL("address",G9)),"Data not available")
If you can't make the text in D10 always exactly match the sheet name (or you don't want to) then you can always make a two column table (name it table) with the cell text in the first column, e.g. NYC - New York and the sheet name in the next, e.g. NYC then you can use VLOOKUP like this
=IFERROR(INDIRECT("'"&VLOOKUP('Choose Variables'!$D$10,table,2,0)&"'!"&CELL("address",G9)),"Data not available")

Resources