A function or VBA code to determine the last digit in a number - excel

In Excel 2016 I have multiple rows containing numbers no greater than 3 characters listed in column A. I am trying assign a group number based on the right most character in each row that are in column A. Once the number is determined the result is placed in column B of the corresponding row.
Example:
If rightmost character of the number is: a 1 or 6 in cell A1, then in column B1 it's given a 1.
If A1 is a 2 or 7 then B1 is 2.
If A1 is a 3 or 8 then B1 is 3.
If A1 is a 4 or 9 then B1 is 4.
If A1 is a 0 or 5 then B1 is 5.
Any suggestions on how to perform with a function or VBA code are appreciated.

Following your problem statment exactly: in B1 you can enter the formula:
=IF(MOD(MOD(A1,10),5)=0,5,MOD(MOD(A1,10),5))
And copy it down as needed.
The part MOD(A1,10) pulls off the last digit. You seem to want the remainder of this mod 5 -- hence the outer MOD in (MOD(MOD(A1,10),5). But -- if the mod is 0 you want to report it as 5 -- hence the overall IF
However -- an even quicker way is just:
=IF(MOD(A1,5)=0,5,MOD(A1,5))
Since literally pulling off the first digit is superfluous since 5 is a divisor of 10.
Finally, #chrisneilsen gave an elegant formula which is even shorter:
=MOD(A1-1,5)+1
For all remainders other than 0 , the -1 inside the MOD and the +1 outside cancel each other, but for multiples of 5 (remainder 0) the -1 inside the MOD converts the number to one with a remainder of 4, with 4 + 1 = 5 the final answer.

Related

Display the column number of the last non-empty cell within a range (if values are not unique)

I have the following Excel spreadsheet:
A B C D E F G H I J K L
1
2
3
4 600 150 80 600 0 0 4
5 200 150 80 80 0 0 4
6
7
In Range K4:K5 I currently use the formula from this question to identify the last non-empty cell within the range and get the the column number of it back:
K4 = LOOKUP(2,1/(D4:J4<>0),COLUMN(D4:J4)-MIN(COLUMN(D4:J4))+1)
K5 = LOOKUP(2,1/(D5:J5<>0),COLUMN(D5:J5)-MIN(COLUMN(D5:J5))+1)
This formula works in the simple example above. However, once I use this formula in a bigger spreadsheet I get a lot of performance issues and numbers are not always updated correctly.
Therefore, I am wondering if there is alternative formula to get the column number of the last non-empty cell no matter if the values are unique or not?
As already described in the comments below the question the following solutions are available:
Option A)
If you want to get column number of the last non-empty cell:
=AGGREGATE(14,7,(COLUMN(D4:J4)-MIN(COLUMN(D4:J4))+1)/(D4:J4>0),1)
Option B)
If you want go tet the value in the last non-empty cell:
=INDEX(D4:J4,AGGREGATE(14,7,(COLUMN(D4:J4)-MIN(COLUMN(D4:J4))+1)/(D4:J4>0),1)
=INDEX(A4:J4,AGGREGATE(14,7,COLUMN(D4:J4)/(D4:J4>0),1)

Dragging formulas across - Increment columns by more than 1

I can't seem to find anything similar that's already been asked (they all relate to incrementing row numbers rather than columns)
I'm looking to drag a formula across horizontally and have the columns increment by 2
E.g. B1-A1, D1-C1, F1-E1...
Thanks!
You'll need to have a value in cell A1 and B1 for the following to work.
For my testing I put the number 1 in A1 and B1.
Try this in Cell C1:
=IF(MOD(COUNT($A$1:B1),2)=0,COLUMN(B1),IF(B1<>A1,B1,A1))
Here's what you should see when you drag that formula across:
A B C D E F G H I J K L M N
1 1 2 2 4 4 6 6 8 8 10 10 12 12
And this is what the formula does:
The MOD(COUNT() part of the formula counts the cells to the left of it, and if they are a multiple of 2, the value changes.
I've left the value to change to (the 'new' value) as the COLUMN() number for the cell before, just for example's sake. but you can change this part.
The last IF statement at the end checks if the cell before is equal to the cell before that, (eg. Is CELL C1 equal to CELL B1) and if they are not equal, it will give the cell before as a value (the 'copy' value).

How do I make a cell value represent a number in Excel?

I have
Year 1 2 3 4 5 6
I'm trying to make it so that each year number 1-6 is equal to another number value i.e. Year 1 is equal to 5. Year 2 is equal to 6.
You will not be able to store one value in a cell and use a different value for calculation. However you may do this calculation with the help of a lookup from another table,
Assuming you have the years in Column A and the corresponding mappings are in Column E and F, you can use the below formula in Column B,
=INDEX(E:F,MATCH(A1,E:E,0),2) * 2
This formula lookups the value in A1 in the table E:F and returns the corresponding Column F element. That is finally multiplied with the 2 to show your result. Instead of just using just A1 for multiplying by 2, you should be using INDEX(E:F,MATCH(A1,E:E,0),2). Hope this helps.

Flag rows contributing to the sum criteria

I have two columns like this:
Name Value
A 1
A 4
B 3
B 2
B 5
C 6
C 8
C 10
C 4
I am doing sumif based on Names but I have a criteria to full fill. Looking from the least values in a name group whenever my sum reaches lets say 5 I want those rows to have a flag 1 or else 0. In this example it should be:
Name Value Flag
A 1 1
A 4 1
B 3 1
B 2 1
B 5 0
C 6 0
C 8 0
C 10 0
C 4 1
The data is random and not in any order and file is dynamic so can not work around by just putting it in decreasing order. I do not have any idea about offset. Could it be done without using offset and only by regular ifs, sumifs etc. Thanks a tonn!
Perhaps this can be simplified somewhat, though, assuming you put your chosen threshold (e.g. 5) in J1 and that, as implied by your reply to my last comment, no one value for a given Name occurs more than once, then, in C2, array formula**:
=IFERROR(GESTEP(MATCH(1,0/(MMULT(0+(ROW(INDEX(A:A,1):INDEX(A:A,COUNTIF(A$2:A$10,A2)))>=TRANSPOSE(ROW(INDEX(A:A,1):INDEX(A:A,COUNTIF(A$2:A$10,A2))))),SMALL(IF(A$2:A$10=A2,B$2:B$10),ROW(INDEX(A:A,1):INDEX(A:A,COUNTIF(A$2:A$10,A2)))))<=J$1)),MATCH(B2,SMALL(IF(A$2:A$10=A2,B$2:B$10),ROW(INDEX(A:A,1):INDEX(A:A,COUNTIF(A$2:A$10,A2)))))),0)
Copy down as required.
**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).
In column C add
=IF($B10<5,1,0)
Im assuming the last value is meant to be 0 as the values in C have already passed 5? Try this one.
=IF(SUM(A2:INDEX($B$2:$B$10,MATCH(A2,$A$2:$A$10,0)))<6,1,0)

Give column number of first column with symbol

So say I have a sheet like the following:
Row 1 2 3 4 5 6
1 x
2 x
3 x x
4 x
5 x x
6 x
And I have another sheet which I want to encapsulate the data with:
1st appeared
1
2
1
4
2
6
I'm basically trying to construct the second sheet. Is there a way for each row to start at column two and go up to the ith column and display where the first 'x' appears. (Note that we can assume that every row will have at least 1 'x' when the row and column meet forming a diagonal down the entire first sheet) [Note that my first spreadsheet is roughly 5,000 x 5,000 hence why I'd like a nice formula for this instead of doing things by hand =)]
Thanks in advance!
The MATCH function return the relative position in either a row or column. Use this formula in your second worksheet.
=iferror(match("x", sheet1!1:1, 0) - 1, 0)
Fill down to retrieve the correct column index or zero if not found. A 1 is subtracted since hte want the position relative to column B. The IFERROR function return a default 0 is no match is found. Substitute "" in place of the zero if you want nothing displayed.

Resources