That is very peculiar.
- I opened your file and (like you said) the end column shows #VALUE.
- Because it is a new file to my system I must click on "Enable Editing" to modify.
- When I did that all those values became proper numbers
I have done nothing here to make it work other than apply my own settings to the file.
- My Excel is treating the percentage correctly , yours thinks it is text (perhaps!)
- I suspect a minor regional setting difference is causing this.
- Your Excel is behaving differently to mine. Are you also using Excel2016?
Try out this modified code . I cannot replicate what happens in your Excel.
Let me know if it does not work and I will provide a few suggestions as to what you could try.
Sub InsertTotals2()
With ActiveSheet
Dim lastRow As Long
Dim subT As Double
Dim r1 As Range, r2 As Range
Dim subT_Formula As String
Dim disc As Variant
lastRow = .Range("G9").End(xlDown).Row
For r = 9 To lastRow
'total price formula
.Cells(r, 9).Formula = "=ROUND(F" & r & "*G" & r & "*(1-H" & r & "),2)"
're-format discount column
.Cells(r, 8) = .Cells(r, 8).Value
disc = --.Cells(r, 8).Value
If disc = 0 Then disc = ""
.Cells(r, 8) = disc
Next r
'Total fruit
.Cells(lastRow, 9).Formula = "=SUM(I2:I" & lastRow - 1 & ")/2"
.Cells(lastRow, 7).Formula = "=SUM(G2:G" & lastRow - 1 & ")/2"
'intermediate sub-totals
.Cells(9, 9).Select
For r = 9 To lastRow - 1
Set r1 = ActiveCell
Do Until ActiveCell.Font.Bold = True
ActiveCell.Offset(1, 0).Select
Loop
Set r2 = ActiveCell.Offset(-1, 0)
subT_Formula = "=sum(" & r1.Address(0, 0) & ":" & r2.Address(0, 0) & ")"
ActiveCell.Formula = subT_Formula
ActiveCell.Copy
ActiveCell.Offset(0, -2).Select
Selection.PasteSpecial Paste:=xlPasteFormulas
ActiveCell.Offset(1, 2).Select
r = ActiveCell.Row
Next r
'grand total
.Cells(lastRow + 2, 9).Formula = "=I" & lastRow
.Cells(lastRow + 2, 7).Formula = "=G" & lastRow
End With
End Sub
Bookmarks