Swapping street names - excel

I have a list of street intersections in excel. Of course it reads S 74th St / Rogers Ave as being different from Rogers Ave / S 74th St. I am trying to swap the cells on the columns so that intersections like that all end up looking the same. I have broken them down into two columns and having been trying the iferror/index/match functions but obviously not doing it right. If there is a macro I could write, that would be ideal. Any ideas?

Assuming your data always appears in a single Cell, in the format "[Street 1] / [Street 2]", this can be done with some helper columns.
First in column B, use the following formula, which will pull out the left name from the intersection:
=LEFT(A1,SEARCH(" / ",A1)-1)
Then do a similar thing in column C:
=RIGHT(A1,LEN(A1)-SEARCH(" / ",A1)-2)
Then, in column D, you will create a new text string showing the intersection, sorted [sort of] alphabetically by the first 4 characters of each road. You can do this as follows:
First, consider the below formula, which picks up the ASCII character value of the first 4 characters of the word found in B1:
=SUM(CODE(MID(LOWER(B1),{1,2,3,4},1)))
This creates a single number which equals the sum of the specific code for each character. We can use that to sort the priority of one cell over another, by comparing with the sum of the same formula for the cell in C1, like so:
=SUM(CODE(MID(LOWER(B1),{1,2,3,4},1)))>SUM(CODE(MID(LOWER(C1),{1,2,3,4},1)))
This will show TRUE if the sum of those codes in B1 is bigger than the sum of those codes in C1. Put this formula in D1 and copy down.
Finally, recreate your ordered string as follows, in column E:
=IF(D1,B1&" / "&C1,C1&" / "&B1)
Now this can be used as a column of ordered data, which should eliminate matches in the streets [assuming no streets have the same 4 characters as any other, and no duplicate streets start differently - ie 5 Ave vs 5th Ave].

Related

How to create a dynamic formula to find the average of a set of values for a given vector

I am trying to create a formula that gives me the average of the last 12 entries in a given dataset depending on the associated vector.
Let's make an example:
I have in column F2,G2,H2 and I2 dates, Company1, Company2 and Company3 respectively. Then from row3 to row 33 I have months dates starting from May 2016.
Date Company1 Company2 Company3
May-16 2,453,845
Jun-16 13,099,823
Jul-16 14,159,037
Aug-16 38,589,050 8,866,101
Sep-16 63,290,285 13,242,522
Oct-16 94,005,364 14,841,793
Nov-16 123,774,792 7,903,600 41,489,883
Dec-16 93,355,037 12,449,604 69,117,105
Jan-17 47,869,982 13,830,712 83,913,764
Feb-17 77,109,905 10,361,555 68,176,643
The goal is to create a formula that, when I drag it down, correctly calculates the average of the last 12 values for a given company.
So for example i would have, say in table "B2:C5":
Company1 76,856,345
Company2 11,120,859
Company3 65,674,349
And, if a new Company4 is added to the list, then I just have to drag it down the formula, to calculate the average of the last 12 months for Company4.
Until now, I have came up with this formula:
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(G:G),ROW(G:G)),ROW(INDIRECT("1:"&MIN(12,COUNT(G:G))))),ROW(G:G),G:G ))
This formula correctly calculates the average of a given column, considering only the last 12 values. The last step would be to come up with a formula that includes all the columns and then calculates the average for the given company.
Thanks!
I recommend that you use a named range to define your data in columns G:I. When a company is added, just modify the named range's specs. I used the name Target. Of course, you can replace it with $G:$I if you feel so inclined but I would rather recommend reducing the number of rows in the range, which is easier to manage when it is named.
Use the formula below to extract the company names from the first row of Target into the first column of your averages table. This is to ensure that the names are spelled identically in both locations.
=INDEX(Target,1,ROW()-2)
The number 2 indicates the number of rows above the row containing the formula. it is copied here from cell M3. There, ROW()-2 creates the number 1, counting sequentially as the formula is copied down.
Now I have the formula below in my cell N3 and copied down.
=SUM(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))
The formula simply sums up the columns G, H, and I in 3 consecutive rows.
In the final step I inserted the range definition established above, meaning excluding the SUM() function, into your existing formula.
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))),ROW(INDIRECT("1:"&MIN(12,COUNT(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))

Excel Substitute a substring in cell (sheet A) by the corresponding string in "lookup" (sheet B)

How to do this in MS Excel 15.4
I want to process Column A to become Column B
Column A | Column B
----------------------------------------------------------------------
one, 1, two, 2, three, 3 | one apple, two bananas, three strawberries
one, 1, four, 4 | one apple, four oranges
.......................
... many other rows ...
.......................
two, 2, four, 4, three, 3 | two bananas, four oranges, three strawberries
The Column A can have n matching substrings in the lookup sheet.
I have another sheet (lookup table) with what to substitute the text in Column A with
Match col | Replace col
----------------------------
one, 1 | one apple
two, 2 | two bananas
three, 3 | three strawberries
four, 4 | four oranges
... and many more ...
I want to replace all the substrings found in Column A with the Replace col value of the lookup table
It looks like I may be able to combine VLOOKUP with SUBSTITUTE, but I am struggling with it
To do it in indivdual cells;
=IFERROR(VLOOKUP(TRIM(SUBSTITUTE(MID(SUBSTITUTE($A1,",",REPT(" ",999)),(COLUMN(A:A)-1)*2*999+1,2*999),REPT(" ",999),",")),Sheet2!$A:$B,2,FALSE),"")
If you have a subscription to Office 3651 excel you can use this array formula to put it all in one cell:
=TEXTJOIN(",",TRUE,IFERROR(LOOKUP(TRIM(SUBSTITUTE(MID(SUBSTITUTE($A1,",",REPT(" ",999)),(ROW(INDIRECT("1:" & INT((LEN($A1)-LEN(SUBSTITUTE($A1,",","")))/2)+1))-1)*2*999+1,2*999),REPT(" ",999),",")),Sheet2!A:A,Sheet2!B:B),""))
Being an array formula it must be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then Excel will put {} around the formula.
On caveat is that the reference data must be sorted on the lookup column:
1 If you do not have Office 365 but want to use this formula you can place the code from my answer HERE that will mimic the TEXTJOIN() in a module attached to the worksheet. Then use the formula as described above
I have a rather clunky solution, but it'll work for you if you don't mind taking perhaps a few extra steps. (No VBA required).
With your original data, highlight all of it and do Text to Columns with a comma delimiter. Set the destination to wherever you like. I chose the column just right of it (so, B2):
So now you have it all split up.
I put the VLOOKUP() table in "Sheet2":
And back on Sheet1, in I2, I used this formula:
=IFERROR(VLOOKUP(TRIM(B2)&", "&TRIM(C2),Sheet2!$A$1:$B$4,2,FALSE),"")
And drag right. You'll have some empty columns which you can hide/Delete, then copy all the data.

Excel: Obtain a column by sorting anotr one values

I need to automatically obtain a sorted column of values from another given column values, like in the sample:
I have I need A unchanged, and also B obtained from A
A A B
-----------------
1 1 0
0 0 0
3 3 1
8 8 3
0 0 8
I mean if the values from A changes, the B should change accordignly...
Is that possible in MS Excel?
Here a sandbox and sample:
http://1drv.ms/1SkqMhS
If you put The formula =SMALL(A:A,ROW()) in B1 and copy down then the cells in B will be linked to the cells in A in such a way that the numbers in B will be the numbers in A in sorted order. This won't be efficient for larger ranges but will work fine for small to medium size ranges.
If you want the numbers to start in a lower row, say B2 because you have a header in B1, adjust ROW() to something like ROW()-1.
A word of warning: Use of ROW() can make a spreadsheet somewhat fragile in that formulas that involve it can change their meaning if rows are inserted or deleted or the block containing the formula is moved to somewhere else. Rather than using ROW(), there is something to be said for adding a helper column which numbers the data in A (which would then be in e.g. B) and referring to these numbers rather than small. For example, in:
If I put the formula
=SMALL($B$2:$B$5,A2)
In C1 and copy down, it works as intended. In response to a question you raised in the comments, I added still another column which gives an index where the corresponding value occurs. To do this I wrote in D2 (then copied) the formula
=MATCH(C2,$B$2:$B$5,0)
Of course. Highlight your range and in the Data tab, click "Sort", then you can choose how you want to sort your data:
If column B has information that is to be used with Column A (like next to A1 is "Car"), and you want to sort the whole table, based on Column A, then just select Columns A and B, then sort by column A.
Found the answer, thanks to John Coleman !
Just some minor details like cell value fixing (with $, like A$2)and the -1+ROW adjustment for the 1 header row!

Ranking in Excel with multiple criteria

For example, I need to create a merit list of few student based on total marks (column C), then higher marks in math (column B) -
A B C D
-------------------------
Student1 80 220 1
Student2 88 180 3
Student3 90 180 2
Expected merit position is given in column D.
I can use RANK function but I can only do that for one column (total number). If total number of multiple student is equal, I could not find any solution of this.
You can try this one in D1
=COUNTIF($C$1:$C$99,">"&C1)+1+SUMPRODUCT(--($C$1:$C$99=C1),--($B$1:$B$99>B1))
and then copy/fill down.
let me know if this helps.
Explanation
Your first criteria sits in column C, and the second criteria sits in Column B.
Basically, first it is counting the number of entries ($C$1:$C$99) that are bigger than the entry itself ($C1). For the first one in the ranking, you will get zero, therefore you need to add 1 to each result (+1).
Until here, you will get duplicate rankings if you have the same value twice. Therefore you need to add another argument to do some extra calculations based on the second criteria:
To resolve the tie situation, you need to sumproduct two array formulas and add the result to the previous argument, the goal is to find the number of entries that are equal to this entry with $C$1:$C$99=C1 and have a bigger value in the second criteria column $B$1:$B$99>B1:
you add -- to convert TRUE and FALSE to 0s and 1s so that you can multiply them:
SUMPRODUCT(--($C$1:$C$99=C1),--($B$1:$B$99>B1))
the first array is to see how many ties you have in the first criteria. And the second array is to find the number of bigger values than the entry itself.
Note you can add as many entries as you like to your columns, but remember to update the ranges in the formula, currently it is set to 99, you can extend it to as many rows as you want.
Sometimes a helper column will provide a quick and calculation-efficient solution. Adding the math marks to the total marks as a decimal should produce a number that will rank according to your criteria. In an unused column to the right, use this formula in row 2,
=C2+B2/1000
Fill down as necessary. You can now use a conventional RANK function on this helper column like =RANK(D2, D$2:D$9) for your ranking ordinals.
Very simple (or, at least, much more simpler that the one provided by the best answer) 'math' solution: do a linear combination with weights.
Do something like
weighted_marks = 10*colC + colB
then sort weighted marks using simple rank function.
It does solve your problem, bulding the ranking you need.
If you don't like to limit the number of rows or the numbers used in the criteria, Jeeped's approach can be extended. You can use the following formulas in cells D2 to L2, assuming that there are three criteria, the first one in column A, the second one in column B, and the third one in column C:
=RANK($A2,$A:$A,1)
=RANK($B2,$B:$B,1)
=D2*2^27+E2
=RANK(F2,F:F,1)
=RANK($C2,$C:$C,1)
=G2*2^27+H2
=RANK(I2,I:I,1)
=J2*2^27-ROW()
=RANK(K2,K:K,0)
The formulas have to be copied down. The result is in column L. Ties are broken using the row number.
If you like to add a fourth criterion, you can do the following after having the formulas above in place:
Add the new criterion between columns C and D.
Insert three new columns between columns I and J.
Copy columns G:I to the new columns J:L.
Copy column G to column M, overwriting its content.
Change the formula in column L to point to the new criterion.
The factor 2^27 used in the formulas balances the precision of 53 bits available in double-precision numbers. This is enough to cover the row limit of current versions of Excel.

Generating letter by its position in the alphabet

I want a formula that can return the letter of the excel alphabet (27 = AA etc.) of a given number.
The purpose of this is that I have a table that returns values in a spreadsheet. I am summarizing data of climate measurements in cities, this data takes up 4 columns (but the same rows and relative positions in each column, so I29 and J29 contain 2 numbers I need, and then M29 and N29 contain the same data for the next location.
I want to create a summary table that looks like the below
City Rainfall Average Sunshine Average
City A =I29 =J29
City B =M29 =N29
City C etc.
my problem is that i go up to a few hundred cities, and i want to be able to populate the cells automatically/fill down. I know what row the data is in but need to generate the column letters using the formula requested above, so I can use a concatenate to create the cell reference.
You can use the ADDRESS function.
For example, =ADDRESS(29,27) will return the string "$AA$29".
However, honestly in your situation I would use the OFFSET function, combined with ROW(). To illustrate, let's say your table starts with a header row on row 50 and data starts on row 51. The data for a given city would start (ROW() - 51) * 4 columns to the right of I29. So:
A B C
50 City Rainfall Average Sunshine Average
51 City A =OFFSET($I$29,0,(ROW()-51)*4) =OFFSET($I$29,0,(ROW()-51)*4+1)
52 City B =OFFSET($I$29,0,(ROW()-51)*4) =OFFSET($I$29,0,(ROW()-51)*4+1)
53 City C etc.
Of course, you have to be careful if you want to move these cells around; you'll have to change the 51.

Resources