IF Statement Added to VBA Code to Display Blank if Sum Equals 0

lamarh755

New Member
Joined
Jan 28, 2020
Messages
35
Office Version
  1. 2013
I have 4 columns in an excel spreadsheet where the values in each column are added up at the end of the last row. When the sheet is blank, the totals at the bottom show 0. I am trying to have a formula (possibly an IF statement) added to my current formula to make it to where the 4 cells at the bottom of the sheet show blank if the sum equals 0. I tried to use an IF statement but it errors out because I am not sure how to do it using LastRow. Any help would be greatly appreciated.


Dim aLastRow As Long
aLastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row - 3
Range("B" & aLastRow + 1).FormulaR1C1 = "=SUM(R[-" & aLastRow & "]C:R[-1]C)"
Range("E" & aLastRow + 1).FormulaR1C1 = "=SUM(R[-" & aLastRow & "]C:R[-1]C)"
Range("J" & aLastRow + 1).FormulaR1C1 = "=SUM(R[-" & aLastRow & "]C:R[-1]C)"
Range("M" & aLastRow + 1).FormulaR1C1 = "=SUM(R[-" & aLastRow & "]C:R[-1]C)"
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
What does aLastRow return when the sheet is blank?
In this situation, what row do you want these formulas which total to 0 to be placed in?
 
Upvote 0
What does aLastRow return when the sheet is blank?
In this situation, what row do you want these formulas which total to 0 to be placed in?
The sheet is never blank, as there is a header on the template. I probably could have used a better formula/code to be more precise, but I didn't know how to write it. The data that populates starts at row 17 down. Everything above row 17 is just header information. The data goes in the 3rd row up from the last row. The row number changes depending on the data that I select which initiates the macro/code.
 
Upvote 0
OK, but you still did not answer my questions.

However you define "blank" (it doesn't really matter to me), I just need to know the answers to those questions.
In the situation in which it meets your definition of blank, what number exactly does this code return when you run it?
VBA Code:
Sub Test()
    Dim aLastRow As Long
    aLastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row - 3
    MsgBox aLastRow
End Sub

And on what run do you want this SUM function to should return zero?

And what would typically be the first row number in your sum function (if the first 17 rows are headers, then do the numbers you want to sum typically start on row 18, if there are any to sum)?
 
Upvote 0
OK, but you still did not answer my questions.

However you define "blank" (it doesn't really matter to me), I just need to know the answers to those questions.
In the situation in which it meets your definition of blank, what number exactly does this code return when you run it?
VBA Code:
Sub Test()
    Dim aLastRow As Long
    aLastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row - 3
    MsgBox aLastRow
End Sub

And on what run do you want this SUM function to should return zero?

And what would typically be the first row number in your sum function (if the first 17 rows are headers, then do the numbers you want to sum typically start on row 18, if there are any to sum)?
The code currently returns a sum of 0 in aLastRow when the cells above are blank/empty.
I would like for it to show blank/empty until the value does not equal 0.
The first 16 rows are headers. The first row number in my sum function starts at row 17.

Sorry for the confusion.
 
Upvote 0
Does this do what you want?
VBA Code:
Dim aLastRow As Long
aLastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row - 3

If aLastRow < 17 Then
    Range("B17") = 0
    Range("E17") = 0
    Range("J17") = 0
    Range("M17") = 0
Else
    Range("B" & aLastRow + 1).FormulaR1C1 = "=SUM(R[-" & aLastRow & "]C:R[-1]C)"
    Range("E" & aLastRow + 1).FormulaR1C1 = "=SUM(R[-" & aLastRow & "]C:R[-1]C)"
    Range("J" & aLastRow + 1).FormulaR1C1 = "=SUM(R[-" & aLastRow & "]C:R[-1]C)"
    Range("M" & aLastRow + 1).FormulaR1C1 = "=SUM(R[-" & aLastRow & "]C:R[-1]C)"
End If
 
Upvote 0
No, that code gave an error message. If that code was to work, it would still not be what I need. I need the formulas that I originally posted to always calculate, even after the macro is ran. I just need the field to turn blank if the sum value is 0 at any given point, meaning once I start entering numbers in the rows above, the field changes from blank to the actual sum value.

I have uploaded a couple of images that will hopefully fully explain. The first image (Excel.JPG) shows the 0's in the Total Results Row. The second image (Excel2.JPG) shows the actual totals once values have been entered on the sheet.
 

Attachments

  • Excel.JPG
    Excel.JPG
    83.7 KB · Views: 4
  • Excel2.JPG
    Excel2.JPG
    82.3 KB · Views: 3
Upvote 0
This did the trick, thank you!!! That solution never crossed my mind.
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,970
Members
449,095
Latest member
Mr Hughes

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