I want to store the SUB below in a module then to call it from anywhere so that I don't have to write the code over and over again...
My problem is how/what to declare of the variables (in the SUB line)
THEN when this sub is called, it will be as below:
SUB SortSubtotal("client", "C2:C",3)
SUB SortSubtotal("item", "D2:D",4)
etc...
SUB SortSubtotal(mitem as variant?, md2d as variant?, m4 as long?)
note: Those hi-lighted (RED) below are to be replaced by variables (above)
"item" = mitem (worksheet name)
"D2:D" = mdsd (range)
4 = m4 (column number)
1 to 19 below is the repetetive task
I hope I explained it clearly.
many many thanks
My problem is how/what to declare of the variables (in the SUB line)
THEN when this sub is called, it will be as below:
SUB SortSubtotal("client", "C2:C",3)
SUB SortSubtotal("item", "D2:D",4)
etc...
SUB SortSubtotal(mitem as variant?, md2d as variant?, m4 as long?)
note: Those hi-lighted (RED) below are to be replaced by variables (above)
"item" = mitem (worksheet name)
"D2:D" = mdsd (range)
4 = m4 (column number)
1 to 19 below is the repetetive task
- Sheets("item").Select
- Sheets("item").Range("A2").Select
- Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
- :=False, Transpose:=False
- Application.CutCopyMode = False
- ActiveWorkbook.Worksheets("item").Sort.SortFields.Clear
- ActiveWorkbook.Worksheets("item").Sort.SortFields.Add Key:=Range("D2:D" & LastRow), _
- SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
- With ActiveWorkbook.Worksheets("item").Sort
- .SetRange Range("A1:J" & LastRow)
- .Header = xlYes
- .MatchCase = False
- .Orientation = xlTopToBottom
- .SortMethod = xlPinYin
- .Apply
- End With
- Sheets("item").Range("A1:J" & LastRow).Select
- Selection.Subtotal GroupBy:=4, Function:=xlSum, TotalList:=Array(7), _
- Replace:=True, PageBreaks:=False, SummaryBelowData:=True
I hope I explained it clearly.
many many thanks