VBA Code Fix - calculating a group of rows, excluding amount if column contains keywords

lichldo

Board Regular
Joined
Apr 19, 2022
Messages
65
Office Version
  1. 365
Platform
  1. MacOS
Hello, I have the following code that is almost working, but I can't crack the last bit.

I have a sheet where I have groups of rows (meaning a blank row in between each set) and in that blank row, I want to total the values of column N, but exclude the value from the total if column F contains the text "pre-sales" or "non-billable". There might be other text in F, but if it contains those keywords at all, it needs to be excluded.

The code works to total the groups I want, put it in the correct place, and with the correct formatting - but it is not excluding the values that have those keywords in F.

VBA Code:
Sub ProjectTotals()
    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 ' Exclude rows 1 and the last row with text
        If InStr(1, Cells(i, 6), "pre-sales") = 0 And InStr(1, Cells(i, 6), "non-billable") = 0 Then ' Exclude rows with "pre-sales" or "non-billable" in column F
            If InStr(1, Cells(i, 14), "pre-sales") = 0 And InStr(1, Cells(i, 14), "non-billable") = 0 Then ' Exclude rows with "pre-sales" or "non-billable" in column N
                If Cells(i, 14) = "" Then
                    Range("N" & i).Formula = "=SUM(N" & PrevRow + 1 & ":N" & i - 1 & ")"
                    Range("N" & i).Copy
                    Range("N" & i).PasteSpecial xlPasteFormulasAndNumberFormats
                    Application.CutCopyMode = False
                    
                    PrevRow = i
                    
                    Range("N" & i).Interior.Color = RGB(255, 255, 0) ' Yellow color
                    Range("N" & i).Font.Bold = True
                End If
            End If
        End If
    Next i
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi @lichldo . thanks for postion on MrExcel.

Try the following:
VBA Code:
Sub ProjectTotals()
  Dim i As Long
  Dim tot As Double
  
  For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row - 1 ' Exclude rows 1 and the last row with text
    If InStr(1, LCase(Range("F" & i).Value), "pre-sales") = 0 And InStr(1, LCase(Range("F" & i).Value), "non-billable") = 0 Then  ' Exclude rows with "pre-sales" or "non-billable" in column F
      With Range("N" & i)
        tot = tot + .Value
        If .Value = "" Then
          .Value = tot
          .Interior.Color = RGB(255, 255, 0) ' Yellow color
          .Font.Bold = True
          tot = 0
        End If
      End With
    End If
  Next i
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------​
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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