Using Sort within a loop

Dreamteam

New Member
Joined
Feb 22, 2018
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Hi guys,

I have written some code that looks at a database and filters on a user number and then pastes the data onto worksheets (named as the user numbers) - all of this part is working fine - I have created a very simple array to store the user numbers. I have some additional formatting for centering the data etc.

Code:
Option Explicit

Sub Macro1()


Dim wb As Workbook
Set wb = ThisWorkbook
Dim wsdata As Worksheet
Set wsdata = wb.Worksheets("data")
Dim rngdataforfilter As Range
Set rngdataforfilter = wsdata.Range("$A$1:$l$1")
Dim rngstartcell As Range
Set rngstartcell = wsdata.Range("a1")


Dim strdrivers(9) As String
Dim i As Integer


If wsdata.ProtectContents = True Then
  Exit Sub
End If




strdrivers(0) = "1"
strdrivers(1) = "3"
strdrivers(2) = "4"
strdrivers(3) = "5"
strdrivers(4) = "6"
strdrivers(5) = "7"
strdrivers(6) = "8"
strdrivers(7) = "14"
strdrivers(8) = "15"
strdrivers(9) = "17"


Application.ScreenUpdating = False


    For i = LBound(strdrivers) To UBound(strdrivers)
    
    rngdataforfilter.AutoFilter Field:=10, Criteria1:=strdrivers(i)
    rngstartcell.CurrentRegion.Copy
    
    Dim rngstartcelldriverws As Range
    Set rngstartcelldriverws = Worksheets(strdrivers(i)).Range("a1")
    
    With Worksheets(strdrivers(i))
       rngstartcelldriverws.PasteSpecial Paste:=xlPasteValues
      .Range("C:C").NumberFormat = "dd/mm/yyyy"
      .Columns.AutoFit
       rngstartcelldriverws.CurrentRegion.HorizontalAlignment = xlCenter
    End With
     
    Next i


Worksheets("data").ShowAllData
   
Application.ScreenUpdating = True
   
End Sub

However, the final piece to the puzzle is just to simply sort the data by date which is column C. I have tried many ways of doing this but none of them seem to work - which I am struggling to understand why not. I want to include this sort into my loop - where everything else is working fine. The annoying thing is that if I write the code procedurally - ie removing the loop and having the code many times larger than it is now - then it works?! I have not included any of the code that I have tried to use for the sorting.

I will try to include one of the worksheets that are created by the code so far.

Any help would be much appreciated.



MONTHDAYDATECONTRACTPICK UPDROP OFFFARE (£)DRIVER REFPICK UP/RETURN
MAYTUE01/05/2018COSAWESCOSAWESPEN COLL208PICK UP
MAYTUE01/05/2018COSAWESPEN COLLCOSAWES208RETURN
MAYWED02/05/2018B MEWSMABE PRIMB MEWS158RETURN
MAYFRI04/05/2018BASS ACCACIABASS ACACIAPEN COLL128PICK UP
MAYFRI04/05/2018BASS ACCACIAPEN COLLBASS ACACIA128RETURN
MAYWED09/05/2018SWAN MONGLSWAN MONGLFAL SCH128PICK UP
MAYTHU10/05/2018BASS ACCACIABASS ACACIAPEN COLL128PICK UP
MAYTHU10/05/2018BASS ACCACIAPEN COLLBASS ACACIA128RETURN
MAYMON14/05/2018TAPPONSNOOTHNEWQUAY608PICK UP
MAYMON14/05/2018TAPNEWQUAYPONSANOOTH608RETURN
MAYMON14/05/2018COSAWESPEN COLLCOSAWES208RETURN
MAYTUE15/05/2018BASS ACCACIABASS ACACIAPEN COLL128PICK UP
MAYTUE15/05/2018BASS ACCACIAPEN COLLBASS ACACIA128RETURN
MAYTUE15/05/2018PENRYN COLLEGE/KERGKERGILLIACKPEN COLL68PICK UP
MAYWED16/05/2018B MEWSB MEWSMABE PRIM158PICK UP
MAYWED16/05/2018TREGEWTREGEWDRAC CNTRE158PICK UP
MAYWED16/05/2018B MEWSMABE PRIMB MEWS158RETURN
MAYTHU17/05/2018COSAWESCOSAWESPEN COLL208PICK UP
MAYTHU17/05/2018COSAWESPEN COLLCOSAWES208RETURN
MAYTHU03/05/2018BAPTIST CHURCHMEADOW,THEYBAP CHURCH48PICK UP
MAYTHU10/05/2018BAPTIST CHURCHMEADOW,THEYBAP CHURCH48PICK UP
MAYMON21/05/2018SWAN MONGLSWAN MONGLFAL SCH128PICK UP
MAYMON21/05/2018SWAN MONGLFAL SCHSWANP MONGL128RETURN
MAYTUE22/05/2018BERKLEY COTTAGESLOWENAFALCARE258RETURN

<tbody>
</tbody>
 
Last edited by a moderator:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
How about
Code:
   With Worksheets(strdrivers(i))
      rngstartcelldriverws.PasteSpecial Paste:=xlPasteValues
      .Range("C:C").NumberFormat = "dd/mm/yyyy"
      .Columns.AutoFit
      rngstartcelldriverws.CurrentRegion.HorizontalAlignment = xlCenter
      .Sort.SortFields.Clear
      .Sort.SortFields.Add key:=Range("C2"), SortOn:=xlSortOnValues, order:=xlAscending, DataOption:=xlSortNormal
      With .Sort
         .SetRange Range("A1").CurrentRegion
         .Header = xlYes
         .MatchCase = False
         .Orientation = xlTopToBottom
         .Apply
      End With
   End With
 
Upvote 0
Fluff - many, many thanks - this works perfectly. I will now go away and work out why my previous attempts failed as they were so close.

Once again - thank you :~)
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
Glad to help & thanks for the feedback

Hi Fluff

Can I ask a question please about the solution that you gave me...

Are you actually using Sort as an object in its own right; it does look like it. I was under the impression that Sort was a method of the Range object and did not know that it could be used like this.

Many thanks

Dt
 
Upvote 0
It's a method of the range object in older versions of Excel, and it still works.

Code:
range("A1"),currentregion.sort key1:=range("C2"), Header:=xlyes
 
Upvote 0
It's a method of the range object in older versions of Excel, and it still works.

Code:
range("A1"),currentregion.sort key1:=range("C2"), Header:=xlyes

Hi shg

Thanks for this.

Can I dig a bit deeper here if you don’t mind - fairly new to vba but trying very hard to write the correct way ie trying to use references to ranges etc and NEVER using .Select ;~)

So, whilst it can be used as a method of Range object - it can also be used as a discrete object having its own methods and properties?

Many thanks
 
Upvote 0
See Help for Sort Object. Fluff's code shows examples of its properties and methods.
 
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,721
Members
449,465
Latest member
TAKLAM

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