Dynamic SUM for data range

You have a range of cells with values. Then you want to create sum function at the bottom and on the right hand side of the range.

Sum functions are needed in row 6 and column F. This is easy and can be done manually, of course. Just copy the sum to remaining rows and columns.

If you need to do this process repeatedly, a macro might help you. The VBA is added at the end of this blog.

To run the macro, you need to place the cursor in the top left corner of the data range. In our case, the start cell is B2.

Zs is the address of the start cell, eg B2. Then we go at the end of the B row, the address of that cell is stored in zl. Then we go one line down and add the sum sentence SUM(start cell:end cell), then we go one row upwards and one column to right. If that cell is empty then we need to add the sum sentences for each row. If that cell is not empty then we continue counting column sums.

After counting column sums, then we go to the start cell, in this case B2. Zs and zl are populated again. We move to the right end of the data range. Then we move one step to right and there the SUM(start cell:end cell) sentence will be created. Then we move one row down and one column to left. If the cell is empty, then the macro is finished. If the cell is not empty, then we continue counting row sums.

Instead of manually creating sum sentences, place the cursor in B2 and execute the VBA below.

Sub z_sum2()

Dim zs, zl, zss

zss = ActiveCell.Address

Do

zs = ActiveCell.Address

Selection.End(xlDown).Select

zl = ActiveCell.Address

ActiveCell.Offset(1, 0).Select

ActiveCell.Formula = “=sum(” & zs & “:” & zl & “)”

ActiveCell.Offset(-1, 1).Select

If ActiveCell.Value = “” Then

    GoTo zrow

Else

    Selection.End(xlUp).Select

End If

Loop

zrow:

Range(zss).Select

Do

zs = ActiveCell.Address

Selection.End(xlToRight).Select

zl = ActiveCell.Address

ActiveCell.Offset(0, 1).Select

ActiveCell.Formula = “=sum(” & zs & “:” & zl & “)”

ActiveCell.Offset(1, -1).Select

If ActiveCell.Value = “” Then

    Range(zss).Select

    MsgBox “Done.”

    End

Else

    Selection.End(xlToLeft).Select

End If

Loop

End Sub

Cost volume analysis graph

In profit cost volume analysis one target is to find the break even point. When the sales are reaching the zero result and the company does not make any profit but not loss either. The result can be presented with a graph, negative side is in red and positive green. Let me demonstrate.

The product is sold for 20 units each. The variable cost is 12,5 per piece. The variable cost is directly dependent on the amount sold. The fixed costs are standard no matter how many pieces are sold.

The sales unit is an independent value. Sales equal to sales units multiplied by sales price per product. Variable costs are sales units multiplied by variable cost per product. Fixed costs are fixed. The result is sales minus variable costs minus fixed costs.

The aim is to have a graph, which shows loss area with a different color than profit area.

The result is divided into two different columns negative and positive. When the result is negative, then the values are presented in negative column.  When the result is positive, then the values are presented in positive column. The sentence in M3 is =IF(L3<0;L3;””) and in N3 =IF(L3>0;L3;””).

To start creating a graph, select insert- chart – 2D line.

You will have a blank canvas. Press right click and select select data.

Add legend entries.

The first legend, press ok.

Add another legend.

The second legend, press ok.

Edit horizontal category.

Show the sales unit amounts.

Data source should look like this. Press ok.

Chart looks like this, not yet finalised.

Right click the graph and select change chart type.

Select combo and stacked area for both neg and pos.

Right click the negative area and select red colour. Select green colour for positive area.

Now we can test our Excel and change the input values.

The sales price was changed to 21 and break even changed to appr. 120.

The variable costs were increased to 14 and break even is appr 140+.

Graph might be descriptive and visualize the break even point.