jthompson92
New Member
- Joined
- Jan 6, 2020
- Messages
- 2
- Office Version
- 365
- Platform
- Windows
Hello,
My first post here, so let me know if you need more information. I've written a macro that calls the following function with sName = the sheet name, and group = name of the desired range to sort by:
I have two essentially identical sheets - one is current inventory and the other is inventory archive. Currently, in the testing phase, each sheet is absolutely identical. They both contain the exact same data and zero formulas. For some reason, when I execute the macro on the archive sheet, it computes in seconds, while in the inventory sheet, it takes minutes, and the computation is risky for crashing.
Does anyone know of a possible reason for this? I should mention that another sheet has several VLOOKUP references to the current inventory sheet, but removing these doesn't seem to make a difference.
Thank you
My first post here, so let me know if you need more information. I've written a macro that calls the following function with sName = the sheet name, and group = name of the desired range to sort by:
VBA Code:
Function g_SortByGroup(sName, group)
Dim lastRow As Long
Dim lastColumn As Long
Dim subCol1 As Long
Dim subCol2 As Long
Dim cName As String
Dim ws As Worksheet
'Sets values using other functions
Set ws = ActiveWorkbook.Sheets(sName)
lastColumn = getLastColumn(sName) + 1
lastRow = getLastRow(sName)
cName = getColumnName(sName)
'Sort according to columns that will be subtotaled
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=Range( _
cName & "_" & group).Rows("2:" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ws.Sort.SortFields.Add Key:=Range( _
cName & "_Shipping_Name").Rows("2:" & lastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ws.Sort
.SetRange Range(Cells(2, 2), Cells(lastRow, lastColumn))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Get the column numbers of the columns to be subtotaled
subCol1 = getColumnNumber(sName, "_" & group) - 1
subCol2 = getColumnNumber(sName, "_Quantity") - 1
'First subtotal
Range(cName & "_All").Select
Selection.Subtotal GroupBy:=subCol1, Function:=xlSum, TotalList:=Array(subCol2), _
Replace:=False, PageBreaks:=False, SummaryBelowData:=True
'Get new values
subCol1 = getColumnNumber(sName, "_Shipping_Name") - 1
lastRow = Application.WorksheetFunction.CountA(Range(cName & "_Quantity"))
'Second subtotal
Range(cName & "_All").Select
Selection.Subtotal GroupBy:=subCol1, Function:=xlSum, TotalList:=Array(subCol2), _
Replace:=False, PageBreaks:=False, SummaryBelowData:=True
ActiveSheet.Outline.ShowLevels RowLevels:=3
End Function
I have two essentially identical sheets - one is current inventory and the other is inventory archive. Currently, in the testing phase, each sheet is absolutely identical. They both contain the exact same data and zero formulas. For some reason, when I execute the macro on the archive sheet, it computes in seconds, while in the inventory sheet, it takes minutes, and the computation is risky for crashing.
Does anyone know of a possible reason for this? I should mention that another sheet has several VLOOKUP references to the current inventory sheet, but removing these doesn't seem to make a difference.
Thank you