Using VLOOKUP or INDEX/MATCH to run a Lottery - excel

I'm using Excel to run a sort of lottery.
The spreadsheet columns are set up thus:
COL1:Person Name; COL2: Chosen Number A; COL3: Chosen Number B; COL4: Chosen Number C
There is then a set of data, generated using RAND() and ROUND, that gives 3 winning numbers, each between 0 and 10.
What I'm trying to do is identify a winner, by using VLOOKUP or INDEX/MATCH, or some combination, or other function, to identify the winning person, so that there is a single cell that returns the name of the winner.
The added complexity is that by looking up each of the numbers individually by column, an individual selection of, say, 1,4,8, isn't a winning selection against a randomly selection of say, 4,8,1.
Ideas?

You can concatenate numbers to additionalcolumn so it will contain string "1,4,8," and then perform a VLOOKUP for concatenated in the same way winning numbers.
By the way, this solution will show only first person, but isn't it possible that several persons guessed same numbers and won?

If you want to generate a 3-digit number, by far the easiest thing to do is to use the formula
=RANDBETWEEN(0,999)
You can select the cells and then enter (via the format dialog accessible by right-clicking) the custom format 000 if you want it to display as 3 digits so that e.g. 7 displays as 007. This will allow you to directly use VLOOKUP on a single value. #kipar asks an excellent question about potential multiple winners.

I implemented the abovementioned solution and it was quite easy. After your 4 columns, you add one column with
=TEXT(B1;0)&TEXT(C1;0)&TEXT(D1;0)
which combines the number to one string. Then you put your winning number in a cell of preference mine was M28 and the value was 123. After your first five columns you use the following formula.
=IF.ERROR(INDEX(A$1:A$4;SMALL(IF(E$1:E$4=TEXT($M$28;0);ROW(E$1:E$4));RI-OW());1);"")
The IF.ERROR is used to put a blank when there are no multiple winners. The index is used to get the winners out of your first column, that'w why there's a one at the end. The small is used to find the first occurence of the winner. You also have to enter it as an array formula so press ctrl+shift+enter instead of just enter when the formula is completed. I hope this answer is satisfying.
PS. For extra information on the use of this function go here: http://chandoo.org/wp/2014/12/09/multiple-occurrences-lookup-and-extraction/

Related

Formula to find n numbers occuring together in a cell

I would like to ask whether it is possible to create formula in Excel that extracts n first numbers in certain cell but only if these numbers are next to each other (in a group).
For instance for RegExp we can write \d{8} which in string:
abc1234_123456789012abc_87654321000_abc
finds groups of every eight numbers occuring together no matter how many times they occur:
abc1234_123456789012abc_87654321_000abc
I would like to achieve similar effect (it may be even the first occurrence, not every) without using VBA (RegExp) and to have ability to easily change number of digits taken into account, i.e. from another cell, not by expanding the formula with additional functions.
Thank you in advance.
With Microsoft365, you could try:
For all overlapping values in C1:
=LET(A,MID(A1,SEQUENCE(LEN(A1)),B1),B,IF(ISNUMBER(--A),A,""),FILTER(B,B<>"",""))
For all non-overlapping sequences in D1:
=LET(A,B1,B,MID(A1,SEQUENCE(LEN(A1)),1),C,FILTERXML(SUBSTITUTE(TRIM(CONCAT("<t><s>'",IF(ISNUMBER(--B),B," "),"</s></t>"))," ","</s><s>'"),"//s"),D,FILTERXML("<t><s>"&TEXTJOIN("</s><s>",,MID(C,SEQUENCE(1,LEN(A1),2,A),A))&"</s></t>","//s[string-length()="&A&"]"),TEXT(D,REPT(0,A)))
The 2nd option got quite long since I needed to find a way to prevend false positives when I checked if preceding tokens were numeric etc. But you'd only now need to change the value in B1 to whichever number you'd like to find the non-overlapping values. For example:
To simply get the first occurence of any 8 digits, try:
=IFERROR(MID(A1,MATCH(1,INDEX((ISNUMBER(--MID(A1,ROW(A$1:INDEX(A:A,LEN(A1))),B$1)))*(LEN(MID(A1,ROW(A$1:INDEX(A:A,LEN(A1))),B$1))=B$1),),0),B$1),"Not Found")

Using the SMALL function in excel, issues with IF function

I am trying to obtain the highest and the lowest values within a data set and record each instance. I have used the following spreadsheet to do this.
I want to obtain the lowest occurrence and if there are one or more values that are the same it will list those values too. However, when I use what I have I cant get the numbers to stop listing.
I am Assuming this is because The numbers are not the same exact values to the number of decimal places the original data comes from. e.g. the lowest value is 21.5 but the raw data gives it to 21.498 etc.
I tried using the Round function but it merely changes the formatting of the cell.
Has anyone found a way around this?
*Edit*****
I only want to report the lowest value, and if there are multiple occurrences of the same number (to one decimal place) then record those as well. The issue is that I don't know how to tell excel to stop the list if the values are the same to 1 decimal place. I tried the round function but it only changes the formatting of the cell not the actual number so excel thinks they are different values. I am not sure how to get this to work.
G3 is the top cell with =IFERROR(SMALL($C$3:$C$101,A3),"") in it.
=IFERROR(SMALL($C$3:$C$101,A3),"")
=IFERROR(IF(SMALL($C$3:$C$101,A4)=G3,"",SMALL($C$3:$C$101,A4)),"")
the data is;
And I need the highest and lowest data to 1 decimal place.
This should fix it:
G3:
=IFERROR(ROUND(SMALL($C$3:$C$101,A3);1),"")
From G4 onwards:
=IFERROR(IF(ROUND(SMALL($C$3:$C$101,A4);1)=G$3,"",ROUND(SMALL($C$3:$C$101,A4);1)),"")
You can use an Array function (entered with Ctrl+Shift+Enter):
=SMALL(ROUND($C$3:$C$20,1),A3)
And for the matching serial number:
=INDEX($B$3:$B$20,MATCH(SMALL($C$3:$C$20,A3),$C$3:$C$20,0))
Here are the results (formatted with 3 decimal places just to prove the values are properly rounded):

Check for one, two or three digits in a VLOOKUP

I have around 30 numbers which are either 1, 2 or 3 digits which are codes. These codes are attached in front of other numbers. I want to know what code is in front of a number, for example for the number 35467036 the first two digits matches with the code 35. So I want the output for that to be 1.5.
This is my current setup, I have a table with all the codes followed by the output in the next column. If all the codes were three digits long I could just do this =VLOOKUP((LEFT(E6,3)&"*"),D1:E3,2,FALSE) but they are unfortunately not.
I have also tried using nested IF statements but I can only go so far as 7 levels.
Will this only be possible in VBS or is there anther way?
Edit:
The code column is formatted to text. If I enter the value 3 into LEFT it does not work for two digits. Is there anyway I can make it work for 1, 2 and 3 digit codes? Also the codes do not overlap, for example, there isn't going to be 96 and 965 in the code table.
Seven nested IFs as a limit points to a very old version of Excel. You may want to consider upgrading to a version that is still supported in this millennium.
But your main problem is that the data type of your lookup value is text, because you concatenate a string with a wildcard. Whereas your Lookup Table's first column is probably made up of numbers.
In order to solve this you will need to add a Text column to your lookup table that represents the numeric value as a text value.
IF you want to use Vlookup, that helper column will need to be the first column of your lookup range.
You can use an Index/Match instead of Vlookup to be independent of the column order, as this screenshot shows:
Column H shows the formula that has been applied in column G.
Edit:
According to the screenshot you posted, you want to look up from the table in columns E to F and this table only has the short codes. Therefore, it makes no sense to add a wildcard to the lookup value. You just need to extract the value you want to use for the lookup.
If you want to lookup only two digits, then you need to extract only two digits. Adding a wildcard does nothing to remove the third digit.
In the screenshot below, a helper column feeds the LEFT() function the number of characters to extract, then uses that value in the VLookup.
=VLOOKUP(LEFT(A1,B1),$E$1:$F$5,2,FALSE)
=INDEX($G$2:$G$5,
SMALL(
IF(LEFT(A1,3)*1=$F$2:$F$5,ROW($G$2:$G$5)-1,
IF(LEFT(A1,2)*1=$F$2:$F$5,ROW($G$2:$G$5)-1,
IF(LEFT(A1,1)*1=$F$2:$F$5,ROW($G$2:$G$5)-1))),1))
=INDEX(LookupValues,Small(ArrayOfRowsFromIfStatements,FirstRow))
This is an array formula so you will need to use Ctrl+Shift+Enter while still in the formula bar.
We use If to build an array of Row numbers where the conditions match or return FALSE if not. The Small function then returns the smallest result in the array which will be the first row where conditions match.
We then index the results returning that row number (-1 deducted from rows to offset the headers).
If your numbers in column A are always 6 digits + the code length you can use this:
=VLOOKUP(--LEFT(A1,LEN(A1)-6),E2:F5,2,FALSE)

How can I take a certain characters from a cell of a different spreadsheet

I have a problem with my current code from Excel VBA.
I need to take only the last four digits from another spreadsheet, but it doesn't seem to be possible.
I'm currently using the Sheets("Sheet2").Cells(y, cOTr).Value instruction to take values of other spreadsheets and the LEFT(a,2) and RIGHT(a,2) instructions to take only specific characters of a string data.
I just can't find a way to combine those two instructions.
E.g. I want to copy only the first 4 numbers of Cells from the second column from sheet 2, which has the numbers similar to 6657-2 in it, to sheet 1.
Sorry if my explanation wasn't clear enough.
Edit: Lets's say I want to make a comparison between a table from spreadsheet 1 and a table from spreadsheet 2: the first table has values with four digits (e.g. 3333) and the second has values with 4 digits, a hyphen and another digit (e.g. 2222-3). I need to make a comparison between table 1 and 2, which are from different spreadsheets and to do that I only need the first four digits from table 2. I already have the logic for the program and it's running perfectly with some tests I did with only 4 digit numbers, I only need a way to take those first four numbers, something like using the "left(cells(1,2),2)" instruction but with cells of another spreadsheet.
You can combine text / strings by using the ampersand operator &
e.g.
=LEFT(A2,2) & RIGHT(A2,2)
Alternatively, you can also use CONCATENATE
e.g.
=CONCATENATE(LEFT(A2,2),RIGHT(A2,2),"some other string","and so on")
you can us this formula: =LEFT(Sheet2!A1,4).
Sheet2 A1 is where you have data 2222-3.

Using SUMIFS with multiple AND OR conditions

I would like to create a succinct Excel formula that SUMS a column based on a set of AND conditions, plus a set of OR conditions.
My Excel table contains the following data and I used defined names for the columns.
Quote_Value (Worksheet!$A:$A) holds an accounting value.
Days_To_Close (Worksheet!$B:$B) contains a formula that results in a number.
Salesman (Worksheet!$C:$C) contains text and is a name.
Quote_Month (Worksheet!$D:$D) contains a formula (=TEXT(Worksheet!$E:$E,"mmm-yy"))to convert a date/time number from another column into a text based month reference.
I want to SUM Quote_Value if Salesman equals JBloggs and Days_To_Close is equal to or less than 90 and Quote_Month is equal to one of the following (Oct-13, Nov-13, or Dec-13).
At the moment, I've got this to work but it includes a lot of repetition, which I don't think I need.
=SUM(SUMIFS(Quote_Value,Salesman,"=JBloggs",Days_To_Close,"<=90",Quote_Month,"=Oct-13")+SUMIFS(Quote_Value,Salesman,"=JBloggs",Days_To_Close,"<=90",Quote_Month,"=Nov-13")+SUMIFS(Quote_Value,Salesman,"=JBloggs",Days_To_Close,"<=90",Quote_Month,"=Dec-13"))
What I'd like to do is something more like the following but I can't work out the correct syntax:
=SUMIFS(Quote_Value,Salesman,"=JBloggs",Days_To_Close,"<=90",Quote_Month,OR(Quote_Month="Oct-13",Quote_Month="Nov-13",Quote_Month="Dec-13"))
That formula doesn't error, it just returns a 0 value. Yet if I manually examine the data, that's not correct. I even tried using TRIM(Quote_Month) to make sure that spaces hadn't crept into the data but the fact that my extended SUM formula works indicates that the data is OK and that it's a syntax issue. Can anybody steer me in the right direction?
You can use SUMIFS like this
=SUM(SUMIFS(Quote_Value,Salesman,"JBloggs",Days_To_Close,"<=90",Quote_Month,{"Oct-13","Nov-13","Dec-13"}))
The SUMIFS function will return an "array" of 3 values (one total each for "Oct-13", "Nov-13" and "Dec-13"), so you need SUM to sum that array and give you the final result.
Be careful with this syntax, you can only have at most two criteria within the formula with "OR" conditions...and if there are two then in one you must separate the criteria with commas, in the other with semi-colons.
If you need more you might use SUMPRODUCT with MATCH, e.g. in your case
=SUMPRODUCT(Quote_Value,(Salesman="JBloggs")*(Days_To_Close<=90)*ISNUMBER(MATCH(Quote_Month,{"Oct-13","Nov-13","Dec-13"},0)))
In that version you can add any number of "OR" criteria using ISNUMBER/MATCH
You can use DSUM, which will be more flexible. Like if you want to change the name of Salesman or the Quote Month, you need not change the formula, but only some criteria cells. Please see the link below for details...Even the criteria can be formula to copied from other sheets
http://office.microsoft.com/en-us/excel-help/dsum-function-HP010342460.aspx?CTT=1
You might consider referencing the actual date/time in the source column for Quote_Month, then you could transform your OR into a couple of ANDs, something like (assuing the date's in something I've chosen to call Quote_Date)
=SUMIFS(Quote_Value,"<=90",Quote_Date,">="&DATE(2013,11,1),Quote_Date,"<="&DATE(2013,12,31),Salesman,"=JBloggs",Days_To_Close)
(I moved the interesting conditions to the front).
This approach works here because that "OR" condition is actually specifying a date range - it might not work in other cases.
Quote_Month (Worksheet!$D:$D) contains a formula (=TEXT(Worksheet!$E:$E,"mmm-yy"))to convert a date/time number from another column into a text based month reference.
You can use OR by adding + in Sumproduct. See this
=SUMPRODUCT((Quote_Value)*(Salesman="JBloggs")*(Days_To_Close<=90)*((Quote_Month="Cond1")+(Quote_Month="Cond2")+(Quote_Month="Cond3")))
ScreenShot
Speed
SUMPRODUCT is faster than SUM arrays, i.e. having {} arrays in the SUM function. SUMIFS is 30% faster than SUMPRODUCT.
{SUM(SUMIFS({}))} vs SUMPRODUCT(SUMIFS({})) both works fine, but SUMPRODUCT feels a bit easier to write without the CTRL-SHIFT-ENTER to create the {}.
Preference
I personally prefer writing SUMPRODUCT(--(ISNUMBER(MATCH(...)))) over SUMPRODUCT(SUMIFS({})) for multiple criteria.
However, if you have a drop-down menu where you want to select specific characteristics or all, SUMPRODUCT(SUMIFS()), is the only way to go. (as for selecting "all", the value should enter in "<>" + "Whatever word you want as long as it's not part of the specific characteristics".
In order to get the formula to work place the cursor inside the formula and press ctr+shift+enter and then it will work!
With the following, it is easy to link the Cell address...
=SUM(SUMIFS(FAGLL03!$I$4:$I$1048576,FAGLL03!$A$4:$A$1048576,">="&INDIRECT("A"&ROW()),FAGLL03!$A$4:$A$1048576,"<="&INDIRECT("B"&ROW()),FAGLL03!$Q$4:$Q$1048576,E$2))
Can use address / substitute / Column functions as required to use Cell addresses in full DYNAMIC.

Resources