How to add +1 to the last two digits of a number? - excel

I want a way to do the following
Original Data I want result as
507-413-0109 507-413-0110
507-430-5409 507-430-5410
507-450-6649 507-450-6650
507-450-8640 507-450-8651
507-451-1563 507-451-1574
if the original number is 2133-2322 , I want it as 2133-2333 .
The last two digits should increase by value 1.
But , if there is a 9,
9+1 = 0
and
0+1 = 1
how to do it in Excel ?

You have to extract the 3rd column of numer (0109, 5409, ...) in a particular column. You can use the function RIGHT(A1,1) (where A1 is the cell which contains the 207-413-0109 string).
Then you just add +1 to them.

=REPLACE($A1,11,2,TEXT(IF(VALUE(RIGHT($A1)) = 9, MOD(VALUE(RIGHT($A1,2))+1,100), VALUE(RIGHT($A1,2))+11),"00"))
Put this in cell C1, then copy to C2 through C5.
I assumed that if the number ends in 99, then it replaces 99 with 00.
This will handle the 99 case as a 4-digit number instead of two, all other things being same:
=REPLACE($A1,9,4,TEXT(IF(VALUE(RIGHT($A1)) = 9, MOD(VALUE(RIGHT($A1,4))+1,10000), VALUE(RIGHT($A1,4))+11),"0000"))
So 1599 will become 1600 instead of 1500. And 9999 will just become 0000.

Assuming that your data begins in the "A1" spreadsheet position, if you put the folling formula in the "B1" cell, and then copy it downward, it should derive the values you're for which you're looking
=LEFT(A2,10) & RIGHT(TEXT(VALUE(RIGHT(A2,2)) + 1,"00"),2)

Related

How to color max. 2 consecutive values in Excel without using VBA?

I'm out of idea how I could format consecutive same (respectively only even) values in Excel tables without using VBA.
The conditional formatting shall color only consecutive values and only
all 0s or all even values, when there are not more than 2.
A: ID
B: binary
C: counting
1
1
1
2
0
2
3
0
2
4
1
3
5
0
4
6
0
4
7
0
4
8
1
5
9
1
5
I tried to format with: =COUNTIF(C1:C9, C1) < 3, but then it also colors the 1s and C6:C7, eventho there are more than 2.
I also tried =AND( COUNTIF(C1:C9,C1) < 3, ISEVEN(C1:C9) ) but then it colors nothing.
I could replace the 0s with empty cells so I could check ISEMPTY(B1:B9) but it again colors nothing. Using $ to set absolute changes nothing as well.
Formating duplicates also colors triplets, which also doesn't work for me.
=OR(COUNTIF($C$1:$C$9,C1) = 1, COUNTIF($C$1:$C$6,C1) = 2) works so far, but also colors the 1s (uneven).
=AND(OR(COUNTIF($C$1:$C$9,C1) = 1, COUNTIF($C$1:$C$6,C1) = 2), ISEVEN($C$1:$C$9)) doesn't work.
=AND(OR(COUNTIF($C$1:$C$9,C1) = 1, COUNTIF($C$1:$C$6,C1) = 2), $B$1:$B$9 <> 1) doesn't work as well.
My only solution so far is using 2 formating rules:
color =OR(COUNTIF($C$1:$C$9,C1) = 1, COUNTIF($C$1:$C$6,C1) = 2)
do not color =$B$1:$B$9 = 1
but I think it is terrible.
I worked on it for some hours, maybe I'm missing something really obvious.
I'm not allowed to use VBA, therefore this is ot an option.
EDIT: My 2.rule-solution can be simplificed with:
color =COUNTIF($C$1:$C$9,C1) < 3
do not color =$B$1:$B$9 = 1
I'm still confused why combining both doesn't work:
AND(COUNTIF($C$1:$C$9,C1) < 3; $B$1:$B$9 <> 1)
EDIT2: I know why it didn't work. Don't check <>1 with absolute value-range $B$1$:$B$9
Solution: B1 <> 1 then it loops through.
Now combining both works:
=AND( COUNTIF($C$1:$C$9, C1) < 3, B1 <> 1)
I can't see an easy answer for the binary numbers. You have two cases:
(1) Current cell is zero, previous cell is 1, next cell is zero and next cell but one is 1.
(2) Current cell is zero, previous cell is zero, previous cell but one is 1, next cell is 1.
But then the first pair of numbers is a special case because there is no previous cell.
Strictly speaking the last pair of numbers is a special case as well because there is no following cell.
=OR(AND(ROW()=1,B$1=0,B$2=0,B$3=1),AND(ROW()=2,B$1=0,B$2=0,B$3=1),AND(B1=0,B1048576=1,B2=0,B3=1),AND(B1=0,B1048576=0,B1048575=1,B2=1))
where I have used the fact that you are allowed to wrap ranges to the end of the sheet (B1048576) in conditional formatting.
Adding the condition for the case where there there are two zeroes at the end of the range:
=OR(AND(ROW()=1,B$1=0,B$2=0,B$3=1),
AND(ROW()=2,B$1=0,B$2=0,B$3=1),
AND(B1=0,B1048576=1,B2=0,OR(B3=1,B3="")),
AND(B1=0,B1048576=0,B1048575=1,OR(B2=1,B2="")))
Even this could go wrong if there was something in the very last couple of rows of the sheet, so I suppose to be absolutely safe:
=OR(AND(ROW()=1,B$1=0,B$2=0,B$3=1),
AND(ROW()=2,B$1=0,B$2=0,B$3=1),
AND(Row()>1,B1=0,B1048576=1,B2=0,OR(B3=1,B3="")),
AND(Row()>2,B1=0,B1048576=0,B1048575=1,OR(B2=1,B2="")))
Shorter:
=OR(AND(ROW()<=2,B$1+B$2=0,B$3=1),
AND(B1+B2=0,B1048576=1,OR(B3=1,B3="")),
AND(B1+B1048576=0,B1048575=1,OR(B2=1,B2="")))
Not the cleanest wat but it works. You only need to move your data 1 row below, so headers would be in row 2 and data in row 3 for this formula to work:
=IF(AND(B3=B4,B3<>B5),IF(AND(B4=B3,B4<>B2),TRUE,FALSE),IF(AND(B3=B2,B3<>B1),IF(AND(B3=B4,B3<>B5),FALSE,TRUE),FALSE))
How about this approach (Office 365):
=LET(range,B$1:B$9,
s,IFERROR(TRANSPOSE(INDEX(range,ROW()+SEQUENCE(5,,-2))),1),
t,TEXTJOIN("",,(s=INDEX(range,ROW()))*ISEVEN(s)),
IFERROR(SEARCH("0110",t)<4,IFERROR(SEARCH("010",t)=2,FALSE)))
It creates an array s of 5 values starting point is the current row of the range, adding the 2 values above and below. If the value is out of range it will replace the error with a 1.
The array s is checked for being even (TRUE/FALSE, IFERROR created values are uneven) and the values to equal the value of the current row of the range (TRUE/FALSE).
These two booleans are multiplied creating 1 for both values being TRUE, else 0.
These values are joined and checked for 2 consecutive 1's (surrounded by 0) to be found in the 2nd or 3rd position of the range (this would be the case if two even consecutive equal numbers are found),
if it errors it will look if a unique even number is found (1 surrounded by 0 in 2nd position).
PS I'm unable to test if conditional formatting allows you to type the range as B:B instead of B$1:B$9 (working from a mobile) but that would make it more dynamical, because that way you can easily expand the conditional range.

Function that return the lines where test is true for first time, then second time, then third time etc

Title is vague. I will try to provide example.
I column where there are values, it is organized like in the screencap.
Example of my table
I need a function I can drag down and that will return the line where the test (value = A) is true for the first time then true for the second time, and so on.
In my example, if I search for "A", it should return 1 then 2 then 3 then 6 then 12 and then 0 or N/A or an error.
Edit : Thanks to Gary's Student for a function that perfectly matched my needs.
BR,
a1a1a1a1
With data in column A, in B1 enter:
=IFERROR(AGGREGATE(15, 6, ROW($1:$999)/($A$1:$A$13="A"), ROW(1:1)),"")
and copy downward:

Excel adding more conditions in a formula

I believed the condtions written will be quite long and i am not really good in writing this long formula
There are 6 columns i've used which is D ,E, M, N, O, P
Sample data:
D3=123456(Changing variable as it can be 12345, 12345A,123456A)
E3=1
M3=31
N3=_
O3=00
P3=0
The formula are design based on this Column D field(the variable changes is in this field) let say
if length of D3 = 6 then (the current formula i've done)
=IF(LEN(D3)=6,CONCATENATE(M3,D3,N3,O3,E3),CONCATENATE(M3,D3,O3,E3))
The outcome for this will be 31123456_001, if let say the D variable is changed to 123456A( the else
in the formula i've shown as no concatenate N3)
then the outcome will be 31123456A001.
I have added in column p, so that i can use it to concatenate to the format that i need.
There are a few more conditions i need to add in,
Which is
1. If the D3= 12345, the format outcome will be 31012345_001 (concatenate M3,P3,D3,N3,O3,E3)
2. If the D= 12345A, the format outcome will be 31012345A001 (concatenate M3,P3,D3,O3,E3)
3. Data for the column D3 field, 12345A, the A alphabet can be in A-Z.
These are the list of all conditions and outcome that i required in a formula.
1. D3 = 123456 then the outcome will be 31123456_001
2. D3 = 123456A then outcome will be 31123456A001
3. D3 = 12345 then outcome will be 31012345_001
4. D3 = 12345A then outcome will be 31012345A001
Additional info:
These are just format as it can be any numbers combinations, the last letter alphabet can be A-Z
D3 = 123456
D3 = 123456A
D3 = 12345
D3 = 12345A
As I couldn't quite catch all the conditions and outcomes, here is an example of how your formula could look:
=IF(LEN(D3)=5,Outcome_1_Concatenation,IF(LEN(D3)=7,Outcome_2_Concatenation,IF(ISNUMBER(VALUE(RIGHT(D3,1))),Outcome_3_Concatenation,Outcome_4_Concatenation)))
Outcome_1_Concatenation => replace with formula when LEN = 5
Outcome_2_Concatenation => replace with formula when LEN = 7
Outcome_3_Concatenation => replace with formula when LEN = 6 and all are numbers
Outcome_4_Concatenation => replace with formula when LEN = 6 and last is character
If you give all examples in a condition => outcome list, I would be glad to help further.
I would look at creating a lookup table range with 3 options for lengths of 5,6,7.
I named my lookup table range "Length".
First setup this lookup table like this:
5 |
=CONCATENATE(M$3,P$3,D$3,IF(ISNUMBER(VALUE(RIGHT(D3,1))),N3,""),O$3,E$3)
6 |
=CONCATENATE(M$3,IF(ISNUMBER(VALUE(RIGHT(D$3,1))),"",P$3),D$3,IF(ISNUMBER(VALUE(RIGHT(D3,1))),N$3,""),O$3,E$3)
7 |
=CONCATENATE(M$3,D$3,IF(ISNUMBER(VALUE(RIGHT(D$3,1))),N$3,""),O$3,E$3)
For any D3 value, it is checking if that last character is a letter, and if not it will insert N3, otherwise it leaves it out.
Also, for any 6 character value, it checks if the last character is a letter, and if so, it will insert P3, otherwise it leaves it out.
Then, your output formula should be:
=VLOOKUP(LEN(D3),Length,2,FALSE)
This makes it clean and simple.
This is your formula plus the added conditions 1 and 2:
=IF(D3=12345,CONCATENATE(M3,P3,D3,N3,O3,E3),IF(D3="12345A",CONCATENATE(M3,P3,D3,O3,E3),IF(LEN(D3)=6,CONCATENATE(M3,D3,N3,O3,E3),CONCATENATE(M3,D3,O3,E3)))
If you want a more generalized version you can check if D3 is a number, the length of it, if D3 ends with a letter, and replace the nested ifs according to your needs
I got my answers, it's
=IF(AND(LEN(D3)>=6,ISNUMBER(RIGHT(D3,1)*1)),M3&D3&N3&O3&E3,IF(AND(LEN(D3)<6,ISNUMBER(RIGHT(D3,1)*1)),M3&P3&D3&N3&O3&E3,IF(AND(LEN(D3)=6,ISTEXT(RIGHT(D3,1))),M3&P3&D3&O3&E3,M3&D3&O3&E3)))

Excel: complicated SUMPRODUCT formula needed

I'm in search of a SUMPRODUCT formula, or a similar sort of formula which does the same thing. It should do the following:
On worksheet A it needs to ignore incorrect zipcodes, meaning
zipcodes which do not consist of 4 numbers and 2 letters need to be
ignored. It also needs to take into account that there sometimes are
superfluous spaces behind the zipcode. And sometimes there is a
space between the numbers and letters, sometimes there isn't. Just
the 4 numbers and 2 letters need to be compared.
The correct zipcodes on worksheet A need to be compared with the
zipcodes on worksheet B. If they match, then all the values behind
the zip code need to be summed up. If there is another record
starting with the same zipcode then these need to be added up as
well.
Neither of the worksheets should need to be changed, since the data is generated frequently. The formula should be able to work on a third, separate worksheet. And it should work in Excel 2003.
EDIT: Added point 3.
I'll add an image to visualize what I mean. Hopefully someone can help me!
With some helper columns, you could use something like this (open in new tab for larger version):
The formulae:
In B2 to remove spaces and hence get a 'clean' ZIP and check the length:
=IF(LEN(SUBSTITUTE(A2," ",""))=6,SUBSTITUTE(A2," ",""),"")
In C2, to get the sum:
=IFERROR(IF(AND(ISNUMBER(LEFT(B2,4)*1),CODE(MID(LOWER(B2),5,1))>=97,CODE(MID(LOWER(B2),5,1))<=122,CODE(RIGHT(LOWER(B2)))>=97,CODE(RIGHT(LOWER(B2)))<=122),SUMPRODUCT($H$2:$K$8*($G$2:$G$8=B2)),""),"")
In G2, I used the same one as in B2:
=IF(LEN(SUBSTITUTE(F2," ",""))=6,SUBSTITUTE(F2," ",""),"")
Without the helper, the formula becomes much longer because of repeating parts:
=IFERROR(IF(AND(LEN(SUBSTITUTE(A2," ",""))=6,ISNUMBER(LEFT(SUBSTITUTE(A2," ",""),4)*1),CODE(MID(LOWER(SUBSTITUTE(A2," ","")),5,1))>=97,CODE(MID(LOWER(SUBSTITUTE(A2," ","")),5,1))<=122,CODE(RIGHT(LOWER(SUBSTITUTE(A2," ",""))))>=97,CODE(RIGHT(LOWER(SUBSTITUTE(A2," ",""))))<=122),SUMPRODUCT($H$2:$K$8*(SUBSTITUTE($F$2:$F$8," ","")=SUBSTITUTE(A2," ",""))),""),"")
Or
=IFERROR(
IF(
AND(
LEN(SUBSTITUTE(A2," ",""))=6, ' Check length
ISNUMBER(LEFT(SUBSTITUTE(A2," ",""),4)*1), ' Check numbers
CODE(MID(LOWER(SUBSTITUTE(A2," ","")),5,1))>=97, ' Check if letter
CODE(MID(LOWER(SUBSTITUTE(A2," ","")),5,1))<=122, ' Check if letter
CODE(RIGHT(LOWER(SUBSTITUTE(A2," ",""))))>=97, ' Check if letter
CODE(RIGHT(LOWER(SUBSTITUTE(A2," ",""))))<=122 ' Check if letter
),
SUMPRODUCT(
$H$2:$K$8*
(SUBSTITUTE($F$2:$F$8," ","")=SUBSTITUTE(A2," ",""))),
""
),
""
)
Oops, forgot that IFERROR was not in 2003. The only reason why I used it was that MID would return an empty string and CODE would subsequently give an error. You can use the below instead which makes sure the string is 6 chars first:
=IF(LEN(SUBSTITUTE(A2," ",""))=6,IF(AND(ISNUMBER(LEFT(SUBSTITUTE(A2," ",""),4)*1),CODE(MID(LOWER(SUBSTITUTE(A2," ","")),5,1))>=97,CODE(MID(LOWER(SUBSTITUTE(A2," ","")),5,1))<=122,CODE(RIGHT(LOWER(SUBSTITUTE(A2," ",""))))>=97,CODE(RIGHT(LOWER(SUBSTITUTE(A2," ",""))))<=122),SUMPRODUCT($H$2:$K$8*(SUBSTITUTE($F$2:$F$8," ","")=SUBSTITUTE(A2," ",""))),""),"")
Here you have a formula to validate Dutch postal codes
=AND(LEN(A2)=6; ISNUMBER(VALUE(LEFT(A2;4))); CODE(MID(LOWER(A2);5;1)) >= 97; CODE(MID(LOWER(A2);5;1)) <= 122; CODE(MID(LOWER(A2);6;1)) >= 97; CODE(MID(LOWER(A2);6;1)) <= 122)
0-9 = ASCII code 48 to 57
a-z = ASCII code 97 to 122 (lowercase)
In case you have a Dutch version of Excel, the formula would be:
=EN(LENGTE(A2)=6; ISGETAL(WAARDE(LINKS(A2;4))); CODE(DEEL(KLEINE.LETTERS(A2);5;1)) >= 97; CODE(DEEL(KLEINE.LETTERS(A2);5;1)) <= 122; CODE(DEEL(KLEINE.LETTERS(A2);6;1)) >= 97; CODE(DEEL(KLEINE.LETTERS(A2);6;1)) <= 122)

How to compare only a part of strings in different excel sheets(columns)

How to compare two sheet columns specied below, however only part of string in location(column)in sheet1 will matches only part of string with the Location(column) in sheet2?
1.only 1st two characters of location(column) in sheet1 and 1st two characters of location(column) in sheet2 should match.
2.only any two characters of location(column) in sheet1 and any two characters of location(column) in sheet2 should match. Please help
Location(sheet1) Location(sheet2)
____________________________________________
india- north USxcs
India-west Indiaasd
India- east Indiaavvds
India- south Africassdcasv
US- north Africavasvdsa
us-west UKsacvavsdv
uk- east Indiacascsa
uk- south UScssca
Africa-middle Indiacsasca
Africa-south Africaccc
Africa-east UKcac
For question 1 you can use MID function to extract the first two characters from each cell value and compare them.
For question 2 there is a solution if you can accept a predetermined maximum length of string. It is not a very nice solution! You can use nested if statements, basically 'unrolling the loop'. This example compares cell A1 and B1 for lengths of A1 up to 12 characters:
=IF(IFERROR(FIND(MID(A1,1,2),B1,1),0)>0,TRUE,
IF(IFERROR(FIND(MID(A1,2,2),B1,1),0)>0,TRUE,
IF(IFERROR(FIND(MID(A1,3,2),B1,1),0)>0,TRUE,
IF(IFERROR(FIND(MID(A1,4,2),B1,1),0)>0,TRUE,
IF(IFERROR(FIND(MID(A1,5,2),B1,1),0)>0,TRUE,
IF(IFERROR(FIND(MID(A1,6,2),B1,1),0)>0,TRUE,
IF(IFERROR(FIND(MID(A1,7,2),B1,1),0)>0,TRUE,
IF(IFERROR(FIND(MID(A1,8,2),B1,1),0)>0,TRUE,
IF(IFERROR(FIND(MID(A1,9,2),B1,1),0)>0,TRUE,
IF(IFERROR(FIND(MID(A1,10,2),B1,1),0)>0,TRUE,
IF(IFERROR(FIND(MID(A1,11,2),B1,1),0)>0,TRUE,
FALSE
)
)
)
)
)
)
)
)
)
)
)
Thanks to James Jenkins for this update
It seems older versions of Excel have a limit of 7 nested functions. You can work around this (if you don't mind making your spreadsheet even more ugly) by chaining together formulas in adjacent cells. In fact if you wanted to get really creative you could use the column index to calculate the offsets for the search, something like:
=IF(IFERROR(FIND(MID($A1,(COLUMN(C1) - 3) * 6 + 1, 2), $B1, 1),0)>0,TRUE,
...repeat with +2, +3, +4, +5
if(D2 = FALSE, FALSE, TRUE)
)))))))
Then the column can be copied right if you ever need more string length. Note the innermost 'if' should force a TRUE or FALSE value when the adjacent column is blank.

Resources