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

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this

Your data on Sheet1, results on Sheet2

Put everything in a module and run the "Separate_sections" macro

VBA Code:
Option Explicit

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

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
  
  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)
    Select Case True
      Case a(i, 2) = "Enc"
        ne = ne + 1
        Call fillarray(a, i, e, ne, 6)
      Case Left(a(i, 1), 3) = "Fnd"
        nd = nd + 1
        Call fillarray(a, i, d, nd, 6)
      Case IsNumeric(Split(a(i, 1), " ")(1))
        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
  
  Call FillSheet(sh2, 2, 2, b, nb, 8, False, "H")
  
  lr = sh2.Range("A" & Rows.Count).End(xlUp).Row + 2
  Call FillSheet(sh2, 2, lr, c, nc, 8, True, "H")
  
  lr = sh2.Range("A" & Rows.Count).End(xlUp).Row + 3
  Call FillSheet(sh2, lr, lr, d, nd, 6, True, "F")
  
  lr = sh2.Range("A" & Rows.Count).End(xlUp).Row + 3
  Call FillSheet(sh2, lr, lr, e, ne, 6, True, "F")
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)
  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
    End With
    sh2.Range("I" & lr2).Value = "Total"
  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
If Sheet2 doesn't exist in the document, how can we code it so that it will open a new sheet titled Sheet2 so the macro can work?

I can see how separate sheets can help with the data sort but I really only want this one sheet. Can we code the deletion of the Sheet1 with the original data once we're done?

Once Sheet1 is deleted, can we rename Sheet2 to Sheet1? (with code)
 
Last edited:
Upvote 0
Once Sheet1 is deleted, can we rename Sheet2 to Sheet1? (with code)

Of course, I can do that, but first you could try the macro as it is.
I understand that you want everything on one sheet, but for the purpose of the test, you can create the sheet2 and run the macro.
Review the results and comment.
 
Upvote 0
I wrote some code to add the sheet and delete the previous one.

I ran your code and I'm getting a Runtime Error 1004. There is some other code in the module that I'm using with my macro but the variable seem to be different.

1580864528696.png
 
Upvote 0
okay, I just moved the nx As Long line up and it worked.

With my delete sheet code, it's asking me to confirm I want to delete it. Can I bypass that?
 
Upvote 0
You could try the macro without modifying it. only for testing purposes, then I help you with the creation of the sheet, that is very simple, the complicated thing is to separate the data. Create sheet2 manually and test.
 
Upvote 0
I figured out what the issue was. Your coding was fine. The dataset I was working with didn't have exactly the 'Enc' term. Solved.

I just used Application.DisplayAlerts = False so that I don't get sheet deletion notification.

Thanks for your coding help!!!
 
Upvote 0
Im glad to help you, thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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