Hi All
My code works if I have more than 3 rows in current region.
When I have 2 rows the source range and end range are the same IE a cell at activecell offset 1,5
Therefore the fillrange doesn't make sense.
How do I get this code to "copy and paste" in the same cell?
Spreadsheet example :
[TABLE="width: 636"]
<tbody>[TR]
[TD]Material / SITE[/TD]
[TD]Size[/TD]
[TD]Supplier[/TD]
[TD]Cost[/TD]
[TD]CEF[/TD]
[TD]Total Cost[/TD]
[/TR]
[TR]
[TD]aaa[/TD]
[TD]5lt[/TD]
[TD]jjj[/TD]
[TD] R 10.20[/TD]
[TD]2[/TD]
[TD]20.40[/TD]
[/TR]
[TR]
[TD]sss[/TD]
[TD]5lt[/TD]
[TD]uu[/TD]
[TD] R 12.50[/TD]
[TD]2[/TD]
[TD]25.00[/TD]
[/TR]
[TR]
[TD]ddd[/TD]
[TD]5lt[/TD]
[TD]yg[/TD]
[TD] R 51.30[/TD]
[TD]2[/TD]
[TD]102.60[/TD]
[/TR]
[TR]
[TD]fff[/TD]
[TD]5lt[/TD]
[TD]t[/TD]
[TD] R 88.88[/TD]
[TD]2[/TD]
[TD]177.76[/TD]
[/TR]
[TR]
[TD]ccc[/TD]
[TD]5lt[/TD]
[TD]hio[/TD]
[TD] R 65.52[/TD]
[TD]1[/TD]
[TD]65.52[/TD]
[/TR]
[TR]
[TD]hhh[/TD]
[TD]tin[/TD]
[TD]edf[/TD]
[TD] R 54.69[/TD]
[TD]1[/TD]
[TD]54.69[/TD]
[/TR]
[TR]
[TD]jjj[/TD]
[TD]5lt[/TD]
[TD]sas[/TD]
[TD] R 69.67[/TD]
[TD]2[/TD]
[TD]139.34[/TD]
[/TR]
[TR]
[TD]uuu[/TD]
[TD]Pair[/TD]
[TD]xvb[/TD]
[TD] R 12.36[/TD]
[TD]4[/TD]
[TD]49.44[/TD]
[/TR]
[TR]
[TD]lll[/TD]
[TD]20Pk[/TD]
[TD]ejh[/TD]
[TD] R 36.25[/TD]
[TD]10[/TD]
[TD]362.50[/TD]
[/TR]
[TR]
[TD]ppp[/TD]
[TD]20Pk[/TD]
[TD]khg[/TD]
[TD] R 36.10[/TD]
[TD]10[/TD]
[TD]361.00[/TD]
[/TR]
[TR]
[TD]rvt[/TD]
[TD]10Pk[/TD]
[TD]jh[/TD]
[TD] R 14.57[/TD]
[TD]10[/TD]
[TD]145.68[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]1503.93[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Material / SITE[/TD]
[TD]Size[/TD]
[TD]Supplier[/TD]
[TD]Cost[/TD]
[TD]ABC[/TD]
[TD]Total Cost[/TD]
[/TR]
[TR]
[TD]aaa[/TD]
[TD]5lt[/TD]
[TD]bbb[/TD]
[TD] R 10.20[/TD]
[TD]6[/TD]
[TD]=E1276*D1276[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]=SUM(F1276)[/TD]
[/TR]
</tbody>[/TABLE]
My code works if I have more than 3 rows in current region.
When I have 2 rows the source range and end range are the same IE a cell at activecell offset 1,5
Therefore the fillrange doesn't make sense.
How do I get this code to "copy and paste" in the same cell?
Spreadsheet example :
[TABLE="width: 636"]
<tbody>[TR]
[TD]Material / SITE[/TD]
[TD]Size[/TD]
[TD]Supplier[/TD]
[TD]Cost[/TD]
[TD]CEF[/TD]
[TD]Total Cost[/TD]
[/TR]
[TR]
[TD]aaa[/TD]
[TD]5lt[/TD]
[TD]jjj[/TD]
[TD] R 10.20[/TD]
[TD]2[/TD]
[TD]20.40[/TD]
[/TR]
[TR]
[TD]sss[/TD]
[TD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D]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Material / SITE[/TD]
[TD]Size[/TD]
[TD]Supplier[/TD]
[TD]Cost[/TD]
[TD]ABC[/TD]
[TD]Total Cost[/TD]
[/TR]
[TR]
[TD]aaa[/TD]
[TD]5lt[/TD]
[TD]bbb[/TD]
[TD] R 10.20[/TD]
[TD]6[/TD]
[TD]=E1276*D1276[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]=SUM(F1276)[/TD]
[/TR]
</tbody>[/TABLE]
Code:
Dim SourceRange As Range
Dim EndRange As Range
Dim FillRange As Range
ActiveCell.Offset(0, 5).Value = "Total Cost"
ActiveCell.Offset(1, 5).NumberFormat = "0.00"
ActiveCell.Offset(1, 5).Formula = "=IFERROR(" & ActiveCell.Offset(1, 3).Address(False, False) & "*" & ActiveCell.Offset(1, 4) _
.Address(False, False) & ","""")"
Set SourceRange = ActiveCell.Offset(1, 5)
Set EndRange = ActiveCell.Offset(ActiveCell.CurrentRegion.Rows.Count - 1, 5)
Set FillRange = Range(SourceRange, EndRange)
ActiveCell.Offset(1, 5).Formula = "=IFERROR(" & ActiveCell.Offset(1, 3).Address(False, False) & "*" & ActiveCell.Offset(1, 4) _
.Address(False, False) & ","""")"
SourceRange.AutoFill Destination:=FillRange
Set SourceRange = Nothing
Set EndRange = Nothing
Set FillRange = Nothing
ActiveCell.Offset(ActiveCell.CurrentRegion.Rows.Count, 5).Formula = "=SUM(" & ActiveCell.Offset(1, 5).Address & ":" & ActiveCell.Offset(ActiveCell.CurrentRegion.Rows.Count - 1, 5).Address & ")"