If a cell in column B contains the word "Total" delete or hide the blank row above

Jasz74

New Member
Joined
Jul 25, 2022
Messages
4
Office Version
  1. 2003 or older
Platform
  1. Windows
Hi
I've tried searching for this but nothing quite matches (can only seem to find if the cell equals something delete that row the cell is in).

Can this be done? Can someone help me with the vba coding please?

Context - I have created a button that generates a formatted report based on pivot table data. I want it to subtotal cube and value for each container and if there are multiple orders in that container I want them separated by a blank row which is all fine, but then it puts this extra blank row between the last value for each container and the subtotal for each container and I don't want that blank line to show. Pivot table field settings either do both or none.

I've attached a screenshot of the report - the highlighted line is the one I don't want to show.
 

Attachments

  • excel.png
    excel.png
    18.6 KB · Views: 7

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Maybe this way
VBA Code:
Sub MM1()
Dim r As Long
For r = 2 To Cells(Rows.Count, "B").End(xlUp).Row
    If Cells(r, 2) = "Total" Then Rows(r - 1).Delete
Next r
End Sub
 
Upvote 0
Solution
VBA Code:
Sub Del()
Dim lr&, i&, j&, k&, c&, rng, arr(1 To 1000000, 1 To 1)
lr = Cells(Rows.Count, "B").End(xlUp).Row
rng = Range("B1:H" & lr).Value
On Error Resume Next
For i = 2 To lr
    If rng(i, 1) <> "" Then
       If UCase(rng(i, 1)) Like "*TOTAL" Then
            c = 0
            For j = 1 To 7
                c = c + Len(rng(i - 1, j))
            Next
            If c = 0 Then
                k = k + 1
                arr(k, 1) = "B" & i - 1
            End If
        End If
    End If
Next
For i = 1 To k
    Range(arr(i, 1)).Value = "#N/A"
Next
Columns("B").SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
End Sub
 
Upvote 0
Maybe this way
VBA Code:
Sub MM1()
Dim r As Long
For r = 2 To Cells(Rows.Count, "B").End(xlUp).Row
    If Cells(r, 2) = "Total" Then Rows(r - 1).Delete
Next r
End Sub
Almost - I had to tweak it slightly with a bit of bebo021999's code ;) works exactly as I need it to! Thanks

Dim r As Long
For r = 2 To sheets("Report").Range("B" & Rows.Count).End(xlUp).Row
If Sheets("Report").Cells(r, 2) Like "*Total" Then Sheets("Report").Rows(r - 1).EntireRow.Delete
Next r
 
Upvote 0
Almost - I had to tweak it slightly with a bit of bebo021999's code ;) works exactly as I need it to! Thanks

Dim r As Long
For r = 2 To sheets("Report").Range("B" & Rows.Count).End(xlUp).Row
If Sheets("Report").Cells(r, 2) Like "*Total" Then Sheets("Report").Rows(r - 1).EntireRow.Delete
Next r
Although it seems it is working, but take into account:
1) Avoid loop, calculate, read and write, delete, insert... directly on worksheet. It may slow down the code, if data was big enough (few thousands of rows)
2) When looping rows then delete rows, it should be looping from bottom:
For r = (last row) to 2 step -1
<then detele row>
...
Next
Otherwhile, some rows may be missed.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,490
Members
448,967
Latest member
visheshkotha

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