2 cells same row match declared string, delete shift cells up.

jamada

Active Member
Joined
Mar 23, 2006
Messages
323
Hi, If on same row, and when Col"E"(5) AND Col"R"(18) BOTH equal MYSTRING, I would like the row/cellrange deleted and shiftedup... have tried many variation's with my limited skillset here, and am stuck. Can someone please assist, ThankYou!!


Dim Lastrow As Long
Dim i As Long
Dim MyString As String
MyString = "XXXxx XX:XX X"
Lastrow = Range("L" & Rows.Count).End(xlUp).Row

For i = Lastrow To 2 Step -1
If Cells(i, 5) = MyString And Cells(i, 18) = MyString Then
Range(Cells(i, 1), Cells(i, 21))..Delete Shift:=xlUp

End If

Next i
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Can do it this way:

Code:
Public Sub DeleteMyWord()
' Run this one
  Const strMY_WORD = "XXXxx XX:XX X"
  Call DeleteRows(strMY_WORD, True)
End Sub

Private Sub DeleteRows(ByVal strFindWord As String, _
              Optional ByVal blnMatchCase As Boolean = False)
  
  Const strSHEET_NAME = "Sheet2" '<= set sheet name here
  Dim lngLastRow As Long
  Dim lngRowNum As Long
  
  With ThisWorkbook.Sheets(strSHEET_NAME)
    lngLastRow = .Range("L" & .Rows.Count).End(xlUp).Row
    For lngRowNum = lngLastRow To 2 Step -1
      If blnMatchCase Then
        If .Range("E" & lngRowNum).Value = strFindWord Then
          If .Range("R" & lngRowNum).Value = strFindWord Then
            .Range("A" & lngRowNum & ":U" & lngRowNum).Delete Shift:=xlUp
          End If
        End If
      Else
        If UCase(.Range("E" & lngRowNum).Value) = UCase(strFindWord) Then
          If UCase(.Range("R" & lngRowNum).Value) = UCase(strFindWord) Then
            .Range("A" & lngRowNum & ":U" & lngRowNum).Delete Shift:=xlUp
          End If
        End If
      End If
    Next lngRowNum
  End With
End Sub
 
Upvote 0
Your code look correct only thing which can cause trouble is use of ".." before delete and replace it with "." (Provided column L is duly filled If not replace that with either column "E" or column "R").
 
Upvote 0
ParamRay, Thank You for your solution. Slightly (alot) above my comprehension LOL.
On the other hand your IF statements provided me with another way I'm more familiar with. Thank you though!

Swapnil, I deleted the extra . and still had difficulties.

Here is my end solution which works well for me!... Thank You Again.

Dim Lastrow As Long
Dim i As Long
Dim MyString As String
MyString = "XXXxx XX:XX X"
Lastrow = Range("L" & Rows.Count).End(xlUp).Row

For i = Lastrow To 2 Step -1
If Cells(i, 5) = MyString Then
If Cells(i, 18) = MyString Then
Range(Cells(i, 1), Cells(i, 21)).Delete Shift:=xlUp
Else
If Cells(i, 5) = MyString Then
Range(Cells(i, 1), Cells(i, 12)).Delete Shift:=xlUp
End If
End If
End If

If Cells(i, 18) = MyString Then
Range(Cells(i, 12), Cells(i, 21)).Delete Shift:=xlUp
End If
Next i
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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