Sorting Poducts VBA Running Slow

Voltz

New Member
Joined
May 6, 2014
Messages
26
Hello All,

Thanks for the Education you are providing.

I have been using the below code (originally created by Hiker95 in here) for the last 4 years now but now it is getting very slow to sort products, This is used to sort products entered randomly for each order with different products.
Each products consist of 2 rows, Product details and product notes

Time to sort 2K products (4k Rows) = 11 min

Excel File can be downloaded from my shared OneDrive link and it is macro free on xlsx version.


I'm on PC
Window 10
MS Office 365


I appreciate any help getting this running faster.

Thanks.

VBA Code:
Sub ReorgData_V6_AS_SENT_BY_HIKER95()
' hiker95, 12/01/2015, ME905348
Dim wjp As Worksheet, wsp As Worksheet
Dim a As Variant, i As Long
Dim o As Variant, j As Long
Dim lr As Long, lc As Long, c As Long, nsc As Long, r As Long
Application.ScreenUpdating = False
Set wjp = Sheets("JSH PRINT")
Set wsp = Sheets("SORTED PRODUCTS")
With wjp
  lr = .Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
  lc = 14
  .Range(.Cells(1, 1), .Cells(lr, lc)).MergeCells = False
  a = .Range(.Cells(1, 1), .Cells(lr, lc))
  ReDim o(1 To UBound(a, 1) / 2, 1 To UBound(a, 2) * 2)
End With
For i = 1 To lr Step 2
  If a(i, 1) = vbEmpty Or a(i, 2) = vbEmpty Then
    'do nothing
  Else
    j = j + 1
    For c = 1 To lc Step 1
      o(j, c) = a(i, c)
    Next c
    nsc = 15
    For c = 1 To lc Step 1
      o(j, nsc) = a(i + 1, c)
      nsc = nsc + 1
    Next c
  End If
Next i
With wsp
  .UsedRange.ClearContents
  .Cells(1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  .Range("A1:AB" & lr).Sort key1:=.Range("B1"), order1:=2
  Erase a: Erase o: j = 0
  a = .Range("A1:AB" & lr)
  lc = 28
  ReDim o(1 To (UBound(a, 1) * 2) + 10, 1 To UBound(a, 2) / 2)
  For i = 1 To lr
    j = j + 1
    For c = 1 To 14 Step 1
      o(j, c) = a(i, c)
    Next c
    j = j + 1
    For c = 15 To lc Step 1
      o(j, c - 14) = a(i, c)
    Next c
  Next i
  .Range("A1:AB" & lr).ClearContents
  .Range("A1").Resize(UBound(o, 1), UBound(o, 2)) = o
  .UsedRange.HorizontalAlignment = xlLeft
  .Activate
End With
Application.ScreenUpdating = True
End Sub


Original data:
Unsorted

1582743521647.png


Sorted Data:

1582743793522.png
 

Attachments

  • 1582742017388.png
    1582742017388.png
    81.7 KB · Views: 7
  • 1582742323196.png
    1582742323196.png
    66.4 KB · Views: 7

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,948
Members
449,198
Latest member
MhammadishaqKhan

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