Help with this custom sort and insert blank rows macro?

Coyotex3

Well-known Member
Joined
Dec 12, 2021
Messages
507
Office Version
  1. 365
Platform
  1. Windows
Hello, so I will have a variable range like this
Automation(19128).xlsx
ABCDEFGHIJ
1Report
2User
3Date:12/01
4Time: 12:45
5
6OrderNameDescriptionCityDateInfoOrder #1Other Info
715JackNonebrookyn9/21/2020Non-Available5850.001256987569
810JohnNonebronx10/10/2020Non-Available1500.00789546521
915JackNonebronx9/21/2020Non-Available3850.001256987569
1015JackNonebronx9/21/2020Non-Available6850.001256987569
1111JaneNonebrooklyn9/10/2020Non-Available2750.00654789546
1215JackNonequeens9/21/2020Non-Available4850.001256987569
13
Sheet1


And I will use these codes

VBA Code:
Sub CopySort()
Range("A6").CurrentRegion.Sort Range("D6"), xlAscending, Range("A6"), , xlAscending, Header:=x
Const DataCol As String = "D"
    Const StartRow = 6
    LastRow = Cells(Rows.Count, DataCol).End(xlUp).Row
    Application.ScreenUpdating = False
    For x = LastRow To StartRow + 1 Step -1
        If Cells(x, DataCol).Value <> Cells(x - 1, DataCol) Then Range(DataCol & x & ":" & DataCol & x + 2).EntireRow.Insert
    Next
End Sub
To get this

Automation(19128).xlsx
ABCDEFGHI
1Report
2User
3Date:12/01
4Time: 12:45
5
6OrderNameDescriptionCityDateInfoOrder #1Other Info
7
8
9
1010JohnNonebronx10/10/2020Non-Available1500.00789546521
1115JackNonebronx9/21/2020Non-Available3850.001256987569
1215JackNonebronx9/21/2020Non-Available6850.001256987569
13
14
15
1611JaneNonebrooklyn9/10/2020Non-Available2750.00654789546
17
18
19
2015JackNonebrookyn9/21/2020Non-Available5850.001256987569
21
22
23
2415JackNonequeens9/21/2020Non-Available4850.001256987569
25
Sheet1


On some of my ranges column D will not be an exact match and will be something like: Bronx01, Bronx02 or Bronx04 etc. I would like for the code to keep them together and not separate them and hopefully get something like this

Automation(19128).xlsx
ABCDEFGHI
1Report
2User
3Date:12/01
4Time: 12:45
5
6OrderNameDescriptionCityDateInfoOrder #1Other Info
7
8
9
1010JohnNonebronx0110/10/2020Non-Available1500.00789546521
1115JackNonebronx029/21/2020Non-Available3850.001256987569
1215JackNonebronx129/21/2020Non-Available6850.001256987569
13
14
15
1611JaneNonebrooklyn9/10/2020Non-Available2750.00654789546
17
18
19
2015JackNonebrookyn9/21/2020Non-Available5850.001256987569
21
22
23
2415JackNonequeens9/21/2020Non-Available4850.001256987569
25
Sheet1


And not this which is what I get when I run the code
Automation(19128).xlsx
ABCDEFGHI
1Report
2User
3Date:12/01
4Time: 12:45
5
6OrderNameDescriptionCityDateInfoOrder #1Other Info
7
8
9
1010JohnNonebronx0110/10/2020Non-Available1500.00789546521
11
12
13
1415JackNonebronx029/21/2020Non-Available3850.001256987569
15
16
17
1815JackNonebronx129/21/2020Non-Available6850.001256987569
19
20
21
2211JaneNonebrooklyn9/10/2020Non-Available2750.00654789546
23
24
25
2615JackNonebrookyn9/21/2020Non-Available5850.001256987569
27
28
29
3015JackNonequeens9/21/2020Non-Available4850.001256987569
Sheet1
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
VBA Code:
Sub Sorteer()
     With Range("TBL_Report").ListObject                        'your list
          With .Range
               .Sort .Cells(1, 10), xlDescending, Header:=xlYes     'sort on the new column "city2"
          End With

          a = .DataBodyRange.Columns(10).Value                  'read that column "city2"
          For i = UBound(a) - 1 To 1 Step -1                    'loop from 2nd last row to row 1
               If StrComp(a(i, 1), a(i + 1, 1), vbTextCompare) <> 0 Then     'if other city2 then
                    .DataBodyRange.Rows(i + 1 & ":" & i + 2).Insert Shift:=xlDown     'insert 2 empty rows
               End If
          Next
     End With
End Sub
There is a 2nd table where you add cities that make part of a greater city, for example your Bronx01, etc.
In the column "city2" this records get the same value
report.xlsm
ABCDEFGHIJKLMNOPQ
1Reportcitycity2
2Userbronx01bronx
3Date:12/01bronx02bronx
4Time: 12:45bronx03bronx
5bronx04bronx
6OrderNameDescriptionCityDateInfoOrder #1Other Infocity2brookynbrooklyn
715JackNonequeens9/21/2020Non-Available48501256987569queens
80
90
1011JaneNonebrooklyn9/10/2020Non-Available2750654789546brooklyn
1115JackNonebrookyn9/21/2020Non-Available58501256987569brooklyn
120
130
1410JohnNonebronx0110/10/2020Non-Available1500789546521bronx
1515JackNonebronx029/21/2020Non-Available38501256987569bronx
1615JackNonebronx9/21/2020Non-Available68501256987569bronx
17
18
Blad2
Cell Formulas
RangeFormula
J7:J16J7=IFERROR(VLOOKUP([@City],Tabel3,2,0),[@City])
 
Upvote 0
VBA Code:
Sub Sorteer()
     With Range("TBL_Report").ListObject                        'your list
          With .Range
               .Sort .Cells(1, 10), xlDescending, Header:=xlYes     'sort on the new column "city2"
          End With

          a = .DataBodyRange.Columns(10).Value                  'read that column "city2"
          For i = UBound(a) - 1 To 1 Step -1                    'loop from 2nd last row to row 1
               If StrComp(a(i, 1), a(i + 1, 1), vbTextCompare) <> 0 Then     'if other city2 then
                    .DataBodyRange.Rows(i + 1 & ":" & i + 2).Insert Shift:=xlDown     'insert 2 empty rows
               End If
          Next
     End With
End Sub
There is a 2nd table where you add cities that make part of a greater city, for example your Bronx01, etc.
In the column "city2" this records get the same value
report.xlsm
ABCDEFGHIJKLMNOPQ
1Reportcitycity2
2Userbronx01bronx
3Date:12/01bronx02bronx
4Time: 12:45bronx03bronx
5bronx04bronx
6OrderNameDescriptionCityDateInfoOrder #1Other Infocity2brookynbrooklyn
715JackNonequeens9/21/2020Non-Available48501256987569queens
80
90
1011JaneNonebrooklyn9/10/2020Non-Available2750654789546brooklyn
1115JackNonebrookyn9/21/2020Non-Available58501256987569brooklyn
120
130
1410JohnNonebronx0110/10/2020Non-Available1500789546521bronx
1515JackNonebronx029/21/2020Non-Available38501256987569bronx
1615JackNonebronx9/21/2020Non-Available68501256987569bronx
17
18
Blad2
Cell Formulas
RangeFormula
J7:J16J7=IFERROR(VLOOKUP([@City],Tabel3,2,0),[@City])
Thank you for the reply and the suggestion. The only issue is that these ranges will have more than 6k rows and many city variables. It might take a while to copy all the cities into the other table. One thing I will say is that the cities will always be separated by numbers. meaning it will always be something like bronx01 bronx10 bronx05, or brooklyn02 brooklyn10 brooklyn18, or 01manhattan02 manhattan04 etc. meaning the letters will always match, the numbers will be different.
 
Upvote 0
Meant to say 01manhattan 02manhattan 03manhattan etc***
 
Upvote 0
Meant to say 01manhattan 02manhattan 03manhattan etc***
in the formula of city2, make right left and left right


if the last 2 characters of the city are numeric, they are omitted, no 2nd table anymore.
VBA Code:
Sub Sorteer()
     Dim DBR
     With Range("TBL_Report").ListObject                        'your list
          With .Range
               .Sort .Cells(1, 10), xlAscending, Header:=xlYes  'sort on the new column "city2"
          End With

          a = .DataBodyRange.Columns(10).Value                  'read that column "city2"
          For i = UBound(a) - 1 To 1 Step -1                    'loop from 2nd last row to row 1
               If StrComp(a(i, 1), a(i + 1, 1), vbTextCompare) <> 0 Then     'if other city2 then
                    .DataBodyRange.Rows(i + 1 & ":" & i + 2).Insert Shift:=xlDown     'insert 2 empty rows
               End If
          Next

          On Error Resume Next
          s = "": s = .DataBodyRange.SpecialCells(xlCellTypeBlanks).Address     'address of all the blank cells in the databodyrange
          If Len(s) > 0 Then                                    'there are blank cells
               sp = Split(s, ",")                               'split the address
               Set c = .Parent.Range(sp(UBound(sp)))            'last area = empty rows at the bottom of the list
               Set DBR = .DataBodyRange
               If c.Columns.Count = DBR.Columns.Count - 1 Then  'that area : number of columns= 1 less than databodyrange
                    If DBR.Row + DBR.Rows.Count = c.Row + c.Rows.Count Then     'last row=same as last row DBR
                         DBR.Offset(c.Row - DBR.Row).Resize(DBR.Rows.Count + DBR.Row - c.Row).Delete     'delete that part of the listobject
                    End If
               End If
          End If
          On Error GoTo 0
     End With
End Sub
report.xlsm
ABCDEFGHIJK
1Report
2User
3Date:12/01
4Time: 12:45
5
6OrderNameDescriptionCityDateInfoOrder #1Other Infocity2
710JohnNonebronx0110/10/2020Non-Available1500789546521bronx
815JackNonebronx029/21/2020Non-Available38501256987569bronx
915JackNonebronx9/21/2020Non-Available68501256987569bronx
10zzz
11zzz
1211JaneNonebrooklyn9/10/2020Non-Available2750654789546brooklyn
13zzz
14zzz
1515JackNonebrookyn9/21/2020Non-Available58501256987569brookyn
16zzz
17zzz
1815JackNonequeens9/21/2020Non-Available48501256987569queens
19
20
21
Blad2
Cell Formulas
RangeFormula
J7:J18J7=IF([@City]="","zzz",LEFT([@City],LEN([@City])-2*(ISNUMBER(-RIGHT([@City],2)))))
 
Upvote 0
Meant to say 01manhattan 02manhattan 03manhattan etc***
in the formula of city2, make right left and left right


if the last 2 characters of the city are numeric, they are omitted, no 2nd table anymore.
VBA Code:
Sub Sorteer()
     Dim DBR
     With Range("TBL_Report").ListObject                        'your list
          With .Range
               .Sort .Cells(1, 10), xlAscending, Header:=xlYes  'sort on the new column "city2"
          End With

          a = .DataBodyRange.Columns(10).Value                  'read that column "city2"
          For i = UBound(a) - 1 To 1 Step -1                    'loop from 2nd last row to row 1
               If StrComp(a(i, 1), a(i + 1, 1), vbTextCompare) <> 0 Then     'if other city2 then
                    .DataBodyRange.Rows(i + 1 & ":" & i + 2).Insert Shift:=xlDown     'insert 2 empty rows
               End If
          Next

          On Error Resume Next
          s = "": s = .DataBodyRange.SpecialCells(xlCellTypeBlanks).Address     'address of all the blank cells in the databodyrange
          If Len(s) > 0 Then                                    'there are blank cells
               sp = Split(s, ",")                               'split the address
               Set c = .Parent.Range(sp(UBound(sp)))            'last area = empty rows at the bottom of the list
               Set DBR = .DataBodyRange
               If c.Columns.Count = DBR.Columns.Count - 1 Then  'that area : number of columns= 1 less than databodyrange
                    If DBR.Row + DBR.Rows.Count = c.Row + c.Rows.Count Then     'last row=same as last row DBR
                         DBR.Offset(c.Row - DBR.Row).Resize(DBR.Rows.Count + DBR.Row - c.Row).Delete     'delete that part of the listobject
                    End If
               End If
          End If
          On Error GoTo 0
     End With
End Sub
report.xlsm
ABCDEFGHIJK
1Report
2User
3Date:12/01
4Time: 12:45
5
6OrderNameDescriptionCityDateInfoOrder #1Other Infocity2
710JohnNonebronx0110/10/2020Non-Available1500789546521bronx
815JackNonebronx029/21/2020Non-Available38501256987569bronx
915JackNonebronx9/21/2020Non-Available68501256987569bronx
10zzz
11zzz
1211JaneNonebrooklyn9/10/2020Non-Available2750654789546brooklyn
13zzz
14zzz
1515JackNonebrookyn9/21/2020Non-Available58501256987569brookyn
16zzz
17zzz
1815JackNonequeens9/21/2020Non-Available48501256987569queens
19
20
21
Blad2
Cell Formulas
RangeFormula
J7:J18J7=IF([@City]="","zzz",LEFT([@City],LEN([@City])-2*(ISNUMBER(-RIGHT([@City],2)))))
Thank you. I'll test it out.
 
Upvote 0
Meant to say 01manhattan 02manhattan 03manhattan etc***
in the formula of city2, make right left and left right


if the last 2 characters of the city are numeric, they are omitted, no 2nd table anymore.
VBA Code:
Sub Sorteer()
     Dim DBR
     With Range("TBL_Report").ListObject                        'your list
          With .Range
               .Sort .Cells(1, 10), xlAscending, Header:=xlYes  'sort on the new column "city2"
          End With

          a = .DataBodyRange.Columns(10).Value                  'read that column "city2"
          For i = UBound(a) - 1 To 1 Step -1                    'loop from 2nd last row to row 1
               If StrComp(a(i, 1), a(i + 1, 1), vbTextCompare) <> 0 Then     'if other city2 then
                    .DataBodyRange.Rows(i + 1 & ":" & i + 2).Insert Shift:=xlDown     'insert 2 empty rows
               End If
          Next

          On Error Resume Next
          s = "": s = .DataBodyRange.SpecialCells(xlCellTypeBlanks).Address     'address of all the blank cells in the databodyrange
          If Len(s) > 0 Then                                    'there are blank cells
               sp = Split(s, ",")                               'split the address
               Set c = .Parent.Range(sp(UBound(sp)))            'last area = empty rows at the bottom of the list
               Set DBR = .DataBodyRange
               If c.Columns.Count = DBR.Columns.Count - 1 Then  'that area : number of columns= 1 less than databodyrange
                    If DBR.Row + DBR.Rows.Count = c.Row + c.Rows.Count Then     'last row=same as last row DBR
                         DBR.Offset(c.Row - DBR.Row).Resize(DBR.Rows.Count + DBR.Row - c.Row).Delete     'delete that part of the listobject
                    End If
               End If
          End If
          On Error GoTo 0
     End With
End Sub
report.xlsm
ABCDEFGHIJK
1Report
2User
3Date:12/01
4Time: 12:45
5
6OrderNameDescriptionCityDateInfoOrder #1Other Infocity2
710JohnNonebronx0110/10/2020Non-Available1500789546521bronx
815JackNonebronx029/21/2020Non-Available38501256987569bronx
915JackNonebronx9/21/2020Non-Available68501256987569bronx
10zzz
11zzz
1211JaneNonebrooklyn9/10/2020Non-Available2750654789546brooklyn
13zzz
14zzz
1515JackNonebrookyn9/21/2020Non-Available58501256987569brookyn
16zzz
17zzz
1815JackNonequeens9/21/2020Non-Available48501256987569queens
19
20
21
Blad2
Cell Formulas
RangeFormula
J7:J18J7=IF([@City]="","zzz",LEFT([@City],LEN([@City])-2*(ISNUMBER(-RIGHT([@City],2)))))
Getting run time error 1004 application defined or object defined error.
 
Upvote 0
you use option explicit ?
oh, no, i used a listobject "TBL_Report" and that doesn't exist in your workbook.
So, make that listobject or rename it to your situation.
 
Upvote 0
you use option explicit ?
oh, no, i used a listobject "TBL_Report" and that doesn't exist in your workbook.
So, make that listobject or rename it to your situation.
Yes now I'm getting a compile error variable not defined for a = .DataBody....
 
Upvote 0
erase the "option explicit" or add this in the Dim-line
VBA Code:
    Dim DBR, a, i, s, sp, c
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,790
Members
451,589
Latest member
Harold14

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