Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default how to search a string in VBA

In the excel file I have:

123 A5679 REF
A111 ACC
BLOCK 545 A2456
COPY Abbc23

I would like to search for string beginning with "A" and at least 3
numbers following after letter A. Then I want to copy that string to
the new cell.
For example, the first cell, the string I want is A5679
second cell, A111
third cell, A2456
forth cell, there is no string I want
because after letter A, it is not a number.

So in the new sheet, I should have
A5679
A111
A2456

Thanks a lot,

Tammy

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 258
Default how to search a string in VBA

Hi Tammy,

see help for like operator.

Regards,
Ivan

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 718
Default how to search a string in VBA

This example is based on Regular Expressions.

Alt-F11 to get VBE
ToolsReferences
Tick Microsoft VBScript Regular Expressions 1.0

HTH
--
AP

'-----------------------------------
Sub ExtractRefs()
Const sDestWSName = "Results"
Dim rSource As Range
Dim wsDest As Worksheet
Dim rDest As Range
Dim re As RegExp
Dim mMatch As MatchCollection
Dim i As Long

'Get Results worksheet
'(create if doesn't exist, clear if exists)
On Error Resume Next
Set wsDest = Worksheets(sDestWSName)
On Error GoTo 0
If wsDest Is Nothing Then
Set wsDest = Worksheets.Add
wsDest.Name = sDestWSName
Else
wsDest.UsedRange.ClearContents
End If
Set rDest = wsDest.Range("A1")

Set re = New RegExp
re.Pattern = "A\d{3,}"
re.Global = True

With Worksheets("Sheet1")
For Each rSource In Range( _
.Range("A1"), _
.Cells(Rows.Count, "A").End(xlUp))

Set mMatch = re.Execute(rSource.Value)
For i = 0 To mMatch.Count - 1
rDest.Value = mMatch(i).Value
Set rDest = rDest.Offset(1, 0)
Next i

Next rSource
End With

End Sub
'--------------------------

"trammy" a écrit dans le message de news:
...
In the excel file I have:

123 A5679 REF
A111 ACC
BLOCK 545 A2456
COPY Abbc23

I would like to search for string beginning with "A" and at least 3
numbers following after letter A. Then I want to copy that string to
the new cell.
For example, the first cell, the string I want is A5679
second cell, A111
third cell, A2456
forth cell, there is no string I want
because after letter A, it is not a number.

So in the new sheet, I should have
A5679
A111
A2456

Thanks a lot,

Tammy



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default how to search a string in VBA

Ardus,
It works just for first row. when it gets into second row, the
rsource.value is empty.
Thanks,
Tammy

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 718
Default how to search a string in VBA

Oooops: forgot a . (pediod)

'-----------------
Sub ExtractRefs()
Const sDestWSName = "Results"
Dim rSource As Range
Dim wsDest As Worksheet
Dim rDest As Range
Dim re As RegExp
Dim mMatch As MatchCollection
Dim i As Long

'Get Results worksheet
'(create if doesn't exist, clear if exists)
On Error Resume Next
Set wsDest = Worksheets(sDestWSName)
On Error GoTo 0
If wsDest Is Nothing Then
Set wsDest = Worksheets.Add
wsDest.Name = sDestWSName
Else
wsDest.UsedRange.ClearContents
End If
Set rDest = wsDest.Range("A1")

Set re = New RegExp
re.Pattern = "A\d{3,}"
re.Global = True

With Worksheets("Sheet1")
For Each rSource In .Range( _
.Range("A1"), _
.Cells(Rows.Count, "A").End(xlUp))

Set mMatch = re.Execute(rSource.Value)
For i = 0 To mMatch.Count - 1
rDest.Value = mMatch(i).Value
Set rDest = rDest.Offset(1, 0)
Next i

Next rSource
End With

End Sub
'-------------
"Ardus Petus" a écrit dans le message de news:
...
This example is based on Regular Expressions.

Alt-F11 to get VBE
ToolsReferences
Tick Microsoft VBScript Regular Expressions 1.0

HTH
--
AP

'-----------------------------------
Sub ExtractRefs()
Const sDestWSName = "Results"
Dim rSource As Range
Dim wsDest As Worksheet
Dim rDest As Range
Dim re As RegExp
Dim mMatch As MatchCollection
Dim i As Long

'Get Results worksheet
'(create if doesn't exist, clear if exists)
On Error Resume Next
Set wsDest = Worksheets(sDestWSName)
On Error GoTo 0
If wsDest Is Nothing Then
Set wsDest = Worksheets.Add
wsDest.Name = sDestWSName
Else
wsDest.UsedRange.ClearContents
End If
Set rDest = wsDest.Range("A1")

Set re = New RegExp
re.Pattern = "A\d{3,}"
re.Global = True

With Worksheets("Sheet1")
For Each rSource In Range( _
.Range("A1"), _
.Cells(Rows.Count, "A").End(xlUp))

Set mMatch = re.Execute(rSource.Value)
For i = 0 To mMatch.Count - 1
rDest.Value = mMatch(i).Value
Set rDest = rDest.Offset(1, 0)
Next i

Next rSource
End With

End Sub
'--------------------------

"trammy" a écrit dans le message de news:
...
In the excel file I have:

123 A5679 REF
A111 ACC
BLOCK 545 A2456
COPY Abbc23

I would like to search for string beginning with "A" and at least 3
numbers following after letter A. Then I want to copy that string to
the new cell.
For example, the first cell, the string I want is A5679
second cell, A111
third cell, A2456
forth cell, there is no string I want
because after letter A, it is not a number.

So in the new sheet, I should have
A5679
A111
A2456

Thanks a lot,

Tammy







  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default how to search a string in VBA

Thanks Ardus,

But the rsource.value is empty. after the first row

Tammy

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default how to search a string in VBA

One way:

Dim rCell As Range
Dim rDest As Range
Dim nPos As Long
Dim nEnd As Long
Dim sTemp As String
Set rDest = Worksheets("Sheet2").Range("A1")
For Each rCell In Range("A1:A" & _
Range("A" & Rows.Count).End(xlUp).Row)
sTemp = rCell.Text
If sTemp Like "*A###*" Then
nPos = InStr(1, sTemp, "A")
Do While nPos < 0
If Mid(sTemp, nPos, 4) Like "A###" Then
nEnd = InStr(nPos, sTemp, " ")
If nEnd = 0 Then nEnd = 32767
rDest.Value = Mid(sTemp, nPos, nEnd - nPos)
Set rDest = rDest.Offset(1, 0)
Exit Do
End If
nPos = InStr(nPos + 1, sTemp, "A")
Loop
End If
Next rCell

In article .com,
"trammy" wrote:

In the excel file I have:

123 A5679 REF
A111 ACC
BLOCK 545 A2456
COPY Abbc23

I would like to search for string beginning with "A" and at least 3
numbers following after letter A. Then I want to copy that string to
the new cell.
For example, the first cell, the string I want is A5679
second cell, A111
third cell, A2456
forth cell, there is no string I want
because after letter A, it is not a number.

So in the new sheet, I should have
A5679
A111
A2456

Thanks a lot,

Tammy

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
to search for a string and affect data if it finds the string? Shwaman Excel Worksheet Functions 1 January 11th 06 12:56 AM
search a string withing a string : find / search hangs itarnak[_9_] Excel Programming 4 October 24th 05 03:19 PM
VBA function : How to search a string in another string? bibi-phoque Excel Programming 5 April 19th 05 06:24 PM
Excel XP VBA code to search all macro code in Excel module for specific search string criteria Ed[_18_] Excel Programming 4 May 20th 04 02:08 PM
Excel XP VBA code to search all macro code in Excel module for specific search string criteria Frank Kabel Excel Programming 0 May 19th 04 08:11 PM


All times are GMT +1. The time now is 01:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"