Macro to delete cell and adjacent cell if criteria not met?

stevenash1367

New Member
Joined
Nov 14, 2011
Messages
30
I will give a watered down version:

Need to see if cells in column Q is equal to "ABC" or "EFG"
If not then would like to delete the cell in column Q and adjacent cell in column R and shift cell leftwards ( so cells in column S would become Q and T would become R).

So lets say Q501 is not equal to ABC or EFG then delete Q501 and R501 and move S501 and T501 over to Q501 and R501.

Thanks for your help:)
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I will give a watered down version:

Need to see if cells in column Q is equal to "ABC" or "EFG"
If not then would like to delete the cell in column Q and adjacent cell in column R and shift cell leftwards ( so cells in column S would become Q and T would become R).

So lets say Q501 is not equal to ABC or EFG then delete Q501 and R501 and move S501 and T501 over to Q501 and R501.

Thanks for your help:)

Have you ever played basketball for the Phoenix Suns?

Maybe:

Code:
Sub stevenash1367()

Dim i As Long

Dim lr As Long

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

For i = lr To 2 Step -1

    If Range("Q" & i).Value <> "ABC" Or Range("Q" & i).Value <> "EFG" Then
    
        Range("S" & i).Cut Range("Q" & i)
        Range("T" & i).Cut Range("R" & i)
        
    End If
    
Next i

'

End Sub
 
Upvote 0
Well due to the recent lockout I have decided to become a excel expert.:LOL:

Thanks for the help but it is basically replacing column Q and R with S and T.
 
Upvote 0
Well due to the recent lockout I have decided to become a excel expert.:LOL:

Thanks for the help but it is basically replacing column Q and R with S and T.


Sorry about the lockout, but you millionaires should have nothing to complain about.

Does it replace irregardless of "ABC" and "EFG" lines? Is it absolutely required for cell shift. Let me know, and I'll try to come up with something.
 
Upvote 0
And yes its replacing irregardless.

Does this make any difference?

Code:
Sub stevenash1367()

Dim i As Long

Dim lr As Long

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

For i = lr To 2 Step -1

Range(Range("Q" & i), Range("T" & i)) = Trim(Range(Range("Q" & i), Range("T" & i)))

    If Range("Q" & i).Value = "ABC" Or Range("Q" & i).Value = "EFG" Then
    
        Range("Q" & i).Value = Range("Q" & i).Value
        
    Else
    
        Range(Range("Q" & i), Range("R" & i)).Value = Range(Range("S" & i), Range("T" & i)).Value
        
        Range(Range("S" & i), Range("T" & i)).Delete shift:=xlToLeft
    
        
    End If
    
Next i

'

End Sub
 
Upvote 0
If Range("Q" & i).Value = "ABC" Or Range("Q" & i).Value = "EFG" Then Range("Q" & i).Value = Range("Q" & i).Value Else Range(Range("Q" & i), Range("R" & i)).Value = Range(Range("S" & i), Range("T" & i)).Value Range(Range("S" & i), Range("T" & i)).Delete shift:=xlToLeft

I was debugging and got an error when I am at this step.
</pre>
 
Upvote 0

Forum statistics

Threads
1,214,576
Messages
6,120,354
Members
448,956
Latest member
Adamsxl

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