use vba/ macro to insert cell if find specific text/ sign

senget

New Member
Joined
Jan 27, 2017
Messages
9
I have a sheet with 1500 rows and columns from A to EZ, each cell contains the data are very diverse.
I want to use VBA to find each cell that has the sign "/" and if found "/" will add the letter "cm:" in front of the cell contents.
example:
cell contents "cigarettes/marlboro" to "cm:cigarettes/marlboro" or
cell contents "oral b/toothpaste/toothbrush" becomes "cm:oral b/toothpaste/toothbrush"

Any help is greatly appreciated
Thank you in advance
 
Code:
Sub Main()
  Dim i&, r As Range, c As Range
  i = Cells(Rows.Count, "A").End(xlUp).Row
  Set r = Range("A2", "EZ" & i)
  For Each c In r
    If InStr(c, "/") > 0 Then c.Value2 = "cm:" & c.Value2
  Next c
End Sub
 
Last edited:
Upvote 0
thanks sir, the code has been going well.
but my question is less complete, if the cell is in accordance (has had cm) to be ignored if it has any cm

Code:
Sub Main()
  Dim i&, r As Range, c As Range
  i = Cells(Rows.Count, "A").End(xlUp).Row
  Set r = Range("A2", "EZ" & i)
  For Each c In r
    If InStr(c, "/") > 0 Then c.Value2 = "cm:" & c.Value2
  Next c
End Sub
 
Upvote 0
Code:
If InStr(c, "/") > 0 And _
      Left(c.Value2, 3) <> "cm:" _
      Then c.Value2 = "cm:" & c.Value2
 
Upvote 0
where i put the code sir?

like this?
Code:
Sub Main()
  Dim i&, r As Range, c As Range
  i = Cells(Rows.Count, "A").End(xlUp).Row
  Set r = Range("A2", "EZ" & i)
  For Each c In r
      If InStr(c, "/") > 0 And _
      Left(c.Value2, 3) <> "cm:" _
      Then c.Value2 = "cm:" & c.Value2
  Next c
End Sub


Code:
If InStr(c, "/") > 0 And _
      Left(c.Value2, 3) <> "cm:" _
      Then c.Value2 = "cm:" & c.Value2
 
Upvote 0

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