Use if then statement to check if text in a cell contains a string variable...

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
912
Office Version
  1. 365
Platform
  1. Windows
This code works perfectly every time. But the value of x must match EXACTLY the contents of the cell value
Code:
Sub SEARCHANDCOPY()
Dim datasheet As Worksheet
Dim reportsheet As Worksheet
Dim x As String
Dim finalrow As Integer
Dim i As Integer
Set datasheet = Worksheets("Sheet1")
Set reportsheet = Worksheets("Sheet2")
x = reportsheet.Range("H1").value
reportsheet.Range("A5:G200").ClearContents
datasheet.Select
finalrow = Cells(Rows.count, 1).End(xlUp).Row
 For i = 2 To finalrow
      If Cells(i, 4) = x Then
         Range(Cells(i, 1), Cells(i, 9)).copy
         reportsheet.Select
         Range("A200").End(xlUp).Offset(1, 0).PasteSpecial xlPasteFormulasAndNumberFormats
         datasheet.Select
      End If
 Next i
reportsheet.Select
Range("H1").Select
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
This code will find all cells which contain ONLY "pymt" and copy them to reportsheet.
Great.
BUT what if the value of x is CONTAINED WITHIN a cell with other text before and after x ?
Is there a way to use wildcard or pattern searches with x suchas **x** etc. so that the code will pick up all values of x whether
at the beginning, in the middle or at the end of any text in a cell ? I prefer to use an if then statement
if possible.
Please help on this. Will be very much appreciated.
THanks,
cr
 

Attachments

  • cellvalue.JPG
    cellvalue.JPG
    11.1 KB · Views: 15
  • cellvaluewithin.JPG
    cellvaluewithin.JPG
    11.3 KB · Views: 25

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
How about
VBA Code:
If InStr(1, Cells(i, 4), x, vbTextCompare) > 0 Then
 
Upvote 0
No data to test with but does this fix it?

Rich (BB code):
If Cells(i, 4) = x Then
If Cells(i, 4).Value Like "*" & x & "*" Then
 
Upvote 0
How about
VBA Code:
If InStr(1, Cells(i, 4), x, vbTextCompare) > 0 Then
This very simple line of code works absolutely great. The value of the variable can be found anywhere WITHIN a cell containing multiple lines
of text. This is very similar to using the FIND Lookin:(xlPartial) except it uses an if statement without having to bother with using a FIndnext
loop or a Do Until loop. Very helpful nd very simple. Thanks again Fluff
 
Upvote 0
If Cells(i, 4).Value Like "*" & x & "*" Then
Hi Peter - yes this also works perfectly. Obviously there's more than one way to do things in VBA. And it seems its a matter of knowing the entire vba coding statements realm

which I don't, to solve any problem. Thanks again for all your help. cr, Kingwood, Tx.
 
Last edited by a moderator:
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
And it seems its a matter of knowing the entire vba coding statements realm
? I don't think that any of us do that!

Anyway, glad you got something that worked for you. (y)
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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