Dreamteam
New Member
- Joined
- Feb 22, 2018
- Messages
- 28
- Office Version
- 365
- Platform
- 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.
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.
[TABLE="width: 781"]
<tbody>[TR]
[TD]MONTH[/TD]
[TD]DAY[/TD]
[TD]DATE[/TD]
[TD]CONTRACT[/TD]
[TD]PICK UP[/TD]
[TD]DROP OFF[/TD]
[TD]FARE (£)[/TD]
[TD]DRIVER REF[/TD]
[TD]PICK UP/RETURN[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]TUE[/TD]
[TD]01/05/2018[/TD]
[TD]COSAWES[/TD]
[TD]COSAWES[/TD]
[TD]PEN COLL[/TD]
[TD]20[/TD]
[TD]8[/TD]
[TD]PICK UP[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]TUE[/TD]
[TD]01/05/2018[/TD]
[TD]COSAWES[/TD]
[TD]PEN COLL[/TD]
[TD]COSAWES[/TD]
[TD]20[/TD]
[TD]8[/TD]
[TD]RETURN[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]WED[/TD]
[TD]02/05/2018[/TD]
[TD]B MEWS[/TD]
[TD]MABE PRIM[/TD]
[TD]B MEWS[/TD]
[TD]15[/TD]
[TD]8[/TD]
[TD]RETURN[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]FRI[/TD]
[TD]04/05/2018[/TD]
[TD]BASS ACCACIA[/TD]
[TD]BASS ACACIA[/TD]
[TD]PEN COLL[/TD]
[TD]12[/TD]
[TD]8[/TD]
[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[TD]15/05/2018[/TD]
[TD]BASS ACCACIA[/TD]
[TD]BASS ACACIA[/TD]
[TD]PEN COLL[/TD]
[TD]12[/TD]
[TD]8[/TD]
[TD]PICK UP[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]TUE[/TD]
[TD]15/05/2018[/TD]
[TD]BASS ACCACIA[/TD]
[TD]PEN COLL[/TD]
[TD]BASS ACACIA[/TD]
[TD]12[/TD]
[TD]8[/TD]
[TD]RETURN[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]TUE[/TD]
[TD]15/05/2018[/TD]
[TD]PENRYN COLLEGE/KERG[/TD]
[TD]KERGILLIACK[/TD]
[TD]PEN COLL[/TD]
[TD]6[/TD]
[TD]8[/TD]
[TD]PICK UP[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]WED[/TD]
[TD]16/05/2018[/TD]
[TD]B MEWS[/TD]
[TD]B MEWS[/TD]
[TD]MABE PRIM[/TD]
[TD]15[/TD]
[TD]8[/TD]
[TD]PICK UP[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]WED[/TD]
[TD]16/05/2018[/TD]
[TD]TREGEW[/TD]
[TD]TREGEW[/TD]
[TD]DRAC CNTRE[/TD]
[TD]15[/TD]
[TD]8[/TD]
[TD]PICK UP[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]WED[/TD]
[TD]16/05/2018[/TD]
[TD]B MEWS[/TD]
[TD]MABE PRIM[/TD]
[TD]B MEWS[/TD]
[TD]15[/TD]
[TD]8[/TD]
[TD]RETURN[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]THU[/TD]
[TD]17/05/2018[/TD]
[TD]COSAWES[/TD]
[TD]COSAWES[/TD]
[TD]PEN COLL[/TD]
[TD]20[/TD]
[TD]8[/TD]
[TD]PICK UP[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]THU[/TD]
[TD]17/05/2018[/TD]
[TD]COSAWES[/TD]
[TD]PEN COLL[/TD]
[TD]COSAWES[/TD]
[TD]20[/TD]
[TD]8[/TD]
[TD]RETURN[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]THU[/TD]
[TD]03/05/2018[/TD]
[TD]BAPTIST CHURCH[/TD]
[TD]MEADOW,THEY[/TD]
[TD]BAP CHURCH[/TD]
[TD]4[/TD]
[TD]8[/TD]
[TD]PICK UP[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]THU[/TD]
[TD]10/05/2018[/TD]
[TD]BAPTIST CHURCH[/TD]
[TD]MEADOW,THEY[/TD]
[TD]BAP CHURCH[/TD]
[TD]4[/TD]
[TD]8[/TD]
[TD]PICK UP[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]MON[/TD]
[TD]21/05/2018[/TD]
[TD]SWAN MONGL[/TD]
[TD]SWAN MONGL[/TD]
[TD]FAL SCH[/TD]
[TD]12[/TD]
[TD]8[/TD]
[TD]PICK UP[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]MON[/TD]
[TD]21/05/2018[/TD]
[TD]SWAN MONGL[/TD]
[TD]FAL SCH[/TD]
[TD]SWANP MONGL[/TD]
[TD]12[/TD]
[TD]8[/TD]
[TD]RETURN[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]TUE[/TD]
[TD]22/05/2018[/TD]
[TD]BERKLEY COTTAGES[/TD]
[TD]LOWENA[/TD]
[TD]FALCARE[/TD]
[TD]25[/TD]
[TD]8[/TD]
[TD]RETURN[/TD]
[/TR]
</tbody>[/TABLE]
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.
[TABLE="width: 781"]
<tbody>[TR]
[TD]MONTH[/TD]
[TD]DAY[/TD]
[TD]DATE[/TD]
[TD]CONTRACT[/TD]
[TD]PICK UP[/TD]
[TD]DROP OFF[/TD]
[TD]FARE (£)[/TD]
[TD]DRIVER REF[/TD]
[TD]PICK UP/RETURN[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]TUE[/TD]
[TD]01/05/2018[/TD]
[TD]COSAWES[/TD]
[TD]COSAWES[/TD]
[TD]PEN COLL[/TD]
[TD]20[/TD]
[TD]8[/TD]
[TD]PICK UP[/TD]
[/TR]
[TR]
[TD]MAY[/TD]
[TD]TUE[/TD]
[TD]01/05/2018[/TD]
[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[TD]BERKLEY COTTAGES[/TD]
[TD]LOWENA[/TD]
[TD]FALCARE[/TD]
[TD]25[/TD]
[TD]8[/TD]
[TD]RETURN[/TD]
[/TR]
</tbody>[/TABLE]
Last edited by a moderator: