Separate sections custom sort - numbers with text separate sections

butch3

Board Regular
Joined
Feb 4, 2019
Messages
54
I've attached two images that will help visualize what I'm trying to do. I have 4 different types of data that need to be sorted. In the 'example' image, you can see that the accounts (Col A) is all clumped together. The 'output' image shows what I would like to happen. The number of data elements will change for each spreadsheet (macro) I would run the code on. Highlighting of the two sums preferred.

The first section in 'output' is for the accounts that are only numbers. The second sections is for accounts that have letters after the first 3 digits.

A Sum is produced for a combined total of the values in the Amt Column.

Next, when the first three digits are 'Fnd'. Total is produced for this section. The numbers to the right of the Amt column is removed.

Lastly, if the Name Column is named 'Enc' the row would be placed in this section. An alternate way to code this is to evaluate whether the 5th and 6th digit is 0 if it is, the row transfers into the last section and a sum for this section is produced. The numbers to the right of the Amt column is removed.

Note, each section is sorted by the Account without any sorting into any other section.


Example:
Book1
ABFGH
1AcctNameAmtNew AmtDifference
2456 456954
3654 2F2844
4123 456734
5Fnd 203624
6789 987505
7123 001Enc413
8654 45636-3
9Fnd 10127-5
10258 75308-8
11789 002Enc19-8
Sheet1



After Macro Output:
Book1
ABFGHI
1AcctNameAmtNew AmtDifference
2123 456734
3258 75308-8
4456 456954
5654 45636-3
6789 987505
7
8654 2F2844
932266Total
10
11Fnd 1012
12Fnd 2036
138Total
14
15123 001Enc4
16789 002Enc1
175Total
Sheet1
 

Attachments

  • example.PNG
    example.PNG
    8.8 KB · Views: 7
  • output.PNG
    output.PNG
    14.1 KB · Views: 6
Check all decimals.
You could paste the original data here to review.
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Okay. So I ran the code in a totally new sheet with no other code other than yours (I only changed the Enc and Vou so that it was capitalized fully and I'm showing that it is labeling not balanced when it should be balanced. And the weird thing is I have changed "Not Balanced" to wtot and also to .Value, and it is picking up the correct values to compare to....

1581009153718.png


Example:
Book1
ABCDEFGH
1AccountDescriptionENCRBRDifference
2101 12ST03792417/18 PAVEMENT11,560.4311560.430
3101 2100001ENCUMBRANCES-11,560.43-4,631,281.61
4101 2100002VOUCHERS PAYABL-124,946.14-512,106.45
5203 12ST0389242019 PAVE 9,191.049191.040
6203 2100001ENCUMBRANCES-246,522.54-949,060.22
7203 4636267HORTICULTURAL -228,265.75228265.750
8203 4785763REPL-EQUIPMENT9,065.759065.750
9206 2100001ENCUMBRANCES-830,704.98
10206 12ST03692416/17 PAVEMENT9,091.509091.50
11206 12ST03792417/18 PAVEMENT821,613.48821613.480
12361 2100001ENCUMBRANCES-221,226.50
13361 4541901DMAPS (TPM/TTPM)129,750.00129,750.00
14361 4541901SMAPS (TPM/TTPM)91,476.5091476.50
15999 9999999SUSPENSE ACCOUN124,946.14-23245.75148,191.89
16Fnd 101 GENFund-8,294.03-8,294.03
17Fnd 207 PROFund-4,731.00-4,731.00
18Fnd 212 MEAFund13,025.0313,025.03
Sheet1


Output:
Book1
ABCDEFGH
1
2203 4636267HORTICULTURAL -228265.75228265.750
3203 4785763REPL-EQUIPMENT9065.759065.750
4361 4541901DMAPS (TPM/TTPM)129750129750
5361 4541901SMAPS (TPM/TTPM)91476.591476.50
6999 9999999SUSPENSE ACCOUN124946.14-23245.75148191.89
7
8101 12ST03792417/18 PAVEMENT11560.4311560.430
9203 12ST0389242019 PAVE 9191.049191.040
10206 12ST03692416/17 PAVEMENT9091.59091.50
11206 12ST03792417/18 PAVEMENT821613.48821613.480
12Total1434960.591157018.7277941.89
13Not Balanced
14
15Fnd 101 GENFund-8294.03
16Fnd 207 PROFund-4731
17Fnd 212 MEAFund13025.03
18Total0
19Balanced
20
21101 2100001ENCUMBRANCES-11560.43
22101 2100002VOUCHERS PAYABL-124946.14
23203 2100001ENCUMBRANCES-246522.54
24206 2100001ENCUMBRANCES-830704.98
25361 2100001ENCUMBRANCES-221226.5
26Total-1434960.59
27Not Balanced
Sheet2
 
Last edited:
Upvote 0
I wondering if you're test reproduces the same issue?

So I ran the code as
VBA Code:
If wtot + .Value = 0 Then .Offset(1).Value = "Balanced" Else .Offset(1).Value = .Value + wtot

and the output for that cell was
1581010725096.png
so it seems it might be a rounding error?
 
Last edited:
Upvote 0
I'm not sure if its the best idea fundamentally to round the absolute value of wtot + .Value but wouldn't that solve the issue?

I wondering if you're test reproduces the same issue?

So I ran the code as
VBA Code:
If wtot + .Value = 0 Then .Offset(1).Value = "Balanced" Else .Offset(1).Value = .Value + wtot

and the output for that cell was View attachment 5942 so it seems it might be a rounding error?
 
Upvote 0
I add this and works:
VBA Code:
If Val(wtot) + Val(.Value) = 0

Try this:

VBA Code:
Option Explicit

Dim b As Variant, c As Variant, d As Variant, e As Variant, wtot As Double

Sub Separate_sections()
  Dim a As Variant, sh1 As Worksheet, sh2 As Worksheet
  Dim i As Long, nb As Long, nc As Long, nd As Long, ne As Long
  Dim lr As Long, lr2 As Long, m
  
  Application.ScreenUpdating = False
  
  Set sh1 = Sheets("Sheet1")
  Set sh2 = Sheets("Sheet2")
  sh2.Rows("2:" & Rows.Count).ClearContents
  a = sh1.Range("A2:H" & sh1.Range("A" & Rows.Count).End(xlUp).Row).Value2
  
  ReDim b(1 To UBound(a), 1 To 8)
  ReDim c(1 To UBound(a), 1 To 8)
  ReDim d(1 To UBound(a), 1 To 6)
  ReDim e(1 To UBound(a), 1 To 6)
  
  For i = 1 To UBound(a)
    m = Split(a(i, 1), " ")(1)
    m = Left(m, Len(m) - 1)
    Select Case True
      Case UCase(Left(a(i, 2), 3)) = UCase("Enc"), UCase(Left(a(i, 2), 3)) = UCase("Vou")
        ne = ne + 1
        Call fillarray(a, i, e, ne, 6)
      Case UCase(Left(a(i, 1), 3)) = UCase("Fnd")
        nd = nd + 1
        Call fillarray(a, i, d, nd, 6)
      Case IsNumeric(m)
        nb = nb + 1
        Call fillarray(a, i, b, nb, 8) '
      Case Else
        nc = nc + 1
        Call fillarray(a, i, c, nc, 8)
    End Select
  Next
  
  If nb > 0 Then Call FillSheet(sh2, 2, 2, b, nb, 8, False, "H", 0)
  
  lr = sh2.Range("A" & Rows.Count).End(xlUp).Row + 2
  If nc > 0 Then Call FillSheet(sh2, 2, lr, c, nc, 8, True, "H", 1)
  
  lr = sh2.Range("A" & Rows.Count).End(xlUp).Row + 4
  If nd > 0 Then Call FillSheet(sh2, lr, lr, d, nd, 6, True, "F", 0)
  
  lr = sh2.Range("A" & Rows.Count).End(xlUp).Row + 4
  If ne > 0 Then Call FillSheet(sh2, lr, lr, e, ne, 6, True, "F", 2)
End Sub

Sub FillSheet(sh2 As Worksheet, ini As Long, nRow As Long, ary As Variant, _
              nx As Long, col As Long, bFormula As Boolean, cf As String, x As Long)
  Dim lr2 As Long
  sh2.Range("A" & nRow).Resize(nx, col).Value = ary
  sh2.Range("A" & nRow).Resize(nx, col).sort key1:=sh2.Range("A" & nRow), order1:=xlAscending, Header:=xlNo
  If bFormula Then
    lr2 = sh2.Range("A" & Rows.Count).End(xlUp).Row + 1
    With sh2.Range("F" & lr2 & ":" & cf & lr2)
      .Formula = "=SUM(F" & ini & ":F" & lr2 - 1 & ")"
      .Value = .Value
      If x = 1 Then
        wtot = sh2.Range("F" & lr2).Value
      End If
    End With
    sh2.Range("B" & lr2).Value = "Total"
    With sh2.Range(cf & lr2)
      Select Case x
        Case 0, 1
          If .Value = 0 Then .Offset(1).Value = "Balanced" Else .Offset(1).Value = "Not Balanced"
        Case Else
          If Val(wtot) + Val(.Value) = 0 Then .Offset(1).Value = "Balanced" Else .Offset(1).Value = "Not Balanced"
      End Select
    End With
  End If
End Sub

Sub fillarray(a As Variant, i As Long, Arr As Variant, n As Long, m As Long)
  Dim j As Long
  For j = 1 To m
    Arr(n, j) = a(i, j)
  Next
End Sub
 
Upvote 0
I guess then everything is covered. Let me know any questions.
I'm glad to help you.
 
Upvote 0

Forum statistics

Threads
1,214,411
Messages
6,119,356
Members
448,888
Latest member
Arle8907

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