add or subtract from alpha numeric value

fayez_MrExcel

Active Member
Joined
Oct 29, 2005
Messages
439
Office Version
  1. 365
Platform
  1. Windows
I have a alpha numeric code where the number is (always) after the 2nd hyphen on the left, but (sometimes) with different format, one has leading zero and others are not, how can i subtract/add 1 while maintaining the format (see expected value)

Book7
ABCD
1Original ValueExpected Value -/+ 1
2QUA-QT-09-I1QUA-QT-08-I1
3QUA-QT-9-I1QUA-QT-8-I1
4
Sheet1
 
Not as much as it appears. (Your shortest solution is a little more than half as long. Your longer solution, 30% shorter.
I would have thought that 30% and nearly 50% would qualify as "much". But to each his own.


But I thought to leave in meaningful variable names so that it makes sense.
I must admit that I often don't worry about that but in this case I thought that 'term3' was meaningful in the context of this question. Apparently you do not agree.
(You seem to have abandoned that idea in post #10 though.)
 
Last edited:
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
For someone who is keen on having a solution that works all of the time (no matter of the variance of the data) that you nitpicked on my solutions in another thread, note that both of your shorter formulas fail this simple case.
Book1.xlsb
ABCD
1Original ValueExpected Value -/+ 1Amount to add
2QUA-QT-009-009I1QUA-QT-010-010I11
3QUA-QT-009-009I1QUA-QT-010-010I11
Sheet4
Cell Formulas
RangeFormula
C2C2=LET(term3,INDEX(TEXTSPLIT(B2,"-"),3),SUBSTITUTE(B2,term3,TEXT(term3+D2,REPT(0,LEN(term3)))))
C3C3=LET(term3,TRIM(MID(SUBSTITUTE(B3,"-",REPT(" ",20)),40,20)),SUBSTITUTE(B3,term3,TEXT(term3+D3,REPT(0,LEN(term3)))))

But my original, and my "new" one that I just found do not.
Book1.xlsb
ABCD
1Original ValueExpected Value -/+ 1Amount to add
2QUA-QT-009-009I1QUA-QT-010-009I11
3QUA-QT-009-009I1QUA-QT-010-009I11
Sheet4
Cell Formulas
RangeFormula
C2C2=LET(t,TEXTSPLIT(B2,"-"),i,INDEX(t,3),CONCAT(INDEX(t,{1,2})&"-")&IFERROR(REPT(0,LEN(i)-LEN(i+D2)),"")&i+D2&"-"&INDEX(t,{4}))
C3C3=LET( amountToAdd,D3, start,SEARCH("-",B3,SEARCH("-",B3)+1)+1, finish,SEARCH("-",B3,start+1)-1, length,finish-start+1, newResult,MID(B3,start,length)+amountToAdd, numberOfZerosToPrepend,length-LEN(newResult), LEFT(B3,start-1)&IF(numberOfZerosToPrepend>0,REPT("0",numberOfZerosToPrepend),"")&newResult&RIGHT(B3,LEN(B3)-finish) )
 
Upvote 0
Based on how his data looks, this is a very likely case to crop up. (Just a variant of the simple case above.) Again, yours in yellow, mine in blue.
Book1.xlsb
ABCD
1Original ValueExpected Value -/+ 1Amount to add
2QUA-QT-9-I9QUA-QT-10-I101
3QUA-QT-9-I9QUA-QT-10-I101
4QUA-QT-9-I9QUA-QT-10-I91
5QUA-QT-9-I9QUA-QT-10-I91
Sheet4
Cell Formulas
RangeFormula
C2C2=LET(term3,INDEX(TEXTSPLIT(B2,"-"),3),SUBSTITUTE(B2,term3,TEXT(term3+D2,REPT(0,LEN(term3)))))
C3C3=LET(term3,TRIM(MID(SUBSTITUTE(B3,"-",REPT(" ",20)),40,20)),SUBSTITUTE(B3,term3,TEXT(term3+D3,REPT(0,LEN(term3)))))
C4C4=LET(t,TEXTSPLIT(B2,"-"),i,INDEX(t,3),CONCAT(INDEX(t,{1,2})&"-")&IFERROR(REPT(0,LEN(i)-LEN(i+D2)),"")&i+D2&"-"&INDEX(t,4))
C5C5=LET( amountToAdd,D5, start,SEARCH("-",B5,SEARCH("-",B5)+1)+1, finish,SEARCH("-",B5,start+1)-1, length,finish-start+1, newResult,MID(B5,start,length)+amountToAdd, numberOfZerosToPrepend,length-LEN(newResult), LEFT(B5,start-1)&IF(numberOfZerosToPrepend>0,REPT("0",numberOfZerosToPrepend),"")&newResult&RIGHT(B5,LEN(B5)-finish) )
 
Upvote 0
note that both of your shorter formulas fail this simple case.
Fair enough, but my suggestions were simply based on the sample data provided by the OP. If that sample is not representative, then they are quite welcome to say that my suggestions fail them and advise more representative sample data. Or they are quite welcome to stick with the longer suggestions already accepted. No problem from my viewpoint either way.
 
Upvote 0
but my suggestions were simply based on the sample data provided by the OP.
So you would think that, based on how that data looks, that the following case is unlikely?
Book1.xlsb
ABCD
1Original ValueExpected Value -/+ 1Amount to add
2QUA-QT-1-I1QUA-QT-2-I21
3QUA-QT-1-I1QUA-QT-2-I21
Sheet4
Cell Formulas
RangeFormula
C2C2=LET(term3,INDEX(TEXTSPLIT(B2,"-"),3),SUBSTITUTE(B2,term3,TEXT(term3+D2,REPT(0,LEN(term3)))))
C3C3=LET(term3,TRIM(MID(SUBSTITUTE(B3,"-",REPT(" ",20)),40,20)),SUBSTITUTE(B3,term3,TEXT(term3+D3,REPT(0,LEN(term3)))))

Based on how creative you were in the other thread (inserting dates of the form "12DEC2022" where text numbers originally were in the sample data, etc.) in order to break my shorter solutions (which were attempted improvements to my longer/working solution that you were comparing their performance to), I am puzzled that when I just change numbers in the data (keeping everything else the same) in this thread, you're suggesting that it's still possible that your formulas will work for his data.

Or they are quite welcome to stick with the longer suggestions already accepted. No problem from my viewpoint either way.
Until I pointed out that your formulas fail simple modifications to the data, you were insisting how your formulas were better than my long one was due to length (fair enough). But if they don't handle the same variety of data (at least on this basic level), they aren't comparable at all.

But what I don't get is, my solution was already accepted. If you have no problem with him sticking with the longer solution ("from your viewpoint"), then why did you post in this thread at all? It's a legitimate/civil/professional question based on the claim you just made. Please take it as such!

If it was to teach others a more direct way to solve the problem, based on the usual (good) quality/robustness of your formulas, I am puzzled to see that you would think it to be good practice to make assumptions about the data as to make quite a shortcut (with the SUBSTITUTE) that would make the formula vulnerable to minor changes to the data.

The one good thing that came out of this conversation is that if he wants a shorter (working) solution, he has one now, as I showed that my new one passes the same tests as the original, and it's of comparable length (and directness) to yours. But, for completeness, can you find a shorter (working) solution that does the same thing as mine LONG solution (for in earlier versions of Excel)? I have no idea how to tackle that without SEARCH (and still use all non-volatile functions).
 
Last edited:
Upvote 0
But, for completeness, can you find a shorter (working) solution that does the same thing as mine LONG solution (for in earlier versions of Excel)? I have no idea how to tackle that without SEARCH (and still use all non-volatile functions).
This is what I had in mind (for using volatile functions), but I didn't write it until now. Not only does it contain volatile functions, but it's actually longer than my original (condensed) solution!

(The "trick" here is something I learned from @bebo021999 . That ROW(INDIRECT("1:"&x)) = SEQUENCE(x).)
Book1.xlsb
ABCD
1Original ValueExpected Value -/+ 1Amount to add
2QUA-QT-1-I1QUA-QT-2-I11
3QUA-QT-1-I1QUA-QT-2-I11
4QUA-QT-1-I1QUA-QT-2-I11
5QUA-QT-1-I1QUA-QT-2-I11
6QUA-QT-009-I1QUA-QT-008-I1-1
7QUA-QT-009-I1QUA-QT-009-I10
8QUA-QT-009-I1QUA-QT-010-I11
9QUA-QT-009-I1QUA-QT-011-I12
10QUA-QT-009-I1QUA-QT-012-I13
11QUA-QT-009-I1QUA-QT-013-I14
12QUA-QT-009-I1QUA-QT-014-I15
13QUA-QT-09-I1QUA-QT-04-I1-5
14QUA-QT-09-I1QUA-QT-05-I1-4
15QUA-QT-09-I1QUA-QT-06-I1-3
16QUA-QT-09-I1QUA-QT-07-I1-2
17QUA-QT-09-I1QUA-QT-08-I1-1
18QUA-QT-09-I1QUA-QT-09-I10
19QUA-QT-09-I1QUA-QT-10-I11
20QUA-QT-09-I1QUA-QT-11-I12
21QUA-QT-09-I1QUA-QT-12-I13
22QUA-QT-09-I1QUA-QT-13-I14
23QUA-QT-09-I1QUA-QT-14-I15
24QUA-QT-9-I1QUA-QT-4-I1-5
25QUA-QT-9-I1QUA-QT-5-I1-4
26QUA-QT-9-I1QUA-QT-6-I1-3
27QUA-QT-9-I1QUA-QT-7-I1-2
28QUA-QT-9-I1QUA-QT-8-I1-1
29QUA-QT-9-I1QUA-QT-9-I10
30QUA-QT-9-I1QUA-QT-10-I11
31QUA-QT-9-I1QUA-QT-11-I12
32QUA-QT-9-I1QUA-QT-12-I13
33QUA-QT-9-I1QUA-QT-13-I14
34QUA-QT-9-I1QUA-QT-14-I15
35QUA-QT-09-I1QUA-QT-08-I1-1
36QUA-QT-9-I1QUA-QT-8-I1-1
37QUA-QT-0222-I2QUA-QT-0223-I21
38QUA-QT-989-I3QUA-QT-990-I31
39QUA-QT-99-I1QUA-QT-100-I11
Sheet4
Cell Formulas
RangeFormula
C2:C39C2=LET( r,ROW(INDIRECT("1:"&LEN(B2))), i,IF(MID(B2,r,1)="-",r,""), s_2,SMALL(i,2), s_3,SMALL(i,3), n,MID(B2,s_2+1, s_3-s_2-1), LEFT(B2,s_2)&IFERROR(REPT(0,LEN(n)-LEN(n+D2)),"")&n+D2&MID(B2, s_3,LEN(B2)) )
 
Upvote 0
you were insisting how your formulas were better than my long one was due to length
I made no such insistence, I simply offered the OP an option based on length.

If you have no problem with him sticking with the longer solution ("from your viewpoint"), then why did you post in this thread at all?
To offer the OP an option, should they wish to choose it - just like you do sometimes after a solution has been accepted

make quite a shortcut (with the SUBSTITUTE) that would make the formula vulnerable to minor changes to the data.
I did concede that. I have added two more characters to those formulas below to cover that situation. (I am assuming no digits in the first two terms but I think that seems more reasonable given the sample data)

But, for completeness, can you find a shorter (working) solution that does the same thing as mine LONG solution (for in earlier versions of Excel)?
Earlier versions of Excel? All your suggestions use LET which is only available in very recent versions. Perhaps you just mean versions that have not yet received all the latest functions (eg TEXTSPLIT)?

Anyway, here are a few more possibilities, depending on version and functions available.

22 09 27.xlsm
ABCDE
1Original ValueResultAmount to addNotes
2QUA-QT-1-I1QUA-QT-2-I11Post 9 formula 1 adjusted for point raised in post 12
3QUA-QT-2-I1Post 9 formula 2 adjusted for point raised in post 12
4QUA-QT-2-I1All Excel versions
5QUA-QT-2-I1All functions available and possible digits in any term
+ -
Cell Formulas
RangeFormula
C2C2=LET(term3,INDEX(TEXTSPLIT(B2,"-"),3),SUBSTITUTE(B2,term3,TEXT(term3+D2,REPT(0,LEN(term3))),1))
C3C3=LET(term3,TRIM(MID(SUBSTITUTE(B2,"-",REPT(" ",20)),40,20)),SUBSTITUTE(B2,term3,TEXT(term3+D2,REPT(0,LEN(term3))),1))
C4C4=SUBSTITUTE(B2,TRIM(MID(SUBSTITUTE(B2,"-",REPT(" ",20)),40,20)),TEXT(TRIM(MID(SUBSTITUTE(B2,"-",REPT(" ",20)),40,20))+D2,REPT(0,LEN(TRIM(MID(SUBSTITUTE(B2,"-",REPT(" ",20)),40,20))))),1)
C5C5=LET(ts,TEXTSPLIT(B2,"-"),term3,INDEX(ts,3),TEXTJOIN("-",,INDEX(HSTACK(ts,TEXT(term3+D2,REPT(0,LEN(term3)))),,{1,2,5,4})))
 
Upvote 0

Forum statistics

Threads
1,215,522
Messages
6,125,312
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top