sorting和总结不同大小的电子表格

我想通过构buildmacros来对用户进行sorting和总结。 macros需要find最后一行,然后sorting,然后小计和总计。 它也应该使用当前的活动工作表。 例如,我应该将第一个电子表格放到第二个电子表格中:

前期和后期电子表格

我可以通过简单的macroslogging来完成这个数据集。

Sub Macro1() ' ' Macro1 Macro ' ' Keyboard Shortcut: Ctrl+Shift+B ' ActiveWorkbook.Worksheets("Oct 2015").Sort.SortFields.Clear ActiveWorkbook.Worksheets("Oct 2015").Sort.SortFields.Add Key:=Range("A2:A24" _ ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal ActiveWorkbook.Worksheets("Oct 2015").Sort.SortFields.Add Key:=Range("B2:B24" _ ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Oct 2015").Sort .SetRange Range("A1:C24") .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(3), _ Replace:=True, PageBreaks:=False, SummaryBelowData:=True Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(3), _ Replace:=False, PageBreaks:=False, SummaryBelowData:=True Range("A1:C45").Select End Sub 

我有下面的代码来find最后一行,但不知道如何将它集成到上面,以取代硬编码的“范围”值。

  Sub GetLastRow(strSheet, strColum) Dim MyRange As Range Dim lngLastRow As Long Set MyRange = Worksheets(strSheet).Range(strColum & "1") lngLastRow = Cells(sheetvar.Rows.Count, MyRange.Column).End(xlUp).Row End Sub 

我还需要将活动工作表值更改为当前打开的工作表,因为这个值将会改变。

列名和列顺序应该一致。 我还需要将这个脚本放在远程用户的PC上,并确保它们在打开Excel时可用。

如果可能的话,我也想遮蔽小区域,但这是次要要求。

你在那里的一半。 首先你声明当前表单的variables,范围和最后一行和一列。 然后你把它们全部放到你刚刚录制的macros中。

  Sub Macro1() ' ' Macro1 Macro ' ' Keyboard Shortcut: Ctrl+Shift+B Dim sht As Worksheet Dim lRow As Long, lCol As Long Dim rng As Range Set sht = ActiveWorkbook.ActiveSheet With sht lRow = .Range("A" & .Rows.Count).End(xlUp).Row lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column Set rng = .Range(.Cells(lRow, 1), .Cells(lRow, lCol)) End With sht.Sort.SortFields.Clear sht.Sort.SortFields.Add Key:=rng, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal sht.Sort.SortFields.Add Key:=rng, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With sht.Sort .SetRange rng .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With Selection.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(3), _ Replace:=True, PageBreaks:=False, SummaryBelowData:=True Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(3), _ Replace:=False, PageBreaks:=False, SummaryBelowData:=True End Sub