If cell contains letters R and P

emukiss10

Board Regular
Joined
Nov 17, 2017
Messages
201
Hello,

I like to copy row based on multiple conditions and I hacve a problem with letters. I like to check if cell in column O contains letters P and R, and if so, copy row to next sheet.

my code is (with not working P and R)

Code:
Dim cell1 As RangeDim lastRow1 As Long, i As Long


lastRow1 = Range("N" & Rows.Count).End(xlUp).Row
i = 2


For Each cell1 In Sheets(1).Range("N1:N" & lastRow1)
    If cell1.Value = "TRANSFERING" [B]And Cells(cell1.Row, 15) [/B]=[B] "*P*" And Cells(cell1.Row, 15) [/B]= [B]"*R*" [/B]Then
        cell1.EntireRow.Copy Sheets("fullList").Cells(i, 1)
        i = i + 1
    End If
Next

Please help :)

Best Regards
W.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Rich (BB code):
    If cell1.Value = "TRANSFERING" And Cells(cell1.Row, 15) Like "*P*" And Cells(cell1.Row, 15) Like "*R*" Then

WBD
 
Upvote 0
Works Great!

Can You modify it to accept not any P and R but P and R with numbers like :

P11111111111 (value nor lenght does not matter)
R1111115555555

or

P 0000000000
R 1111111111111

(there can be space between P/R and numbers)

I dont wont to catch P 11111111 with name maRk - beacuse of R in name.
 
Upvote 0
If it has to start with P or R then just remove the first asterisk and group the conditions:

Code:
    If cell1.Value = "TRANSFERING" And (Cells(cell1.Row, 15) Like "P*" Or Cells(cell1.Row, 15) Like "R*") Then

WBD
 
Upvote 0
It doesnt... the cell may look like this


P 000000000 R 000000000
or
P111222211R11122222
or
P,121212121,R222111121

and it is correct but I dont want

P111222111 MaR2017
 
Last edited:
Upvote 0
If you are using Sheets(1) then you should use it when you use Cells, ie Sheets(1).Cells.
 
Upvote 0
Then you're in regular expression territory and your original code becomes something more like this:

Code:
Dim cell1 As Range
Dim lastRow1 As Long, i As Long
Dim regex As Object

lastRow1 = Range("N" & Rows.Count).End(xlUp).Row
i = 2

Set regex = CreateObject("VBScript.RegExp")
regex.Global = True
regex.Pattern = "P[\s,]?[0-9]+[\s,]?R[\s,]?[0-9]+"

For Each cell1 In Sheets(1).Range("N1:N" & lastRow1)
    If cell1.Value = "TRANSFERING" And regex.Test(Cells(cell1.Row, 15).Value) Then
        cell1.EntireRow.Copy Sheets("fullList").Cells(i, 1)
        i = i + 1
    End If
Next

WBD
 
Upvote 0
Thank You WBD,

unfortunetly it still catches letters in names like AUREIL (the R) :(

After letters P and R there will allways be a special char like " ", ",", ".", "/", "-" etc or number without spaces (or two spaces lik "P 121212121" but never other letters
 
Upvote 0
The regular expression above most certainly does not match on AUREIL. What is the exact contents of the cell that is matching?

WBD
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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