Eliminate consecutive numbers in a 5 number string
May I please be assisted with a code that eliminates consecutive numbers.
I found this one on the internet but it does not do the job from mrexceldotcomforum (mrexcel.com/forum/excel-questions/712373-how-remove-consecutive-number-string-only-column.html)
I used it on my sheet, but it still gives me results such as 1,2,3,4,5.
Can I have some help please, how can I manipulate it that it only gives me a unique set of 5 non-consecutive numbers only, such as 13579 instead of 12345?
Thank you in advance
Last edited by jeffreybrown; 08-19-2019 at 01:23 PM.
Reason: Please use code tags!
Re: Eliminate consecutive numbers in a 5 number string
Please show example cell data and expected results. The link's code input value are delimited by space characters. If your data is not that, then code will need to be changed.
Or, did you want to generate a random 5 digit integer number with no two digits increasing by one? e.g. A1=13213, but not A1=12231.
Or, if A1=12313 then return B1=True or if A1=12231 then B1=False.
Avoid using Select, Selection and Activate in your code. Use With ... End With instead. You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.
Re: Eliminate consecutive numbers in a 5 number string
thank you very much for your replies!
At: bakerman2 I get an error
At: jiodon the code simply copies column A to Column B. Yes I would really love to upload my workbook but at this time the forum does not allow me to make such abs upload. Hence I thought of the direct messaging option.
Re: Eliminate consecutive numbers in a 5 number string
Please attach a sample workbook (not a picture or pasted copy). Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.
Remember to desensitize the data.
Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
Re: Eliminate consecutive numbers in a 5 number string
I think post #4 might have explained the need best. The way I read it:
1. The next number can not be one more than the previous.
2. No duplicate numbers.
There might be a 3rd step where duplicates are removed after (2) and recheck the results.
The examples files are ok for input but do not show output results. It is my recommendation to manually markup the column B results for a few rows and attach that file. The forum can skew things due to alignment via html.
Here are some of my interpretations:
Input: 1 2 4 22 23
Output: 4
Input: 22 24 26 22 3
Output: 22 24 26 3
Input: 1 3 5 1 3
Output: 1 3 5
Or, is it that all of the output in cases above would be nothing?
Last edited by Kenneth Hobson; 08-20-2019 at 06:36 PM.
According to your post #18 attachment that's irrelevant as it has no duplicate !
If you change rules or data, just update accordingly the formulas within the next demonstration …
An Excel beginner can achieve this without any code just with formulas in helper columns
so according to your post #18 full file I apply some formulas in this starter demonstration :
PHP Code:
Sub Demo1() Application.ScreenUpdating = False With [A1].CurrentRegion.Columns(1) .Item("B:M").Clear .TextToColumns [D1], xlDelimited, Space:=True .Item("I:M").Formula = Array("=IF(E1-D1>1,D1,"""")", "=IF(AND(ISNUMBER(I1),F1-E1>1),E1,"""")", _ "=IF(AND(F1-E1>1,G1-F1>1),F1,"""")", "=IF(AND(G1-F1>1,ISNUMBER(M1)),G1,"""")", "=IF(H1-G1>1,H1,"""")") .Item(2).Formula = "=TRIM(I1&"" ""&J1&"" ""&K1&"" ""&L1&"" ""&M1)" .Item(2).NumberFormat = "@" .Item(2).Formula = .Item(2).Value2 .Item("D:M").Clear End With Application.ScreenUpdating = True End Sub
Do you like it ? So thanks to click on bottom left star icon « ★ Add Reputation » !
Sub Demo2() Dim V, R&, S With [A1].CurrentRegion.Columns V = .Item(1).Value2 For R = 1 To UBound(V) S = Split(V(R, 1)) If S(1) - S(0) > 1 Then V(R, 1) = " " & S(0) Else V(R, 1) = "" If S(1) - S(0) > 1 And S(2) - S(1) > 1 Then V(R, 1) = V(R, 1) & " " & S(1) If S(2) - S(1) > 1 And S(3) - S(2) > 1 Then V(R, 1) = V(R, 1) & " " & S(2) If S(3) - S(2) > 1 And S(4) - S(3) > 1 Then V(R, 1) = V(R, 1) & " " & S(3) If S(4) - S(3) > 1 Then V(R, 1) = V(R, 1) & " " & S(4) Next .Item(2).Value2 = V End With End Sub
► Do you like it ? ► ► So thanks to click on bottom left star icon « ★ Add Reputation » !
Is it possible to write a piece of code that will take all 5 digit numbers from Column B and copy them over to Column C, I have attached the workbook.
You must explain all your need when you create the thread
as it can be a waste of time for any helper to rewrite a code for any new change !
As per forum rules a new question, a new thread …
So I hope at least it's now your complete need (?) or explain at least all you need.
Sub Demo3() Const F = "¤" Dim B, C, R&, S$(), T$() With [A1].CurrentRegion.Columns B = .Item(1).Value2: C = B For R = 1 To UBound(B) S = Split(B(R, 1)) If UBound(S) = 4 Then T = S If T(1) - T(0) < 2 Then S(0) = F If S(0) = F Or T(2) - T(1) < 2 Then S(1) = F If T(2) - T(1) < 2 Or T(3) - T(2) < 2 Then S(2) = F If T(4) - T(3) < 2 Then S(4) = F If T(3) - T(2) < 2 Or S(4) = F Then S(3) = F S = Filter(S, F, False) B(R, 1) = Join(S) C(R, 1) = IIf(UBound(S) = 4, B(R, 1), Empty) Else B(R, 1) = F: C(R, 1) = F End If Next .Item("B:C").NumberFormat = "@" .Item(2).Value2 = B .Item(3).Value2 = C End With End Sub
► Do you like it ? ► ► So thanks to click on bottom left star icon « ★ Add Reputation » !
Last edited by Marc L; 08-22-2019 at 08:49 AM.
Reason: oops ! Optimization …
Re: Eliminate consecutive numbers in a 5 number string
thanks jindon, I noticed in my original post that there was some details missing in the output, this threat has been going on for a while I will close it as requested and start a new one.
Bookmarks