AvoidingVBA
New Member
- Joined
- Dec 15, 2021
- Messages
- 7
- Office Version
- 365
- Platform
- Windows
I'm trying to take a recorded macro which results in absolute ranges and apply dynamic ranges to the code. In this case, I need to sum a dynamic range and then have the result appear two cells below the last row of the dynamic range.
In this example, I would need to sum the totals of A1:A4 and have the result appear in A6. The number of rows will change with each dataset but the columns will not.
I've already had wonderful help from these forums so there will be a mix of recorded and VBA. Please note this task is just a part of a larger macro, and due to my unfamiliarity with VBA I'm not 100% sure where these specific inputs start and end, but it is after the AutoFill and before the Sort commands. In this code, the column that holds the range I will need to tabulate is E. I will then have to replicate the same tabulation in the four adjacent cells (F,G,H,I). Here is the current code:
Range("A11").Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("A11:A" & Range("E" & Rows.Count).End(xlUp).Row)
Range("A11:A54").Select
Range("I11").Select
Selection.AutoFill Destination:=Range("I11:I" & Range("E" & Rows.Count).End(xlUp).Row)
Range("I11:I54").Select
Range("E11").Select
Selection.End(xlDown).Select
Range("E56").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=SUM(R[-46]C:R[-2]C)"
Range("E56").Select
Selection.Copy
Range("E56:I56").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("K56").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-6]-RC[-2]"
Range("A10:J54").Select
Range("E25").Activate
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add2 Key:=Range( _
"A11:A54"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
"Equity,Allocation,Unknown,BDC,Fixed,Cash/Equiv", DataOption:=xlSortNormal
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add(Range("B11:B54"), _
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(0, 176 _
, 240)
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add(Range("B11:B54"), _
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(231, _
230, 230)
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add2 Key:=Range( _
"B11:B54"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A10:J54")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWindow.SmallScroll Down:=-72
Range("A4:B8").Select
End Sub
In this example, I would need to sum the totals of A1:A4 and have the result appear in A6. The number of rows will change with each dataset but the columns will not.
I've already had wonderful help from these forums so there will be a mix of recorded and VBA. Please note this task is just a part of a larger macro, and due to my unfamiliarity with VBA I'm not 100% sure where these specific inputs start and end, but it is after the AutoFill and before the Sort commands. In this code, the column that holds the range I will need to tabulate is E. I will then have to replicate the same tabulation in the four adjacent cells (F,G,H,I). Here is the current code:
Range("A11").Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("A11:A" & Range("E" & Rows.Count).End(xlUp).Row)
Range("A11:A54").Select
Range("I11").Select
Selection.AutoFill Destination:=Range("I11:I" & Range("E" & Rows.Count).End(xlUp).Row)
Range("I11:I54").Select
Range("E11").Select
Selection.End(xlDown).Select
Range("E56").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=SUM(R[-46]C:R[-2]C)"
Range("E56").Select
Selection.Copy
Range("E56:I56").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("K56").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-6]-RC[-2]"
Range("A10:J54").Select
Range("E25").Activate
ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add2 Key:=Range( _
"A11:A54"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
"Equity,Allocation,Unknown,BDC,Fixed,Cash/Equiv", DataOption:=xlSortNormal
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add(Range("B11:B54"), _
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(0, 176 _
, 240)
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add(Range("B11:B54"), _
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(231, _
230, 230)
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add2 Key:=Range( _
"B11:B54"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A10:J54")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWindow.SmallScroll Down:=-72
Range("A4:B8").Select
End Sub