Excel VBA - Search column for text and insert text in another cell

sparkytech

Board Regular
Joined
Mar 6, 2018
Messages
96
Office Version
  1. 365
  2. 2019
I need to create code to search column "C" for any cells that contain "REQ", and then insert the text "REQ" in the same row in Column "Y". My code is not working and any help would be appreciated!

Code:
If ActiveCell.Value == REQ" Then    ActiveCell.offset(0,25).Value = "REQ" End If
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Will a col C cell contain exactly "REQ" or might it contain
ABC REQ
IT IS REQUIRED
req
It Is Required
 
Upvote 0
Code:
Sub Test()

	Dim rngEnd      As Range
	Dim rngBeg      As Range
	Dim iCell       As Range
	Dim strSearch   As String


	Set rngBeg = Range("C1")
	Set rngEnd = Range("C" & Range("C1").End(xlDown).Row)


	strSearch = "REQ"


	For Each iCell In Range(rngBeg, rngEnd)
		If InStr(iCell.Value, strSearch) Then
			iCell.Offset(0, 25).Value = "REQ"
		End If
	Next iCell
	
End Sub
 
Upvote 0
The cell will contain other text as well as "REQ.
@ Frank_Excel - I tried your code, but am having no luck so far.
 
Last edited:
Upvote 0
The cell will contain other text as well as "REQ.
Try
Code:
Sub REQ()
  With Range("C1", Range("C" & Rows.Count).End(xlUp))
    .Offset(, 23).Value = Evaluate("if(isnumber(search(""REQ""," & .Address & ")),""REQ"","""")")
  End With
End Sub
 
Upvote 0
Thanks Peter, that works perfect! How would I go about adding 2 other "text checks" with different text?

(i.e.: Look for REQ2, and put REQ2 in cell)
 
Upvote 0
How about
Code:
Sub REQ()
   Dim i As Long
   Dim Ary As Variant
   Ary = Array("REQ", "REQ2")
   For i = 0 To UBound(Ary)
      With Range("C1", Range("C" & Rows.count).End(xlUp))
        .Offset(, 23).Value = Evaluate(Replace("if(isnumber(search(#," & .Address & ")),#," & .Offset(, 23).Address & ")", "#", Chr(34) & Ary(i) & Chr(34)))
      End With
   Next i
End Sub
 
Upvote 0
Thanks to all for the quick solutions, it is greatly appreciated! I should have clarified my request for 2 additional text checks. Basically, I am not sure how to add another 2 checks in this same routine. I am very new to VBA, so I figure a lot out by trial and error, and I am not sure how to add these additional functions.


If columns "C" and "D" contain data, and columns "F" AND "G" are both blank, then put "Sproject" in the "Y" column
If columns "C", "D", "F", and "G" all contain data, then put "Project" in column "Y"
Else do nothing

Thanks in advance!
 
Upvote 0
Thanks to all for the quick solutions, it is greatly appreciated! I should have clarified my request for 2 additional text checks. Basically, I am not sure how to add another 2 checks in this same routine. I am very new to VBA, so I figure a lot out by trial and error, and I am not sure how to add these additional functions.


If columns "C" and "D" contain data, and columns "F" AND "G" are both blank, then put "Sproject" in the "Y" column
If columns "C", "D", "F", and "G" all contain data, then put "Project" in column "Y"
Else do nothing

Thanks in advance!
I would change to this approach
Code:
Sub ColY()
  With Range("Y2:Y" & Range("C" & Rows.Count).End(xlUp).Row)
    .Formula = "=IF(COUNTA(C2:D2)=2,IF(F2&G2="""",""Sproject"",IF(COUNTA(F2:G2)=2,""Project"","""")),"""")"
    .Value = .Value
  End With
End Sub
 
Upvote 0
That also works great, thanks! So, hopefully just one more question: how can I combine these 2 routines into one? They each work great independently, but one will overwrite what the other just did. I also tweaked them a little to work better with possible entries on my sheet.


Need to combine this:


Code:
With Range("Y2:Y" & Range("C" & Rows.Count).End(xlUp).Row)
    .Formula = "=IF(COUNTA(C2)=1,IF(E2&F2="""",""Super Project"",IF(COUNTA(F2)=1,""Project"","""")),"""")"
    .value = .value
  End With


With this:


Code:
With Range("C1", Range("C" & Rows.Count).End(xlUp))
    .Offset(, 22).value = Evaluate("if(isnumber(search(""REQ""," & .Address & ")),""Outage"","""")")
  End With


Thanks again for all of the help! I am learning a lot by working through this.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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