Sort Rows By Paragraph Number

Caleeco

Well-known Member
Joined
Jan 9, 2016
Messages
980
Office Version
  1. 2010
Platform
  1. Windows
Hello

I have a problem i'm trying to solve where by each ROW in column B has a Paragraph number stored as TEXT (which can change).

I need to be able to sort the rows in conventional paragraph sorting order, see example attached; cutting and pasting entire rows as they shuffle in the correct places.

As you can see, sorting largest to smallest via a Excel table does not give me the intended result. Is there an elegant way to solve this using VBA? I can think of a brute force LOOP UNTIL, but that would be computationally pretty intense i'd imagine!

Many Thanks
Caleeco
 

Attachments

  • SortImg.JPG
    SortImg.JPG
    63.5 KB · Views: 16
OK, if vba is allowed then you could use this udf to produce a column of values to sort by.

VBA Code:
Function SortVal(s As String, Optional maxterms As Long = 5) As String
  Dim RX As Object
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "\.(?=\d(\.|$))"
  SortVal = Left(RX.Replace("." & s & Replace(Space(maxterms), " ", ".0"), ".0"), 3 * maxterms)
End Function

Here is my sample data with the udf deployed but sort not yet done.

Cell Formulas
RangeFormula
I2:I12I2=SortVal(H2)


After sort (which I have done manually but you could by code if you want)

Cell Formulas
RangeFormula
I2:I12I2=SortVal(H2)


By default the function allows for up 5 sections. If you want or need more, just use the optional second function argument.
eg This to allow for 20 sections
Excel Formula:
=SortVal(H2,20)
 
Upvote 0
Solution

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
OK, if vba is allowed then you could use this udf to produce a column of values to sort by.

VBA Code:
Function SortVal(s As String, Optional maxterms As Long = 5) As String
  Dim RX As Object
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "\.(?=\d(\.|$))"
  SortVal = Left(RX.Replace("." & s & Replace(Space(maxterms), " ", ".0"), ".0"), 3 * maxterms)
End Function

Here is my sample data with the udf deployed but sort not yet done.

Cell Formulas
RangeFormula
I2:I12I2=SortVal(H2)


After sort (which I have done manually but you could by code if you want)

Cell Formulas
RangeFormula
I2:I12I2=SortVal(H2)


By default the function allows for up 5 sections. If you want or need more, just use the optional second function argument.
eg This to allow for 20 sections
Excel Formula:
=SortVal(H2,20)

Wow this is great!

I wouldnt have thought of using a RegEx for this. I will try and unpick it so I can understand how it works.

Could we also calculate 'maxterms' in the UDF> maxterms = Count(".") of input string +1

I'll see if I can get something like this to work.

Thanks for your help!
Caleeco
 
Upvote 0
Could we also calculate 'maxterms' in the UDF
You could, using a udf with different arguments.

VBA Code:
Function Sort_Val(rAll As Range, s As String) As String
  Dim RX As Object
  Dim maxterms As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "\.(?=\d(\.|$))"
  maxterms = Evaluate(Replace("Max(Len(#) - Len(Substitute(#, ""."", """")))+1", "#", rAll.Address))
  Sort_Val = Left(RX.Replace("." & s & Replace(Space(maxterms), " ", ".0"), ".0"), 3 * maxterms)
End Function

Cell Formulas
RangeFormula
I2:I12I2=Sort_Val(H$2:H$12,H2)
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,196
Members
449,072
Latest member
DW Draft

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