VBA Formula to Calculate a Range between blank rows and loop

lichldo

Board Regular
Joined
Apr 19, 2022
Messages
65
Office Version
  1. 365
Platform
  1. MacOS
Please see attached image for sample sheet!

I have data sorted by row H and I have a blank row in between each different group. In that blank row, I need to total the values in that group for columns I through O. I want to be able to run a macro do to this. I know I'll need to set the range (which I'd do $1:$300) and then loop it so that if its blank, then the next cell is the top of the new range but I'm getting stuck of how to do that.

Thanks for any help!
 

Attachments

  • Screen Shot 2022-04-19 at 3.55.43 PM.png
    Screen Shot 2022-04-19 at 3.55.43 PM.png
    146.1 KB · Views: 35

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hello! I think this will accomplish what you're looking for:
Excel Formula:
Sub Totals()
Dim i As Long
Dim lrow As Long
Dim PrevRow As Long

lrow = Cells(Rows.Count, 1).End(xlUp).Row
PrevRow = 1

For i = 2 To lrow + 1
    If Cells(i, 1) = "" Then
        Range("I" & i) = "=Sum(I" & PrevRow + 1 & ":I" & i - 1 & ")"
        Range("I" & i).Copy
        Range("J" & i & ":O" & i).PasteSpecial xlPasteFormulas
        Application.CutCopyMode = False
        
        PrevRow = i
        
        Range("I" & i & ":O" & i).Font.Bold = True 'You can comment this out if you do not want the total values to be bolded.
        
    End If
    
    
Next i


End Sub

This will automatically determine the last row in your data set, so no need to hard code a range. I also added a line in the code that will bold the new total values so they are easy to see. Feel free to delete that line if you don't want that addition.

I hope this helps!
 
Upvote 0
Another option
VBA Code:
Sub lichldo()
   Dim Rng As Range
   
   For Each Rng In Range("H2", Range("H" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
      With Rng.Offset(Rng.Count, 1).Resize(1, 7)
         .Formula = "=sum(" & Rng.Offset(, 1).Address(0, 0) & ")"
         .Value = .Value
      End With
   Next Rng

End Sub
 
Upvote 0
Hello! I think this will accomplish what you're looking for:
Excel Formula:
Sub Totals()
Dim i As Long
Dim lrow As Long
Dim PrevRow As Long

lrow = Cells(Rows.Count, 1).End(xlUp).Row
PrevRow = 1

For i = 2 To lrow + 1
    If Cells(i, 1) = "" Then
        Range("I" & i) = "=Sum(I" & PrevRow + 1 & ":I" & i - 1 & ")"
        Range("I" & i).Copy
        Range("J" & i & ":O" & i).PasteSpecial xlPasteFormulas
        Application.CutCopyMode = False
       
        PrevRow = i
       
        Range("I" & i & ":O" & i).Font.Bold = True 'You can comment this out if you do not want the total values to be bolded.
       
    End If
   
   
Next i


End Sub

This will automatically determine the last row in your data set, so no need to hard code a range. I also added a line in the code that will bold the new total values so they are easy to see. Feel free to delete that line if you don't want that addition.

I hope this helps!

That works perfectly. I'm wondering if there is a way to in addition to bold the total values, have it put any number under 50,000 in green text color, and any number over 75,000 in red text color?
 
Upvote 0
That works perfectly. I'm wondering if there is a way to in addition to bold the total values, have it put any number under 50,000 in green text color, and any number over 75,000 in red text color?
Actually I'm running into an issue. I will need to reuse this on new tabs as they get added and then it won't work?
 
Upvote 0
The below code will change the text to green for anything above 50k. This macro should work across multiple worksheets. So I can trouble shoot what's wrong, could you send me a screenshot of:
  1. The sheet you are running the macro it on and receiving an error
  2. The error message you're getting
  3. When you press debug on the error message, send me a screenshot of the code that is highlighted

VBA Code:
Sub Totals()
Dim i As Long
Dim lrow As Long
Dim PrevRow As Long
Dim j As Long

lrow = Cells(Rows.Count, 1).End(xlUp).Row
PrevRow = 1

For i = 2 To lrow + 1
    If Cells(i, 1) = "" Then
        Range("I" & i) = "=Sum(I" & PrevRow + 1 & ":I" & i - 1 & ")"
        Range("I" & i).Copy
        Range("J" & i & ":O" & i).PasteSpecial xlPasteFormulas
        Application.CutCopyMode = False
        
        PrevRow = i
        
        Range("I" & i & ":O" & i).Font.Color = 0 'Turn all to black font
        Range("I" & i & ":O" & i).Font.Bold = True 'You can comment this out if you do not want the total values to be bolded.
        
        For j = 9 To 15
            If Cells(i, j) > 50000 Then Cells(i, j).Font.Color = -11489280 'Turn everything > 50k to green font
        Next j
          
    End If
       
Next i


End Sub
 
Upvote 0
The below code will change the text to green for anything above 50k. This macro should work across multiple worksheets. So I can trouble shoot what's wrong, could you send me a screenshot of:
  1. The sheet you are running the macro it on and receiving an error
  2. The error message you're getting
  3. When you press debug on the error message, send me a screenshot of the code that is highlighted

VBA Code:
Sub Totals()
Dim i As Long
Dim lrow As Long
Dim PrevRow As Long
Dim j As Long

lrow = Cells(Rows.Count, 1).End(xlUp).Row
PrevRow = 1

For i = 2 To lrow + 1
    If Cells(i, 1) = "" Then
        Range("I" & i) = "=Sum(I" & PrevRow + 1 & ":I" & i - 1 & ")"
        Range("I" & i).Copy
        Range("J" & i & ":O" & i).PasteSpecial xlPasteFormulas
        Application.CutCopyMode = False
       
        PrevRow = i
       
        Range("I" & i & ":O" & i).Font.Color = 0 'Turn all to black font
        Range("I" & i & ":O" & i).Font.Bold = True 'You can comment this out if you do not want the total values to be bolded.
       
        For j = 9 To 15
            If Cells(i, j) > 50000 Then Cells(i, j).Font.Color = -11489280 'Turn everything > 50k to green font
        Next j
         
    End If
      
Next i


End Sub
thanks so much, this is almost perfect! Any way to get it to omit Row 2? It's somehow trying to total the months in the headers, see image
 

Attachments

  • Screen Shot 2022-04-24 at 3.47.04 PM.png
    Screen Shot 2022-04-24 at 3.47.04 PM.png
    27.9 KB · Views: 9
Upvote 0
Try this:
VBA Code:
Sub Totals()
Dim i As Long
Dim lrow As Long
Dim PrevRow As Long
Dim StartRow As Integer
Dim j As Long

lrow = Cells(Rows.Count, 1).End(xlUp).Row

If Cells(2, 1) = "" Then
    StartRow = 3
    PrevRow = 2
Else
    StartRow = 2
    PrevRow = 1
End

For i = StartRow To lrow + 1
    If Cells(i, 1) = "" Then
        Range("I" & i) = "=Sum(I" & PrevRow + 1 & ":I" & i - 1 & ")"
        Range("I" & i).Copy
        Range("J" & i & ":O" & i).PasteSpecial xlPasteFormulas
        Application.CutCopyMode = False
       
        PrevRow = i
       
        Range("I" & i & ":O" & i).Font.Color = 0 'Turn all to black font
        Range("I" & i & ":O" & i).Font.Bold = True 'You can comment this out if you do not want the total values to be bolded.
       
        For j = 9 To 15
            If Cells(i, j) > 50000 Then Cells(i, j).Font.Color = -11489280 'Turn everything > 50k to green font
        Next j
         
    End If
      
Next i


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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