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'
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[/TR]
[TR]
[TD]xxx[/TD]
[TD]xxx[/TD]
[TD]Poster[/TD]
[TD]13[/TD]
[TD]62131[/TD]
[/TR]
[TR]
[TD]xxx[/TD]
[TD]xxx[/TD]
[TD]Index[/TD]
[TD]16[/TD]
[TD]62141[/TD]
[/TR]
</tbody>[/TABLE]


COPY TO Col E Cell to Col A Cell Than
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[/TR]
[TR]
[TD]62131[/TD]
[TD]xxx[/TD]
[TD]Poster[/TD]
[TD]13[/TD]
[TD]62131[/TD]
[/TR]
[TR]
[TD]62141[/TD]
[TD]xxx[/TD]
[TD]Index[/TD]
[TD]16[/TD]
[TD]62141[/TD]
[/TR]
</tbody>[/TABLE]


Delete Col E
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[/TR]
[TR]
[TD]62131[/TD]
[TD]xxx[/TD]
[TD]Poster[/TD]
[TD]13[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]62141[/TD]
[TD]xxx[/TD]
[TD]Index[/TD]
[TD]16[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
oh my.. should be


If Col C cell contain 'Poster' OR 'Index' than
Copy Col E 'value' to Col A Cell Than
clearcontents Col E 'value' (delete the data), not row or column
 
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
 
Upvote 0
hi yes! thanks! work great.


hmmm

i can add a Set ws = Sheets("path")?

Code:
Sub Poster_Index()
  
  Set ws = Sheets("path")
  With ws.Range("A1:E" & Range("C" & Rows.Count).End(xlUp).Row)
    ws.Columns(1).Value = Evaluate(Replace(Replace(Replace("IF(ISNUMBER(SEARCH(""|""&#C&""|"",""|Poster|Index|"")),[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E[/URL]  ,[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=A]#A[/URL]  )", "#C", .Columns(3).Address), "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E[/URL]  ", .Columns(5).Address), "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=A]#A[/URL]  ", .Columns(1).Address))
    ws.Columns(5).Value = Evaluate(Replace(Replace("IF(ISNUMBER(SEARCH(""|""&#C&""|"",""|Poster|Index|"")),"""",[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E[/URL]  )", "#C", .Columns(3).Address), "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E[/URL]  ", .Columns(5).Address))
  End With
End Sub





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|"")),[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E [/URL] ,[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=A]#A [/URL] )", "#C", .Columns(3).Address), "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E [/URL] ", .Columns(5).Address), "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=A]#A [/URL] ", .Columns(1).Address))
    .Columns(5).Value = Evaluate(Replace(Replace("IF(ISNUMBER(SEARCH(""|""&#C&""|"",""|Poster|Index|"")),"""",[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E [/URL] )", "#C", .Columns(3).Address), "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E [/URL] ", .Columns(5).Address))
  End With
End Sub
 
Upvote 0
hi yes! thanks! work great.


hmmm

i can add a Set ws = Sheets("path")?

Code:
Sub Poster_Index()
  
  Set ws = Sheets("path")
  With ws.Range("A1:E" & Range("C" & Rows.Count).End(xlUp).Row)
    ws.Columns(1).Value = Evaluate(Replace(Replace(Replace("IF(ISNUMBER(SEARCH(""|""&#C&""|"",""|Poster|Index|"")),[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E][URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E[/URL] [/URL]  ,[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=A][URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=A]#A[/URL] [/URL]  )", "#C", .Columns(3).Address), "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E][URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E[/URL] [/URL]  ", .Columns(5).Address), "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=A][URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=A]#A[/URL] [/URL]  ", .Columns(1).Address))
    ws.Columns(5).Value = Evaluate(Replace(Replace("IF(ISNUMBER(SEARCH(""|""&#C&""|"",""|Poster|Index|"")),"""",[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E][URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E[/URL] [/URL]  )", "#C", .Columns(3).Address), "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E][URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E[/URL] [/URL]  ", .Columns(5).Address))
  End With
End Sub
Almost. If you want to operate on sheet "path" I would add the blue bits and remove the red bits. Also, always best to declare (Dim) your variables.
Rich (BB code):
Sub Poster_Index()
  Dim ws As Worksheet
  
  Set ws = Sheets("path")
  With ws.Range("A1:E" & ws.Range("C" & ws.Rows.Count).End(xlUp).Row)
    <del>ws</del>.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))
    <del>ws</del>.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
 
Upvote 0
This code is what i want but...................

i cant get it code works if i had another code...


Code:
Sub Step4_Copy4Path()


Dim cel As Range, ws As Worksheet
Set ws = Sheets("path")


    'START of confirmation message box'
    response = MsgBox("Run Macro for" & vbNewLine & "Step 1: CopyRange?", vbYesNo)
        If response = vbNo Then
        MsgBox ("Macro Canceled!")
    Exit Sub
        End If
    'END of confirmation message box'
    
    
    ' Code 1: Copy sheet
    Application.ScreenUpdating = False
    With Sheets("CopyTranpose")  'Source worksheet name
    Set DestSh = Sheets("path")  'Destination worksheet name
        
        lr = .Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
        .Range(.Cells(2, "F"), .Cells(lr, "F")).Copy DestSh.Cells(2, "A")  'Ir = last column of x
        .Range(.Cells(2, "E"), .Cells(lr, "E")).Copy DestSh.Cells(2, "C")
        .Range(.Cells(2, "D"), .Cells(lr, "D")).Copy DestSh.Cells(2, "D")
        .Range(.Cells(2, "C"), .Cells(lr, "C")).Copy DestSh.Cells(2, "E")
        .Range(.Cells(2, "A"), .Cells(lr, "A")).Copy DestSh.Cells(2, "I")
        .Range(.Cells(2, "B"), .Cells(lr, "B")).Copy DestSh.Cells(2, "J")
    End With
        Application.ScreenUpdating = True
        
    
    Set ws = Sheets("path")
    With ws.Range("A1:E" & Range("C" & Rows.Count).End(xlUp).Row)
        ws.Columns(1).Value = Evaluate(Replace(Replace(Replace("IF(ISNUMBER(SEARCH(""|""&#C&""|"",""|Poster|Index|"")),[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E[/URL]  ,[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=A]#A[/URL]  )", "#C", .Columns(3).Address), "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E[/URL]  ", .Columns(5).Address), "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=A]#A[/URL]  ", .Columns(1).Address))
        ws.Columns(5).Value = Evaluate(Replace(Replace("IF(ISNUMBER(SEARCH(""|""&#C&""|"",""|Poster|Index|"")),"""",[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E[/URL]  )", "#C", .Columns(3).Address), "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E[/URL]  ", .Columns(5).Address))
    End With
  
  
      End
    
End Sub


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|"")),[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E [/URL] ,[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=A]#A [/URL] )", "#C", .Columns(3).Address), "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E [/URL] ", .Columns(5).Address), "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=A]#A [/URL] ", .Columns(1).Address))
    .Columns(5).Value = Evaluate(Replace(Replace("IF(ISNUMBER(SEARCH(""|""&#C&""|"",""|Poster|Index|"")),"""",[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E [/URL] )", "#C", .Columns(3).Address), "[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E]#E [/URL] ", .Columns(5).Address))
  End With
End Sub
 
Upvote 0
Hi Thanks.

This code is what i want but...................

i cant get it code works if i had another code...

Sub Step4_Copy4Path()


Code:
Dim cel As Range, ws As Worksheet
Set ws = Sheets("path")


    'START of confirmation message box'
    response = MsgBox("Run Macro for" & vbNewLine & "Step 1: CopyRange?", vbYesNo)
        If response = vbNo Then
        MsgBox ("Macro Canceled!")
    Exit Sub
        End If
    'END of confirmation message box'
    
    
    ' Code 1: Copy sheet
    Application.ScreenUpdating = False
    With Sheets("CopyTranpose")  'Source worksheet name
    Set DestSh = Sheets("path")  'Destination worksheet name
        
        lr = .Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
        .Range(.Cells(2, "F"), .Cells(lr, "F")).Copy DestSh.Cells(2, "A")  'Ir = last column of x
        .Range(.Cells(2, "E"), .Cells(lr, "E")).Copy DestSh.Cells(2, "C")
        .Range(.Cells(2, "D"), .Cells(lr, "D")).Copy DestSh.Cells(2, "D")
        .Range(.Cells(2, "C"), .Cells(lr, "C")).Copy DestSh.Cells(2, "E")
        .Range(.Cells(2, "A"), .Cells(lr, "A")).Copy DestSh.Cells(2, "I")
        .Range(.Cells(2, "B"), .Cells(lr, "B")).Copy DestSh.Cells(2, "J")
    End With
        Application.ScreenUpdating = True
        
    
    Set ws = Sheets("path")
    With ws.Range("A1:E" & Range("C" & Rows.Count).End(xlUp).Row)
        ws.Columns(1).Value = Evaluate(Replace(Replace(Replace("IF(ISNUMBER(SEARCH(""|""&#C&""|"",""|Poster|Index|"")),[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E"]#E[/URL]  ,[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=A"]#A[/URL]  )", "#C", .Columns(3).Address), "[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E"]#E[/URL]  ", .Columns(5).Address), "[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=A"]#A[/URL]  ", .Columns(1).Address))
        ws.Columns(5).Value = Evaluate(Replace(Replace("IF(ISNUMBER(SEARCH(""|""&#C&""|"",""|Poster|Index|"")),"""",[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E"]#E[/URL]  )", "#C", .Columns(3).Address), "[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=E"]#E[/URL]  ", .Columns(5).Address))
    End With
  
  
      End
    
End Sub

Almost. If you want to operate on sheet "path" I would add the blue bits and remove the red bits. Also, always best to declare (Dim) your variables.
Rich (BB code):
Sub Poster_Index()
  Dim ws As Worksheet
  
  Set ws = Sheets("path")
  With ws.Range("A1:E" & ws.Range("C" & ws.Rows.Count).End(xlUp).Row)
    <del>ws</del>.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))
    <del>ws</del>.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
 
Last edited:
Upvote 0
I had a code1 that copy the data from sheet1 to sheet2 at current Marco

So I want it to run the code1 and than this one (code2) under on Marco.

But it run together and mess up
 
Last edited:
Upvote 0
Still not very clear but I notice that you have not made the red & blue changes that I suggested in post 5. That would be a good place to start.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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