phyxius117
New Member
- Joined
- May 14, 2024
- Messages
- 6
- Office Version
- Prefer Not To Say
- Platform
- Windows
Hey all,
After looking up guides, I tried to incorporate a line of code to mimic custom sort for my end product of this macro. Basically this macro takes existing tabs of active worksheet, copies them then perform some operations on them within the new worksheet. For some reason my sorting code isn't working. The Relevant Section is in blue and the sorting line is in red color. Can anyone tell me why and how to fix it? Thanks.
After looking up guides, I tried to incorporate a line of code to mimic custom sort for my end product of this macro. Basically this macro takes existing tabs of active worksheet, copies them then perform some operations on them within the new worksheet. For some reason my sorting code isn't working. The Relevant Section is in blue and the sorting line is in red color. Can anyone tell me why and how to fix it? Thanks.
VBA Code:
Sub Export()
Set wbThis = ThisWorkbook
Dim DCname As String
DCname = ActiveWorkbook.Sheets("Reference").Range("R2").Value
Dim FiscalYear As String
FiscalYear = ActiveWorkbook.Sheets("Reference").Range("R8").Value
Dim Period As String
Period = ActiveWorkbook.Sheets("Reference").Range("R11").Value
Dim FileName As String
If Worksheets("Reference").Range("R8") = "Test" Then
FileName = InputBox("Enter File Name")
Else
FileName = DCname & "_SQL Data_" & FiscalYear & "_" & Period
End If
Dim Visible As XlSheetVisibility
Worksheets("Route Totals").Visible = xlSheetVisible
Worksheets("Stops").Visible = xlSheetVisible
Worksheets("Domicile Rates").Visible = xlSheetVisible
Worksheets(Array("Route Totals", "Stops", "Domicile Rates")).Copy
With ActiveWorkbook.Worksheets("Route Totals")
.UsedRange.Value = .UsedRange.Value
.Cells.Columns.AutoFit
End With
[COLOR=rgb(41, 105, 176)] With ActiveWorkbook.Worksheets("Stops")
.UsedRange.Value = .UsedRange.Value
Dim lastrow As Long
lastrow = .Range("M" & Rows.Count).End(xlUp).Row
.Range("L3").Formula = "=miles($AL$2,AL3)"
.Range("L3:L" & lastrow).FillDown
.Range("L3:L" & lastrow).Copy
.Range("L3:L" & lastrow).PasteSpecial Paste:=xlPasteValues
.Range("AM3").Formula = "=IF(G3=0,0,tolls(AL2,AL3,true)*-1.05)"
.Range("AM3:AM" & lastrow).FillDown
.Range("AM3:AM" & lastrow).Copy
.Range("AM3:AM" & lastrow).PasteSpecial Paste:=xlPasteValues
.Range("AN2").Formula = "=IF(G2=0,sumif(B:B,B2,AM:AM),0)"
.Range("AN2:AN" & lastrow).FillDown
.Range("AN2:AN" & lastrow).Copy
.Range("AN2:AN" & lastrow).PasteSpecial Paste:=xlPasteValues
.Range("AO2").Formula = "=IFERROR(VLOOKUP(K2,'Domicile Rates'!A:AP,42,FALSE),0)"
.Range("AO2:AO" & lastrow).FillDown
.Range("AO2:AO" & lastrow).Copy
.Range("AO2:AO" & lastrow).PasteSpecial Paste:=xlPasteValues
.Cells.Columns.AutoFit
[/COLOR][COLOR=rgb(226, 80, 65)] Columns.Sort Key1:=Columns("C"), Order1:=xlAscending, Key2:=Columns("B"), Order2:=xlAscending, Key3:=Columns("G"), Order3:=xlAscending, Header:=xlYes[/COLOR]
[COLOR=rgb(41, 105, 176)] End With[/COLOR]
With ActiveWorkbook.Worksheets("Domicile Rates")
.Visible = Visible
End With
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & FileName, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ThisWorkbook.Worksheets("Route Totals").Visible = Visible
ThisWorkbook.Worksheets("Stops").Visible = Visible
ThisWorkbook.Worksheets("Domicile Rates").Visible = Visible
End Sub