Search Cell for the letter D or d and if it is there place yes in another cell.

Bigheadz

New Member
Joined
Mar 26, 2018
Messages
6
Hello,

First post.

I'm building a MACRO which I want to look at the cells in column 18 "Avail Designation" one at a time for the letter 'D' or "d". If the cell contains either letter then place "Y" in the corresponding Docking cell in column 20. If not place a "N" in Cell. The Cell i, 18 could contain many different variations of DSRA, SRA, EDSRA, PMA, DPMA to include numbers and dashes. (EDSRA-1 or SRA(d)).

This is my original code. I started to add each string the MACRO might encounter but there has to be a better way. I have researched IfStr and InStr and the use of Contains but cant make it work correctly. It either places a "Y" in every i, 20 cell or crashes,

[TABLE="width: 500"]
<tbody>[TR]
[TD][TABLE="class: grid, width: 200, align: left"]
<tbody>[TR]
[TD]Avail Designation[/TD]
[TD]
Docking
[/TD]
[/TR]
[TR]
[TD]DMP[/TD]
[TD] Y[/TD]
[/TR]
[TR]
[TD]DSRA1[/TD]
[TD] Y[/TD]
[/TR]
[TR]
[TD]SRA2-2[/TD]
[TD]N[/TD]
[/TR]
[TR]
[TD]SRA1-2[/TD]
[TD]N[/TD]
[/TR]
[TR]
[TD]SRA(d)[/TD]
[TD] Y[/TD]
[/TR]
[TR]
[TD]DMP[/TD]
[TD] Y[/TD]
[/TR]
[TR]
[TD]DSRA1[/TD]
[TD] Y[/TD]
[/TR]
[TR]
[TD]SRA2-2[/TD]
[TD]N[/TD]
[/TR]
</tbody>[/TABLE]
<strike></strike>
[/TD]
[/TR]
[TR]
[TD]
[/TD]
[/TR]
[TR]
[TD]
[/TD]
[/TR]
</tbody>[/TABLE]



Code:
Sub SMPPRECALC()
Application.ScreenUpdating = False


Dim totalrows As Double


totalrows = (ActiveWorkbook.Worksheets("Master file").Range("N1", Worksheets("Master file").Range("N2").End(xlDown)).Rows.Count)

For i = 2 To totalrows
   If Cells(i, 18).Value = "DPMA" Or Cells(i, 18).Value = "DSRA" Or Cells(i, 18).Value = "DMP" Or Cells(i, 18).Value = "EDSRA" Or Cells(i, 18).Value = "SRA(d)" Then
      Cells(i, 20).Value = "Y"
    Else
       Cells(i, 20).Value = "N"
    End If
Next i
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi,

Would you consider a formula solution?


Book1
AB
1Avail DesignationDocking
2DMPY
3DSRA1Y
4SRA2-2N
5SRA1-2N
6SRA(d)Y
7DMPY
8DSRA1Y
9SRA2-2N
Sheet12
Cell Formulas
RangeFormula
B2=IF(ISNUMBER(SEARCH("D",A2)),"Y","N")


Formula copied down.
 
Upvote 0
Sure Jtakw,

But my goal is to have it part of a larger macro, not shown, which populates a much larger data sheet. Plus I want to learn something cool. :)
 
Upvote 0
How about
Code:
Sub SMPPRECALC()

   Dim Cl As Range
Application.ScreenUpdating = False

   With Worksheets("Master file")
      For Each Cl In .Range("R2", .Range("R" & Rows.Count).End(xlUp))
         If InStr(1, Cl.Value, "d", vbTextCompare) > 0 Then
            Cl.Offset(, 2).Value = "Y"
         Else
            Cl.Offset(, 2).Value = "N"
         End If
      Next Cl
   End With
End Sub
 
Upvote 0
Here is another macro that you can consider...
Code:
[table="width: 500"]
[tr]
	[td]Sub SMPPRECALC()
  Dim Addr As String
  Addr = "A2:A" & Cells(Rows.Count, "A").End(xlUp).Row
  Range(Addr).Offset(, 1) = Evaluate("IF({1},MID(""YN"",2-(ISNUMBER(SEARCH(""d""," & Addr & "))),1))")
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
How about
Code:
Sub SMPPRECALC()

   Dim Cl As Range
Application.ScreenUpdating = False

   With Worksheets("Master file")
      For Each Cl In .Range("R2", .Range("R" & Rows.Count).End(xlUp))
         If InStr(1, Cl.Value, "d", vbTextCompare) > 0 Then
            Cl.Offset(, 2).Value = "Y"
         Else
            Cl.Offset(, 2).Value = "N"
         End If
      Next Cl
   End With
End Sub

Works like a champ. Thank you. I see you used the R column from my code. I just removed the sub and it fitted right in. I also walked through the process of figuring out why it works. I love learning this stuff.
 
Upvote 0
I see you used the R column from my code.
You were not able to adapt the code I posted in Message #5 to your actual worksheet and range, were you? I'm sorry... I had forgotten to adjust the code from the test range I used to develop it to the actual sheet name and range you wanted it for. To keep the thread record straight, here is what my code would have looked like had I done that.
Code:
[table="width: 500"]
[tr]
	[td]Sub SMPPRECALC()
  Dim Addr As String
  Addr = "'Master File'!R2:R" & Sheets("Master File").Cells(Rows.Count, "R").End(xlUp).Row
  Range(Addr).Offset(, 2) = Evaluate("IF({1},MID(""YN"",2-(ISNUMBER(SEARCH(""d""," & Addr & "))),1))")
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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