VBA Insert SUM formula not result of SUM

tourless

Board Regular
Joined
Feb 8, 2007
Messages
144
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,

I have invoice data separated with blank lines between invoice numbers and I'm currently using the following code which gives me the result of summing up the line items of a given invoice and it works fine. But what I would like to do is actually insert the sum formula for the rows that make up each invoice and insert that formula in the column to the right of the blank line which marks the end of a given invoice. The code I'm using should make it clearer...

VBA Code:
    With ActiveSheet
        lastRow = Cells(Rows.Count, "E").End(xlUp).Row
        firstRow = 6
        TempTotal = 0
        For x = firstRow To lastRow + 1
            If Cells(x, "E") <> "" Then
                TempTotal = TempTotal + Cells(x, "E")
            Else:   Cells(x, "F") = TempTotal
                    Cells(x, "F").Interior.ColorIndex = 6
                    TempTotal = 0
            End If
        Next x
    End With

The result is accurate as far as the math goes but my data range can have a value that extends to four decimal places which I have to truncate to two decimal places which can change the value of the sum and that new value is not reflected in my sum with the code I'm using because it only enters the value of the sum and not the formula for the sum. With that I'm not sure if it would be easier to round to the two decimal places before I run the code above or to try and make the change I'm looking for in the code.

I defer to you experts for advise.

Thanks.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
What is in col E? Is it hard values, or formulas?
 
Upvote 0
hard values. They are formatted as two digit decimals but can contain values that extend up to four decimal places depending on its value.
 
Upvote 0
Ok, how about
VBA Code:
Sub tourless()
   Dim Rng As Range
   For Each Rng In Range("E6", Range("E" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
      With Rng.Offset(Rng.Count, 1).Resize(1)
         .Formula = "=sum(" & Rng.Address & ")"
         .Interior.ColorIndex = 6
      End With
   Next Rng
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,596
Members
452,658
Latest member
GStorm

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top