dpaton05
Well-known Member
- Joined
- Aug 14, 2018
- Messages
- 2,375
- Office Version
- 365
- 2016
- Platform
- Windows
I have a procedure that copies data to different files. At the moment, it sorts the files each time a row is copied to it. This is very slow and I want to sort them at the end.
Many rows might need to be copied the same wsDst, or even different wsDst files depending on the date of them.
Instead of sorting them after each row is copied. Could someone help me with the vba code to sort each wsDst that has been opened at the end of the procedure please?
This is my code and I have included one row fron tblCosting.
- DocYearName is a variable that I have which stores the filename to be opened for each row.
- Combo is a variable that stores the monthly sheet that the row needs to be put in
- wsDst is a combination of the above 2 variables, ie,
VBA Code:
Set wsDst= Workbooks(DocYearName).Worksheets(Combo)
Many rows might need to be copied the same wsDst, or even different wsDst files depending on the date of them.
Instead of sorting them after each row is copied. Could someone help me with the vba code to sort each wsDst that has been opened at the end of the procedure please?
This is my code and I have included one row fron tblCosting.
VBA Code:
Sub cmdCopy()
Dim wsDst As Worksheet, wsHours As Worksheet, wsTrack As Worksheet, worker As String, wsSrc As Worksheet, tblrow As ListRow
Dim Combo As String, sht As Worksheet, tbl As ListObject
Dim lastrow As Long, DocYearName As String, Site As String, lr As Long, HoursRow As Long
Dim RowColor As Long, w As Window, r As Long, HoursRegister As String, ReportTracking As String
Application.ScreenUpdating = False
'assign values to variables
Set tbl = ThisWorkbook.Worksheets("Costing_tool").ListObjects("tblCosting")
Set sht = ThisWorkbook.Worksheets("Costing_tool")
Site = ThisWorkbook.Worksheets("Start_here").Range("H9").Value
'Check if each row has a date, service and requesting organisation
For Each tblrow In tbl.ListRows
If tblrow.Range.Cells(1, 1).Value = "" Or tblrow.Range.Cells(1, 5).Value = "" Or tblrow.Range.Cells(1, 6).Value = "" Then
MsgBox "The Date, Service or Requesting Organisation has not been entered for every record in the table"
Exit Sub
End If
Next tblrow
For Each tblrow In tbl.ListRows
'Define combo as the month to be recorded in
Combo = tblrow.Range.Cells(1, 26).Value
ReportTracking = tblrow.Range.Cells(1, 39)
Select Case Site
Case "Western"
Select Case tblrow.Range.Cells(1, 6).Value
Case "Ang Wes", "AngWagga", "AngAlbury", "AngSouth Coast", "Yiri"
DocYearName = tblrow.Range.Cells(1, 37).Value
Case Else
DocYearName = tblrow.Range.Cells(1, 36).Value
End Select
Case "Riv"
Select Case tblrow.Range.Cells(1, 6).Value
Case "AngWestern", "AngWagga", "AngAlbury", "AngSouth Coast", "Yiri"
DocYearName = tblrow.Range.Cells(1, 42).Value
Case Else
DocYearName = tblrow.Range.Cells(1, 36).Value
End Select
End Select
If Not isFileOpen(DocYearName & ".xlsm") Then Workbooks.Open ThisWorkbook.Path & "\" & "Work Allocation Sheets" & "\" & Site & "\" & DocYearName & ".xlsm"
'If Not isFileOpen(HoursRegister & ".xlsm") Then Workbooks.Open ThisWorkbook.Path & "\" & "Hours Register" & "\" & Site & "\" & HoursRegister & ".xlsm"
If Not isFileOpen(ReportTracking & ".xlsm") Then Workbooks.Open ThisWorkbook.Path & "\" & "Report Tracking" & "\" & Site & "\" & ReportTracking & ".xlsm"
'Set wsHours = Workbooks(HoursRegister).Worksheets(worker)
Set wsDst = Workbooks(DocYearName).Worksheets(Combo)
Set wsTrack = Workbooks(ReportTracking).Worksheets(Combo)
lr = wsDst.Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
With wsTrack
'this copies the date column in the tblCosting
tblrow.Range(, 1).Copy
'this pastes it into column A of report tracking file
.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteFormulasAndNumberFormats
'this copies the YP name column in the tblCosting
tblrow.Range(, 4).Copy
'this pastes it into column B of report tracking file
.Range("A" & Rows.Count).End(xlUp).Offset(, 1).PasteSpecial xlPasteFormulasAndNumberFormats
'this copies the YP name column in the tblCosting
tblrow.Range(, 5).Copy
'this pastes it into column A of report tracking file
.Range("A" & Rows.Count).End(xlUp).Offset(, 2).PasteSpecial xlPasteFormulasAndNumberFormats
End With
With wsDst
'This sets column width of request number column so it can be read and is not xxxxx
.Columns("C:C").ColumnWidth = 8
'This copies the first 7 columns, i.e. A:G, of the current row of the table to column A in the destination sheet.
tblrow.Range.Resize(, 7).Copy
'This pastes in the figures in the first 7 columns starting in column A
.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteFormulasAndNumberFormats
'This copies the first 7 columns, i.e. A:G, of the current row of the table to column A in the destination sheet.
tblrow.Range(, 10).Copy
'This pastes in the figures in the first 7 columns starting in column A
.Range("H" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteFormulasAndNumberFormats
'Overwrites the numbers pasted to column I with a formula
.Range("I" & Rows.Count).End(xlUp).Offset(1).Formula = "=IF(RC[-4]=""Activities"",0,RC[-1]*0.1)"
'Overwrites the numbers pasted to column L with a formula
.Range("J" & Rows.Count).End(xlUp).Offset(1).Formula = "=RC[-1]+RC[-2]"
'Adds currency formatting to total ex gst column
.Columns(8).NumberFormat = "$#,##0.00"
'Adds Australian date format to date column
'.Range("A:A").NumberFormat = "dd/mm/yyyy"
'sort procedure copied from vba
wsDst.Sort.SortFields.Clear
wsDst.Sort.SortFields.Add Key:=Range("A4:A" & lr), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With Workbooks(DocYearName).Worksheets(Combo).Sort
'set range to sort of A3 to AO
.SetRange Range("A3:AO" & lr)
.header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
Next tblrow
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
CSS quoting tool 29.42.xlsm | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | AA | AB | AC | AD | AE | AF | AG | AH | AI | AJ | AK | AL | AM | AN | AO | AP | |||||
4 | Date | Purchase order # | Quote Ref # | Name | Service | Requesting Organisation | Caseworker Name | Price ex. GST | GST | Price inc. GST | Column5 | Column6 | Column7 | Column8 | Column9 | Staff # | Kms | Add. Stops | Day Rate | extra pickup | $ 4 extra hrs | $ 4 kms | Max Pay | Month | Year | Combination | Document name | New doc name | 10% increase | Activities | First Year | Second Year | Financial Year | ExternalAS | InternalAS | HoursRegister | ReportTracking | Column1 | Column2 | Column3 | ||||
5 | 07/01/2021 | 51268 | Supervised Transport | $55.80 | $5.58 | $61.38 | January | 2021 | 2022 - 2023 | 2020 | 2021 | 2020 - 2021 | 2020 - 2021 Western CSS Work Allocation Sheet | 2020 - 2021 Western Internal Work Allocation Sheet | 2020 - 2021 Western Hours Register | 2020 - 2021 Western Report Tracking | 2020 - 2021 Riv Internal Work Allocation Sheet | |||||||||||||||||||||||||||
Costing_tool |
Cell Formulas | ||
---|---|---|
Range | Formula | |
K5 | K5 | =IF(E5="Activities",0,[Price ex. GST]*0.1) |
L5 | L5 | =IF(E5="Activities",[@[Price ex. GST]],[GST]+[Price ex. GST]) |
Z5 | Z5 | =IF(MONTH(A5)=6,"June",TEXT(DATE(YEAR(A5),IF(DAY(A5)<26,MONTH(A5),MONTH(A5)+1),1), "mmmm")) |
AA5 | AA5 | =TEXT(DATE(YEAR(A5),IF(DAY(A5)<26,MONTH(A5),MONTH(A5)+1),1), "yyyy") |
AD5 | AD5 | =CONCATENATE(AA5+1," - ",AA5+2) |
AG5 | AG5 | =IF(MONTH(A5)<7,YEAR(A5)-1,YEAR(A5)) |
AH5 | AH5 | =IF(MONTH(A5)<7,YEAR(A5),YEAR(A5)+1) |
AI5 | AI5 | =CONCATENATE(AG5," - ",AH5) |
AJ5 | AJ5 | =CONCATENATE(AI5," ",Start_here!$H$9," CSS Work Allocation Sheet") |
AK5 | AK5 | =CONCATENATE(AI5," ",Start_here!$H$9," Internal Work Allocation Sheet") |
AL5 | AL5 | =CONCATENATE(AI5," ",Start_here!$H$9," Hours Register") |
AM5 | AM5 | =CONCATENATE(AI5," ",Start_here!$H$9," Report Tracking") |
AP5 | AP5 | =CONCATENATE(AI5," ","Riv Internal Work Allocation Sheet") |