Is there a way of speeding up these codes? It takes about 2 minutes to run this code with 6k rows.

Coyotex3

Active Member
Joined
Dec 12, 2021
Messages
496
Office Version
  1. 365
Platform
  1. Windows
VBA Code:
Sub OrderFormatting()
    Range("A1:P40000").Select
    With Selection
        .Hyperlinks.Delete
        With Selection
    .Borders.LineStyle = xlNone
            With .Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
            End With
        End With
    End With
    Range("A1:P40000").UnMerge
End Sub

Sub CopyDeleteInsertSortInsertDelete()
    Dim x   As Long, LastRow    As Long
    Dim rng As Range
    
    Range("D:E,K:O").Delete
    Columns("A:O").EntireColumn.AutoFit
    
    For Each rng In Range("D8", Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
        rng.Offset(-1, -3).ReSize(rng.Count + 1, 2).FillDown
    Next rng
End Sub

Sub DeleteBlankRows()

With Range("D6:D" & Cells(Rows.Count, "B").End(xlUp).Row)
    .Value = .Value
    .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    
End With

End Sub

Sub EndFormatting()

    Rows(5).Insert

    Range("A6").CurrentRegion.Sort Range("D6"), xlAscending, Range("A6"), , xlAscending, Header:=xlYes

    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

    Rows("5:5").EntireRow.Delete

    Rows("6:8").EntireRow.Delete
End Sub

Sub UsualS()
  Dim LR As Long, i As Long
  
  Application.ScreenUpdating = False
  LR = Range("A" & Rows.Count).End(xlUp).Row
  For i = LR To 1 Step -1
    If Range("A" & i) = "01" And (Range("D" & i).Value = "bronx" Or Range("D" & i).Value = "brooklyn" Or Range("D" & i).Value = "queens") Then Rows(i).Delete
  Next i
  Application.ScreenUpdating = True
End Sub

Sub RangeLabels11()
Application.ScreenUpdating = False
Dim area As Range, lngRentRow&
For Each area In Columns(1).SpecialCells(2).Areas
lngRentRow = area.Row + area.Rows.Count
With Cells(lngRentRow, 7)
.Value = "Rent"
.Font.Bold = True
.Interior.Color = 5296274
With .Offset(1)
.Value = "Cash"
.Font.Bold = True
.Interior.Color = 65535
End With
End With
Next area
Columns(7).AutoFit
Application.ScreenUpdating = True

ActiveCell.Columns("G:G").EntireColumn.EntireColumn.AutoFit
End Sub

Sub Sorting()
   Dim UsdRws As Long
   
   UsdRws = Range("A" & Rows.Count).End(xlUp).Row
   With Range("A:A").SpecialCells(xlConstants)
      With Range(.Areas(.Areas.Count - 1), Range("A" & UsdRws)).ReSize(, 9)
         .Sort .Range("A1"), xlAscending, .Range("G1"), , xlAscending, Header:=xlNo
      End With
   End With
End Sub

Sub States()
Dim States(4) As Variant
Dim i As Long

States(1) = "nc"
States(2) = "sc"
States(3) = "wa"
States(4) = "ga"

With ActiveSheet
    For i = 1 To 4
        .AutoFilterMode = False
        With Range("d1", Range("d" & Rows.Count).End(xlUp))
            .AutoFilter 1, "*" & States(i) & "*"
            On Error Resume Next
            .Offset(1).SpecialCells(12).EntireRow.Delete
        End With
        .AutoFilterMode = False
    Next i
End With
End Sub

Sub UsualS565()
  Dim LR As Long, i As Long
  
  Application.ScreenUpdating = False
  LR = Range("A" & Rows.Count).End(xlUp).Row
  For i = LR To 1 Step -1
    If Range("A" & i) = "01" And (Range("D" & i).Value = "bronx" Or Range("D" & i).Value = "brooklyn" Or Range("D" & i).Value = "queens") Then Rows(i).Delete
  Next i
  Application.ScreenUpdating = True
End Sub
Sub GetLastRow()
Range("A" & Rows.Count).End(xlUp).Select
 Range(Selection, Selection.End(xlUp)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.ClearContents
End Sub

I have a code which calls all of these macros. Would be great if they could be consolidated and tweaked to make faster. Thank you.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Yes there is a way of speeding up your code, but to do it properly it needs a complete rewrite. The critical thing to understand as to why your code is so slow; is that VBA is very fast when it is working in memory but every access to the workhseet to read from , write to or format the worksheet is very slow. So the way speed up your code is to read the entire worksheet into memory, then manipulate the data in memory, e.g loop through looking for things and delete bits of the data add blank rows, etc etc , then writing it all out in one go. To do this you need to load the entrie worksheet into a variant array, and then copy the data into another output array and finally write the output array back to the worksheet. You can't avoid the time taken to format bits of the worksheet, so you do have to do that bit. This is likely to be 1000 times faster i.e less than 1 second to run
 
Upvote 0
Yes there is a way of speeding up your code, but to do it properly it needs a complete rewrite. The critical thing to understand as to why your code is so slow; is that VBA is very fast when it is working in memory but every access to the workhseet to read from , write to or format the worksheet is very slow. So the way speed up your code is to read the entire worksheet into memory, then manipulate the data in memory, e.g loop through looking for things and delete bits of the data add blank rows, etc etc , then writing it all out in one go. To do this you need to load the entrie worksheet into a variant array, and then copy the data into another output array and finally write the output array back to the worksheet. You can't avoid the time taken to format bits of the worksheet, so you do have to do that bit. This is likely to be 1000 times faster i.e less than 1 second to run
Hi, thank you so much for the reply. I have no idea how to do this, I’m fairly new to VBA.
 
Upvote 0
Sheet will Originally look like this:
Automation(19128).xlsx
ABCDEFGHIJKLMNOP
1Report
2User
3Date:12/01
4Time: 12:45
5OrderNameDescriptionMiscPOCityDateInfoOrder #123456Other Info
6OrdersDesc.123456
7#
810John0.00
9NoneN/A12354bronx10/10/2020Non-Available1500.00500.000.000.000.000.00789546521
10Total John500.00500.000.000.000.000.00
11
1211Jane0.00
13NoneN/A5652brooklyn9/10/2020Non-Available2750.00750.000.000.000.000.00654789546
14Total Jane750.00750.000.000.000.000.00
15
1615Jack0.00
17NoneN/A41421bronx9/21/2020Non-Available3850.00850.000.000.000.000.001256987569
18NoneN/A41421manhattan9/21/2020Non-Available4850.00850.000.000.000.000.001256987569
19NoneN/A41421nc029/21/2020Non-Available5850.00850.000.000.000.000.001256987569
20NoneN/A41421nc019/21/2020Non-Available6850.00850.000.000.000.000.001256987569
21Total Jack850.00850.000.000.000.000.00
22
23Grand Total 2,100.002,100.000.000.000.000.00
24
Sheet9


These first 3 macros:
VBA Code:
Sub OrderFormatting()
    Range("A1:P40000").Select
    With Selection
        .Hyperlinks.Delete
        With Selection
    .Borders.LineStyle = xlNone
            With .Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
            End With
        End With
    End With
    Range("A1:P40000").UnMerge
End Sub

Sub CopyDeleteInsertSortInsertDelete()
    Dim x   As Long, LastRow    As Long
    Dim rng As Range
    
    Range("D:E,K:O").Delete
    Columns("A:O").EntireColumn.AutoFit
    
    For Each rng In Range("D8", Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
        rng.Offset(-1, -3).ReSize(rng.Count + 1, 2).FillDown
    Next rng
End Sub

Sub DeleteBlankRows()

With Range("D6:D" & Cells(Rows.Count, "B").End(xlUp).Row)
    .Value = .Value
    .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    
End With

End Sub

Get me this:

Automation(19128).xlsx
ABCDEFGHI
1Report
2User
3Date:12/01
4Time: 12:45
5OrderNameDescriptionCityDateInfoOrder #1Other Info
610JohnNonebronx10/10/2020Non-Available1500.00789546521
711JaneNonebrooklyn9/10/2020Non-Available2750.00654789546
815JackNonebronx9/21/2020Non-Available3850.001256987569
915JackNonemanhattan9/21/2020Non-Available4850.001256987569
1015JackNonenc029/21/2020Non-Available5850.001256987569
1115JackNonenc019/21/2020Non-Available6850.001256987569
12Total Jack850.00
13
14Grand Total 2,100.00
15
Sheet10


Then these next four subs:

VBA Code:
Sub EndFormatting()

    Rows(5).Insert

    Range("A6").CurrentRegion.Sort Range("D6"), xlAscending, Range("A6"), , xlAscending, Header:=xlYes

    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

    Rows("5:5").EntireRow.Delete

    Rows("6:8").EntireRow.Delete
End Sub

Sub UsualS()
  Dim LR As Long, i As Long
  
  Application.ScreenUpdating = False
  LR = Range("A" & Rows.Count).End(xlUp).Row
  For i = LR To 1 Step -1
    If Range("A" & i) = "10" And (Range("D" & i).Value = "bronx" Or Range("D" & i).Value = "brooklyn" Or Range("D" & i).Value = "queens") Then Rows(i).Delete
  Next i
  Application.ScreenUpdating = True
End Sub

Sub GetLastRow()
Range("A" & Rows.Count).End(xlUp).Select
 Range(Selection, Selection.End(xlUp)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.ClearContents
End Sub

Sub Sorting()
   Dim UsdRws As Long
   
   UsdRws = Range("A" & Rows.Count).End(xlUp).Row
   With Range("A:A").SpecialCells(xlConstants)
      With Range(.Areas(.Areas.Count - 1), Range("A" & UsdRws)).Resize(, 9)
         .Sort .Range("A1"), xlAscending, .Range("G1"), , xlAscending, Header:=xlNo
      End With
   End With
End Sub

To get me this:

Automation(19128).xlsx
ABCDEFGHIJ
1Report
2User
3Date:12/01
4Time: 12:45
5OrderNameDescriptionCityDateInfoOrder #1Other Info
615JackNonebronx9/21/2020Non-Available3850.001256987569
7
8
9
1011JaneNonebrooklyn9/10/2020Non-Available2750.00654789546
11
12
13
1415JackNonemanhattan9/21/2020Non-Available4850.001256987569
15
16
17
1815JackNonenc029/21/2020Non-Available5850.001256987569
1915JackNonenc019/21/2020Non-Available6850.001256987569
20
Sheet10


And finally this sub
VBA Code:
Sub RangeLabels11()
Application.ScreenUpdating = False
Dim area As Range, lngRentRow&
For Each area In Columns(1).SpecialCells(2).Areas
lngRentRow = area.Row + area.Rows.Count
With Cells(lngRentRow, 7)
.Value = "Rent"
.Font.Bold = True
.Interior.Color = 5296274
With .Offset(1)
.Value = "Cash"
.Font.Bold = True
.Interior.Color = 65535
End With
End With
Next area
Columns(7).AutoFit
Application.ScreenUpdating = True

ActiveCell.Columns("G:G").EntireColumn.EntireColumn.AutoFit
End Sub

To get this final product:

Automation(19128).xlsx
ABCDEFGHIJ
1Report
2User
3Date:12/01
4Time: 12:45
5OrderNameDescriptionCityDateInfoOrder #1Other Info
615JackNonebronx9/21/2020Non-Available3850.001256987569
7Rent
8Cash
9
1011JaneNonebrooklyn9/10/2020Non-Available2750.00654789546
11Rent
12Cash
13
1415JackNonemanhattan9/21/2020Non-Available4850.001256987569
15Rent
16Cash
17
1815JackNonenc029/21/2020Non-Available5850.001256987569
1915JackNonenc019/21/2020Non-Available6850.001256987569
20Rent
21Cash
22
Sheet10


Those are the subs in order (left one out) would love to learn a more efficient way to arrive at the same end result.
 
Upvote 0
I basically took your code and combined into one macro with minor modifications. The macro runs quite quickly. Give it a try.
VBA Code:
Sub FormatData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, x As Long, rng As Range, LR As Long, i As Long, UsdRws As Long, area As Range, lngRentRow&
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With Range("A1:P" & LastRow)
        .Hyperlinks.Delete
        .Borders.LineStyle = xlNone
        .Interior.Pattern = xlNone
        .UnMerge
    End With
    Range("D:E,K:O").Delete
    Columns("A:I").EntireColumn.AutoFit
    For Each rng In Range("D8", Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
        rng.Offset(-1, -3).Resize(rng.Count + 1, 2).FillDown
    Next rng
    With Range("D6:D" & Cells(Rows.Count, "B").End(xlUp).Row)
        .Value = .Value
        .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End With
    Rows(5).Insert
    Range("A6").CurrentRegion.Sort Range("D6"), xlAscending, Range("A6"), , xlAscending, Header:=xlYes
    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
    Rows("5:5").EntireRow.Delete
    Rows("6:8").EntireRow.Delete
    LR = Range("A" & Rows.Count).End(xlUp).Row
    For i = LR To 1 Step -1
        If Range("A" & i) = "10" And (Range("D" & i).Value = "bronx" Or Range("D" & i).Value = "brooklyn" Or Range("D" & i).Value = "queens") Then Rows(i).Delete
    Next i
    Range("A" & Rows.Count).End(xlUp).Select
    Range(Selection, Selection.End(xlUp)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.ClearContents
    UsdRws = Range("A" & Rows.Count).End(xlUp).Row
    With Range("A:A").SpecialCells(xlConstants)
        With Range(.Areas(.Areas.Count - 1), Range("A" & UsdRws)).Resize(, 9)
           .Sort .Range("A1"), xlAscending, .Range("G1"), , xlAscending, Header:=xlNo
        End With
    End With
    For Each area In Columns(1).SpecialCells(2).Areas
        lngRentRow = area.Row + area.Rows.Count
        With Cells(lngRentRow, 7)
            .Value = "Rent"
            .Font.Bold = True
            .Interior.Color = 5296274
            With .Offset(1)
                .Value = "Cash"
                .Font.Bold = True
                .Interior.Color = 65535
            End With
        End With
    Next area
    Columns(7).AutoFit
    Application.ScreenUpdating = True
    ActiveCell.Columns("G:G").EntireColumn.EntireColumn.AutoFit
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
I basically took your code and combined into one macro with minor modifications. The macro runs quite quickly. Give it a try.
VBA Code:
Sub FormatData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, x As Long, rng As Range, LR As Long, i As Long, UsdRws As Long, area As Range, lngRentRow&
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With Range("A1:P" & LastRow)
        .Hyperlinks.Delete
        .Borders.LineStyle = xlNone
        .Interior.Pattern = xlNone
        .UnMerge
    End With
    Range("D:E,K:O").Delete
    Columns("A:I").EntireColumn.AutoFit
    For Each rng In Range("D8", Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
        rng.Offset(-1, -3).Resize(rng.Count + 1, 2).FillDown
    Next rng
    With Range("D6:D" & Cells(Rows.Count, "B").End(xlUp).Row)
        .Value = .Value
        .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End With
    Rows(5).Insert
    Range("A6").CurrentRegion.Sort Range("D6"), xlAscending, Range("A6"), , xlAscending, Header:=xlYes
    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
    Rows("5:5").EntireRow.Delete
    Rows("6:8").EntireRow.Delete
    LR = Range("A" & Rows.Count).End(xlUp).Row
    For i = LR To 1 Step -1
        If Range("A" & i) = "10" And (Range("D" & i).Value = "bronx" Or Range("D" & i).Value = "brooklyn" Or Range("D" & i).Value = "queens") Then Rows(i).Delete
    Next i
    Range("A" & Rows.Count).End(xlUp).Select
    Range(Selection, Selection.End(xlUp)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.ClearContents
    UsdRws = Range("A" & Rows.Count).End(xlUp).Row
    With Range("A:A").SpecialCells(xlConstants)
        With Range(.Areas(.Areas.Count - 1), Range("A" & UsdRws)).Resize(, 9)
           .Sort .Range("A1"), xlAscending, .Range("G1"), , xlAscending, Header:=xlNo
        End With
    End With
    For Each area In Columns(1).SpecialCells(2).Areas
        lngRentRow = area.Row + area.Rows.Count
        With Cells(lngRentRow, 7)
            .Value = "Rent"
            .Font.Bold = True
            .Interior.Color = 5296274
            With .Offset(1)
                .Value = "Cash"
                .Font.Bold = True
                .Interior.Color = 65535
            End With
        End With
    Next area
    Columns(7).AutoFit
    Application.ScreenUpdating = True
    ActiveCell.Columns("G:G").EntireColumn.EntireColumn.AutoFit
    Application.ScreenUpdating = True
End Sub
Mumps, THANK YOU!! This one is faster than my previous one. This one did 8k rows in about 65 seconds.
 
Upvote 0
@mumps follow up question,
@Coyotex3 what is the order that you run the sub routines that you mentioned in?
It would be in this order

VBA Code:
Sub OrderFormatting()
    Range("A1:P40000").Select
    With Selection
        .Hyperlinks.Delete
        With Selection
    .Borders.LineStyle = xlNone
            With .Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
            End With
        End With
    End With
    Range("A1:P40000").UnMerge
End Sub

Sub CopyDeleteInsertSortInsertDelete()
    Dim x   As Long, LastRow    As Long
    Dim rng As Range
    
    Range("D:E,K:O").Delete
    Columns("A:O").EntireColumn.AutoFit
    
    For Each rng In Range("D8", Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
        rng.Offset(-1, -3).ReSize(rng.Count + 1, 2).FillDown
    Next rng
End Sub

Sub DeleteBlankRows()

With Range("D6:D" & Cells(Rows.Count, "B").End(xlUp).Row)
    .Value = .Value
    .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    
End With

End Sub

Sub EndFormatting()

    Rows(5).Insert

    Range("A6").CurrentRegion.Sort Range("D6"), xlAscending, Range("A6"), , xlAscending, Header:=xlYes

    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

    Rows("5:5").EntireRow.Delete

    Rows("6:8").EntireRow.Delete
End Sub

Sub UsualS()
  Dim LR As Long, i As Long
  
  Application.ScreenUpdating = False
  LR = Range("A" & Rows.Count).End(xlUp).Row
  For i = LR To 1 Step -1
    If Range("A" & i) = "10" And (Range("D" & i).Value = "bronx" Or Range("D" & i).Value = "brooklyn" Or Range("D" & i).Value = "queens") Then Rows(i).Delete
  Next i
  Application.ScreenUpdating = True
End Sub

Sub GetLastRow()
Range("A" & Rows.Count).End(xlUp).Select
 Range(Selection, Selection.End(xlUp)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.ClearContents
End Sub

Sub Sorting()
   Dim UsdRws As Long
   
   UsdRws = Range("A" & Rows.Count).End(xlUp).Row
   With Range("A:A").SpecialCells(xlConstants)
      With Range(.Areas(.Areas.Count - 1), Range("A" & UsdRws)).Resize(, 9)
         .Sort .Range("A1"), xlAscending, .Range("G1"), , xlAscending, Header:=xlNo
      End With
   End With
End Sub

Sub RangeLabels11()
Application.ScreenUpdating = False
Dim area As Range, lngRentRow&
For Each area In Columns(1).SpecialCells(2).Areas
lngRentRow = area.Row + area.Rows.Count
With Cells(lngRentRow, 7)
.Value = "Rent"
.Font.Bold = True
.Interior.Color = 5296274
With .Offset(1)
.Value = "Cash"
.Font.Bold = True
.Interior.Color = 65535
End With
End With
Next area
Columns(7).AutoFit
Application.ScreenUpdating = True

ActiveCell.Columns("G:G").EntireColumn.EntireColumn.AutoFit
End Sub
 
Upvote 0
See if this version makes any difference:
VBA Code:
Sub FormatData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, x As Long, rng As Range, LR As Long, i As Long, UsdRws As Long, area As Range, lngRentRow&
    Dim v As Variant, ii As Long, Val As String
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With Range("A1:P" & LastRow)
        .Hyperlinks.Delete
        .Borders.LineStyle = xlNone
        .Interior.Pattern = xlNone
        .UnMerge
    End With
    Range("D:E,K:O").Delete
    Columns("A:I").EntireColumn.AutoFit
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    v = Range("A8:A" & LastRow - 3).Resize(, 9).Value
    For ii = LBound(v) To UBound(v)
        If v(ii, 2) <> "" Then
            Range("A" & ii + 7).Resize(Range("C" & ii + 8).CurrentRegion.Rows.Count - 1, 2) = Array(v(ii, 1), v(ii, 2))
        End If
    Next ii
    With Range("D6:D" & Cells(Rows.Count, "B").End(xlUp).Row)
        .Value = .Value
        .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End With
    Rows(5).Insert
    Range("A6").CurrentRegion.Sort Range("D6"), xlAscending, Range("A6"), , xlAscending, Header:=xlYes
    Const DataCol As String = "D"
    Const StartRow = 7
    LastRow = Cells(Rows.Count, DataCol).End(xlUp).Row
    v = Range("A7:A" & LastRow).Resize(, 9).Value
    For ii = UBound(v) To LBound(v) Step -1
        If ii > 1 Then
            If v(ii, 4) <> v(ii - 1, 4) Then
                Range(DataCol & ii + 6 & ":" & DataCol & ii + 8).EntireRow.Insert
            End If
        Else
            If v(ii, 4) <> v(ii, 4) Then
                Range(DataCol & ii + 6 & ":" & DataCol & ii + 8).EntireRow.Insert
            End If
        End If
    Next ii
    Rows(5).Delete
    LR = Range("A" & Rows.Count).End(xlUp).Row
    v = Range("A6:A" & LR - 3).Resize(, 9).Value
    For ii = UBound(v) To LBound(v) Step -1
        If v(ii, 1) <> "" Then
            Val = "bronx brooklyn queens"
            If v(ii, 1) = "10" And InStr(Val, v(ii, 4)) > 0 Then
                Rows(ii + 5).Delete
            End If
        End If
    Next ii
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A" & LastRow - 2).Resize(3).EntireRow.Delete
    UsdRws = Range("A" & Rows.Count).End(xlUp).Row
    With Range("A:A").SpecialCells(xlConstants)
        With Range(.Areas(.Areas.Count - 1), Range("A" & UsdRws)).Resize(, 9)
           .Sort .Range("A1"), xlAscending, .Range("G1"), , xlAscending, Header:=xlNo
        End With
    End With
    For Each area In Columns(1).SpecialCells(2).Areas
        lngRentRow = area.Row + area.Rows.Count
        With Cells(lngRentRow, 7)
            .Value = "Rent"
            .Font.Bold = True
            .Interior.Color = 5296274
            With .Offset(1)
                .Value = "Cash"
                .Font.Bold = True
                .Interior.Color = 65535
            End With
        End With
    Next area
    Columns(7).AutoFit
    Application.ScreenUpdating = True
    ActiveCell.Columns("G:G").EntireColumn.EntireColumn.AutoFit
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
See if this version makes any difference:
VBA Code:
Sub FormatData()
    Application.ScreenUpdating = False
    Dim LastRow As Long, x As Long, rng As Range, LR As Long, i As Long, UsdRws As Long, area As Range, lngRentRow&
    Dim v As Variant, ii As Long, Val As String
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With Range("A1:P" & LastRow)
        .Hyperlinks.Delete
        .Borders.LineStyle = xlNone
        .Interior.Pattern = xlNone
        .UnMerge
    End With
    Range("D:E,K:O").Delete
    Columns("A:I").EntireColumn.AutoFit
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    v = Range("A8:A" & LastRow - 3).Resize(, 9).Value
    For ii = LBound(v) To UBound(v)
        If v(ii, 2) <> "" Then
            Range("A" & ii + 7).Resize(Range("C" & ii + 8).CurrentRegion.Rows.Count - 1, 2) = Array(v(ii, 1), v(ii, 2))
        End If
    Next ii
    With Range("D6:D" & Cells(Rows.Count, "B").End(xlUp).Row)
        .Value = .Value
        .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End With
    Rows(5).Insert
    Range("A6").CurrentRegion.Sort Range("D6"), xlAscending, Range("A6"), , xlAscending, Header:=xlYes
    Const DataCol As String = "D"
    Const StartRow = 7
    LastRow = Cells(Rows.Count, DataCol).End(xlUp).Row
    v = Range("A7:A" & LastRow).Resize(, 9).Value
    For ii = UBound(v) To LBound(v) Step -1
        If ii > 1 Then
            If v(ii, 4) <> v(ii - 1, 4) Then
                Range(DataCol & ii + 6 & ":" & DataCol & ii + 8).EntireRow.Insert
            End If
        Else
            If v(ii, 4) <> v(ii, 4) Then
                Range(DataCol & ii + 6 & ":" & DataCol & ii + 8).EntireRow.Insert
            End If
        End If
    Next ii
    Rows(5).Delete
    LR = Range("A" & Rows.Count).End(xlUp).Row
    v = Range("A6:A" & LR - 3).Resize(, 9).Value
    For ii = UBound(v) To LBound(v) Step -1
        If v(ii, 1) <> "" Then
            Val = "bronx brooklyn queens"
            If v(ii, 1) = "10" And InStr(Val, v(ii, 4)) > 0 Then
                Rows(ii + 5).Delete
            End If
        End If
    Next ii
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("A" & LastRow - 2).Resize(3).EntireRow.Delete
    UsdRws = Range("A" & Rows.Count).End(xlUp).Row
    With Range("A:A").SpecialCells(xlConstants)
        With Range(.Areas(.Areas.Count - 1), Range("A" & UsdRws)).Resize(, 9)
           .Sort .Range("A1"), xlAscending, .Range("G1"), , xlAscending, Header:=xlNo
        End With
    End With
    For Each area In Columns(1).SpecialCells(2).Areas
        lngRentRow = area.Row + area.Rows.Count
        With Cells(lngRentRow, 7)
            .Value = "Rent"
            .Font.Bold = True
            .Interior.Color = 5296274
            With .Offset(1)
                .Value = "Cash"
                .Font.Bold = True
                .Interior.Color = 65535
            End With
        End With
    Next area
    Columns(7).AutoFit
    Application.ScreenUpdating = True
    ActiveCell.Columns("G:G").EntireColumn.EntireColumn.AutoFit
    Application.ScreenUpdating = True
End Sub
Mumps, thank you once again for looking into this. Oddly enough the first time I ran the sub it took about 90 seconds to finish(8k rows). I reran the macro again and it was able to do all 8k rows in less than 20 seconds on the second try, but I was only able to accomplish that once and now it is taking roughly 90 seconds again.
 
Upvote 0

Forum statistics

Threads
1,215,444
Messages
6,124,893
Members
449,194
Latest member
JayEggleton

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