Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
dan dan is offline
external usenet poster
 
Posts: 866
Default String in a range

What is the Syntax to check if a string is in a range?
Thanks Dan
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default String in a range


Hi Dan,

Try something like:

'=============
Public Sub Tester()
Dim SH As Worksheet
Dim Rng As Range
Dim Rng2 As Range
Const sStr As String = "ABC"

Set SH = ActiveSheet

With SH
Set Rng = .Range("A1")
Set Rng2 = .Range("A1:A10")
End With

MsgBox InStr(1, Rng.Value, sStr, vbTextCompare) 0
MsgBox Application.CountIf(Rng2, sStr) 0

End Sub
'<<=============


---
Regards,
Norman


"Dan" wrote in message
...
What is the Syntax to check if a string is in a range?
Thanks Dan



  #3   Report Post  
Posted to microsoft.public.excel.programming
dan dan is offline
external usenet poster
 
Posts: 866
Default String in a range

Thanks

"Norman Jones" wrote:


Hi Dan,

Try something like:

'=============
Public Sub Tester()
Dim SH As Worksheet
Dim Rng As Range
Dim Rng2 As Range
Const sStr As String = "ABC"

Set SH = ActiveSheet

With SH
Set Rng = .Range("A1")
Set Rng2 = .Range("A1:A10")
End With

MsgBox InStr(1, Rng.Value, sStr, vbTextCompare) 0
MsgBox Application.CountIf(Rng2, sStr) 0

End Sub
'<<=============


---
Regards,
Norman


"Dan" wrote in message
...
What is the Syntax to check if a string is in a range?
Thanks Dan




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default String in a range

Perhaps something like:

Function well_is_it(sr As Range, r As Range) As Boolean
well_is_it = False
s = sr.Value
For Each rr In r
If InStr(1, rr.Value, s) 0 Then
well_is_it = True
Exit Function
End If
Next
End Function
--
Gary''s Student - gsnu200733


"Dan" wrote:

What is the Syntax to check if a string is in a range?
Thanks Dan

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default String in a range


Hi Dan.

MsgBox Application.CountIf(Rng2, sStr) 0


Should have been:

MsgBox Application.CountIf(Rng2.Value, sStr) 0


Another possibility might be::

'=============
Public Sub Tester2()
Dim SH As Worksheet
Dim Rng As Range
Dim rCell As Range
Dim blFound As Boolean
Const sStr As String = "ABC"

Set SH = ActiveSheet
Set Rng = SH.Range("A1:A10")

For Each rCell In Rng.Cells
If InStr(1, rCell.Value, sStr, vbTextCompare) 0 Then
blFound = True
Exit For
End If
Next rCell

MsgBox Prompt:="The string " & Chr(34) & sStr _
& Chr(34) & " was found = " & blFound
End Sub
'<<=============

You could also use the Find method.


---
Regards,
Norman


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
Find the POSITION IN A RANGE of text in a string that matches value(s) in a range Cornell1992 Excel Programming 0 March 14th 06 07:19 PM
Retrieving range string from named range clapper Excel Programming 4 October 13th 05 03:09 PM
Translate range name passed as string to a custom function to range addresses! agarwaldvk[_25_] Excel Programming 3 September 7th 04 12:47 PM
Passing a String in Array to Range as String [email protected] Excel Programming 2 September 1st 04 01:13 AM
String to Range? Kevin G[_2_] Excel Programming 1 January 31st 04 07:08 PM


All times are GMT +1. The time now is 04:02 AM.

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

About Us

"It's about Microsoft Excel"