Remove rows with certain characters

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
smart people of Mrexcel, I have this code to calculate the total amount of a certain item, and display the total after the last row in the excel.
after this row is added after the last row, I want to remove any row that contains the keyword "Sealant" (these can be any row, any number of rows), except the newly added row
what is the best approach to this problem, to ensure the new row will not be removed ?

VBA Code:
Sub SealantConseal()
    Dim lrNew As Long, lr As Long
    lr = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    lrNew = lr
    sr = 2

    With Range("M1", Cells(Rows.Count, "M").End(3))
        .Replace What:="""", Replacement:=vbNullString, LookAt:=xlPart
        .Replace What:="/JOINT", Replacement:=vbNullString, LookAt:=xlPart
        
    n = WorksheetFunction.SumIfs(Range("M" & sr & ":M" & lr), Range("K" & sr & ":K" & lr), "*JOINT SEALANT*")
    lrNew = lrNew + 1
    Cells(lrNew, "A") = Cells(lr, "A")
    Cells(lrNew, "B") = "."
    Cells(lrNew, "C") = Int(((n / 12 / 14.5) + 0.5) * 2)
    Cells(lrNew, "D") = "F51019"
    Cells(lrNew, "I") = "Purchased"
    Cells(lrNew, "K") = "CS-102 Sealant"
    If Cells(lrNew, "C").Value = 0 Then
        Rows(lrNew).Delete
    End If
End Sub
 
:huh: Are you sure that your code logic is correct?
VBA Code:
    If Cells(lrNew, "C").Value = 0 Then
        Rows(lrNew).Delete
This deletes the new line if the value in column C is zero. The only way that column C can be zero is if the SUMIFS calculation comes to -87
That seems very odd to me and I'm wondering if you really mean to delete that new row if the SUMIFS calculation comes to zero (ie n = 0)?
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
:unsure: Are you sure that your code logic is correct?
VBA Code:
    If Cells(lrNew, "C").Value = 0 Then
        Rows(lrNew).Delete
This deletes the new line if the value in column C is zero. The only way that column C can be zero is if the SUMIFS calculation comes to -87
That seems very odd to me and I'm wondering if you really mean to delete that new row if the SUMIFS calculation comes to zero (ie n = 0)?
Peter, the macros are for cleaning up a large number of csv files. These csv then get imported into another software .
Any item with a qty of 0 does not need to be imported, so I can either delete the whole row, or remove the item # (removing the item # will prevent this item from getting imported as well)
one of the benefits of removing the whole new row, is that it is easier (less items to look at) for the users to check the files before importing
 
Upvote 0
:unsure: Are you sure that your code logic is correct?
VBA Code:
    If Cells(lrNew, "C").Value = 0 Then
        Rows(lrNew).Delete
This deletes the new line if the value in column C is zero. The only way that column C can be zero is if the SUMIFS calculation comes to -87
That seems very odd to me and I'm wondering if you really mean to delete that new row if the SUMIFS calculation comes to zero (ie n = 0)?
Another way to approach this problem, is to simply not generate the new row at all when the qty is 0, this saves the headache of having to remove a row that just got added to the file.
With other peoples' help, I did try this route, but the code I had kept adding new row to the file even when the qty is 0 :(
 
Upvote 0
Any item with a qty of 0
Nothing in the code mentions qty, so I have no way of knowing what is meant by that.

My point was that the only way that
Cells(lrNew, "C").Value = 0
is if
Int(((n / 12 / 14.5) + 0.5) * 2) = 0
and the only way that
Int(((n / 12 / 14.5) + 0.5) * 2) = 0
is if
n = -87

Since n = WorksheetFunction.SumIfs(Range("M" & sr & ":M" & lr), Range("K" & sr & ":K" & lr), "*JOINT SEALANT*")
that seemed very odd to me.
 
Upvote 0
Nothing in the code mentions qty, so I have no way of knowing what is meant by that.

My point was that the only way that
Cells(lrNew, "C").Value = 0
is if
Int(((n / 12 / 14.5) + 0.5) * 2) = 0
and the only way that
Int(((n / 12 / 14.5) + 0.5) * 2) = 0
is if
n = -87

Since n = WorksheetFunction.SumIfs(Range("M" & sr & ":M" & lr), Range("K" & sr & ":K" & lr), "*JOINT SEALANT*")
that seemed very odd to me.
Peter, I saw this same problem when I was running some tests. I've removed the + 0.5, and tweaked a couple of numbers to get to the ideal results.
 
Upvote 0
Glad to hear you have a good outcome. Could you share the final code?
of course, see below for full code

VBA Code:
Sub SealantConseal()
  Dim lrNew As Long, lr As Long
  Dim sr As Long, n As Double, i As Long
  lr = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
  lrNew = lr
  sr = 2

  With Range("M1", Cells(Rows.Count, "M").End(3))
    .Replace what:="""", Replacement:=vbNullString, Lookat:=xlPart
    .Replace what:="/JOINT", Replacement:=vbNullString, Lookat:=xlPart
  End With

  n = WorksheetFunction.SumIfs(Range("M" & sr & ":M" & lr), Range("K" & sr & ":K" & lr), "*JOINT SEALANT*")
  lrNew = lrNew + 1
  Cells(lrNew, "A") = Cells(lr, "A")
  Cells(lrNew, "B") = "."
  Cells(lrNew, "C") = Int((n / 12 / 14) * 2)
  Cells(lrNew, "D") = "F51019"
  Cells(lrNew, "I") = "Purchased"
  Cells(lrNew, "K") = "CS-102 Sealant"
  If Cells(lrNew, "C").Value = 0 Then
    Rows(lrNew).Delete
  Else

    Range("K1:K" & lr).Replace "*Sealant*", "#N/A", xlWhole
    On Error Resume Next
    Range("K1:K" & lr).SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
    On Error GoTo 0
  End If
End Sub
 
Upvote 0
see below for full code
Thanks. I was just going to suggest some possible changes that you may want to consider, though keeping the existing working code would be fine too.

You mentioned earlier one of the things that I had in mind
Another way to approach this problem, is to simply not generate the new row at all when the qty is 0, this saves the headache of having to remove a row that just got added to the file.
The other thing that I was going to mention is that if you want, the whole new line can be entered at once rather than one cell at a time.

VBA Code:
Sub SealantConseal_a()
  Dim lr As Long, sr As Long, n As Double, i As Long
  
  lr = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
  sr = 2
  With Range("M1", Cells(Rows.Count, "M").End(3))
    .Replace what:="""", Replacement:=vbNullString, Lookat:=xlPart
    .Replace what:="/JOINT", Replacement:=vbNullString, Lookat:=xlPart
  End With

  n = WorksheetFunction.SumIfs(Range("M" & sr & ":M" & lr), Range("K" & sr & ":K" & lr), "*JOINT SEALANT*")
  If Int((n / 12 / 14) * 2) <> 0 Then
    Cells(lr + 1, "A").Resize(1, 11).Value = _
      Array(Cells(lr, "A"), ".", Int((n / 12 / 14) * 2), "F51019", "", "", "", "", "Purchased", "", "CS-102 Sealant")
    Range("K1:K" & lr).Replace "*Sealant*", "#N/A", xlWhole
    On Error Resume Next
    Range("K1:K" & lr).SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
    On Error GoTo 0
  End If
End Sub

BTW, I wasn't exactly correct when I made the statement below. :oops:
However, it seems like the focus on it has picked up something that wasn't quite right so "all's well that ends well". :)
The only way that column C can be zero is if the SUMIFS calculation comes to -87
 
Upvote 0
Thanks. I was just going to suggest some possible changes that you may want to consider, though keeping the existing working code would be fine too.

You mentioned earlier one of the things that I had in mind

The other thing that I was going to mention is that if you want, the whole new line can be entered at once rather than one cell at a time.

VBA Code:
Sub SealantConseal_a()
  Dim lr As Long, sr As Long, n As Double, i As Long
 
  lr = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
  sr = 2
  With Range("M1", Cells(Rows.Count, "M").End(3))
    .Replace what:="""", Replacement:=vbNullString, Lookat:=xlPart
    .Replace what:="/JOINT", Replacement:=vbNullString, Lookat:=xlPart
  End With

  n = WorksheetFunction.SumIfs(Range("M" & sr & ":M" & lr), Range("K" & sr & ":K" & lr), "*JOINT SEALANT*")
  If Int((n / 12 / 14) * 2) <> 0 Then
    Cells(lr + 1, "A").Resize(1, 11).Value = _
      Array(Cells(lr, "A"), ".", Int((n / 12 / 14) * 2), "F51019", "", "", "", "", "Purchased", "", "CS-102 Sealant")
    Range("K1:K" & lr).Replace "*Sealant*", "#N/A", xlWhole
    On Error Resume Next
    Range("K1:K" & lr).SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
    On Error GoTo 0
  End If
End Sub

BTW, I wasn't exactly correct when I made the statement below. :oops:
However, it seems like the focus on it has picked up something that wasn't quite right so "all's well that ends well". :)
Peter, looking at your code
And I just love the whole idea of adding the whole new row at once instead of adding one cell at a time

I will apply this to many other parts of the macro as well, I think using array will make the entire macro easier to manage as well.

Thanks !
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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