Home |
Search |
Today's Posts |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
On Mon, 3 Apr 2006 02:53:55 -0500, KH_GS
wrote: Hi I have a column containing text phrases in each cell, if there's any word in the cell ends with "abc", I want that phrase to be in a new column. Data in each cell: appleabc is not red roadabc is long green apple roadblock apple is fruitabc sweet fruit Output(in a new column): appleabc is not red roadabc is long apple is fruitabc Here's the code that doesn't work ![]() Sub PrintEnd_ING() Dim Cell As Range Dim myString As String Application.DisplayAlerts = False Application.ScreenUpdating = False x = ActiveCell.Row y = ActiveCell.Column For Each Cell In Range(Selection, Selection.End(xlDown)) myString = Cells(x, y).Value If myString Like "*abc" Or myString = "*abc? " Then ActiveSheet.Cells(x, y + 3).Value = myString ActiveSheet.Cells(x, y + 4).Value = Cell.Offset(0, 1).Value x = x + 1 End If Next Application.DisplayAlerts = True Application.ScreenUpdating = True End Sub With all these variations in string processing that you've been posting, you might do better by implementing Regular Expressions to define your string. Here's an example. It requires setting a reference in the VB Editor (see the Tools/References item from the main menu) to "Microsoft VBScript Regular Expressions 5.5" which should show up in the list. Also, you may want to change Src and Dest and how you derive them. The routine will move the contents of cells that meet your test into the Dest column and not leave blanks. If you want to have blanks for cells that don't make the test, that's easy to do also. I believe the routine is documented well enough so that you can understand the basic principals. But you may have to do some research for info on constructing Regular Expressions. In the below the portion of Pattern that is "\b" means the end of a word. So "abc\b" means any sequence of "abc" followed by a word boundary. Just another approach. ================================================== ==== Option Explicit Sub ING() 'set up to use Regular Expressions Dim objRegExp As RegExp 'set a pattern to look for words ending in "abc" Const Pattern As String = "abc\b" Dim c As Range Dim Src As Range, Dest As Range Dim i As Long ' Create a regular expression object. Set objRegExp = New RegExp 'Set the pattern by using the Pattern property. objRegExp.Pattern = Pattern ' Set Case Insensitivity. objRegExp.IgnoreCase = True 'Set global applicability. objRegExp.Global = True Set Src = [a1:a100] 'Clear and Set Dest Set Dest = Src.Offset(0, 1) Dest.Clear Set Dest = Dest.Resize(1, 1) For Each c In Src 'Test whether the String can be compared. If objRegExp.Test(c.Text) = True Then Dest.Offset(i, 0).Value = c.Value i = i + 1 End If Next c End Sub =============================== --ron |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Return Text based upon text matching | Excel Worksheet Functions | |||
matching text to text in excel | Excel Worksheet Functions | |||
Help with Matching Text Fields - Then Moving the Matching Cells Side by Side | Excel Discussion (Misc queries) | |||
Matching Text | Excel Worksheet Functions | |||
Matching 2 text columns | Excel Discussion (Misc queries) |