VBA: If Range Contains Cell Value Copy than del

harky

Active Member
Joined
Apr 8, 2010
Messages
405
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
I need help :)


If Col C cell contain 'Poster' OR 'Index' COPY TO
Col E Cell to Col A Cell Than
Delete Col E E

the flow is

*xxx refer to some text

If Col C cell contain 'Poster' OR 'Index'
ABCDE
xxxxxxPoster1362131
xxxxxxIndex1662141

<tbody>
</tbody>


COPY TO Col E Cell to Col A Cell Than
ABCDE
62131xxxPoster1362131
62141xxxIndex1662141

<tbody>
</tbody>


Delete Col E
ABCDE
62131xxxPoster13
62141xxxIndex16

<tbody>
</tbody>
 
Last edited:
somehow like this code but dunno why it not working


Code:
Set sh = Sheets("path")


lr = sh.Cells(Rows.Count, "C").End(xlUp).Row


Set rng = sh.Range("C2:C" & lr)
    For Each c In rng
        If LCase(c.Value) = "Index" Then
            sh.Range("E" & c.Row).Cut sh.Range("A" & c.Row)
        End If
    Next
End Sub
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I manage to use this code to make it work..... but using cut and paste will cause the formula to had error.

Some how, the formula which i have it on Col F.
=IF(OR(A2="",C2=""),"",B2&C2&"_"&D2&""&E2&"")


When i active this code, it will cut E to A but end up my formula is having error.
=IF(OR(#REF!="",C16=""),"",B16&C16&"_"&D16&""&A16&"")

any idea, how to avoid tht?

Code:
Sub CutNPaste()
Dim sh As Worksheet, lr As Long, rng As Range


Set sh = ThisWorkbook.Worksheets("path")
lr = sh.Cells(Rows.Count, "C").End(xlUp).Row


Set rng = sh.Range("C2:C" & lr)
    For Each c In rng
        If LCase(c.Value) = "poster" Then
            sh.Range("E" & c.Row).Cut sh.Range("A" & c.Row)
        End If
    Next
    
Set rng = sh.Range("C2:C" & lr)
    For Each c In rng
        If LCase(c.Value) = "index" Then
            sh.Range("E" & c.Row).Cut sh.Range("A" & c.Row)
        End If
    Next
   
End Sub
 
Last edited:
Upvote 0
Sorry to flood..

manage to solve it somehow.. but i think the code can improve better :)


Code:
Sub CutNPasteNDelete()
Dim sh As Worksheet, lr As Long, rng As Range


Set sh = ThisWorkbook.Worksheets("path")
lr = sh.Cells(Rows.Count, "C").End(xlUp).Row


Set rng = sh.Range("C2:C" & lr)
    For Each c In rng
        If LCase(c.Value) = "poster" Then
            sh.Range("E" & c.Row).Copy sh.Range("A" & c.Row)
            sh.Range("E" & c.Row).Clear
        End If
    Next


Set rng = sh.Range("C2:C" & lr)
    For Each c In rng
        If LCase(c.Value) = "index" Then
            sh.Range("E" & c.Row).Copy sh.Range("A" & c.Row)
            sh.Range("E" & c.Row).Clear
        End If
    Next
End Sub
 
Upvote 0
Give this a try in a copy of your workbook.
I have assumed no empty cells among the column A data & same for column C.

Rich (BB code):
Sub Poster_Index()
  With Range("A1:E" & Range("C" & Rows.Count).End(xlUp).Row)
    .Columns(1).Value = Evaluate(Replace(Replace(Replace("IF(ISNUMBER(SEARCH(""|""&#C&""|"",""|Poster|Index|"")),#E ,#A )", "#C", .Columns(3).Address), "#E ", .Columns(5).Address), "#A ", .Columns(1).Address))
    .Columns(5).Value = Evaluate(Replace(Replace("IF(ISNUMBER(SEARCH(""|""&#C&""|"",""|Poster|Index|"")),"""",#E )", "#C", .Columns(3).Address), "#E ", .Columns(5).Address))
  End With
End Sub

A little more compact...
Code:
Sub Test()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Range("A1:A" & LastRow) = Evaluate(Replace("IF((C1:C#=""Poster"")+(C1:C#=""Index""),E1:E#,A1:A#)", "#", LastRow))
  Range("E1:E" & LastRow) = Evaluate(Replace("IF((C1:C#=""Poster"")+(C1:C#=""Index""),"""",E1:E#)", "#", LastRow))
End Sub
 
Upvote 0
i need to retain the original format which i had..

If replace, does tht mean it wont copy the format which i had on col E?

A little more compact...
Code:
Sub Test()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Range("A1:A" & LastRow) = Evaluate(Replace("IF((C1:C#=""Poster"")+(C1:C#=""Index""),E1:E#,A1:A#)", "#", LastRow))
  Range("E1:E" & LastRow) = Evaluate(Replace("IF((C1:C#=""Poster"")+(C1:C#=""Index""),"""",E1:E#)", "#", LastRow))
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,595
Members
449,089
Latest member
Motoracer88

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