Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
KR KR is offline
external usenet poster
 
Posts: 121
Default Easy newbie question

I have a string in VBA (variable "mystring") and text strings in Sheet1,
column A (A2:A100). I need to identify (in VBA) whether "mystring" is
already in the range of A2:A100, but I will be repeating this operation
thousands of times, so I'm looking for something better than just looping
A2:A100 and checking against each value individually, something more like:

If mystring (is in) Sheet1.range("A2:A100") then....

but I can't find the right keyword- I've looked for "contains", "includes",
"in", and others.... what is the proper way to write this?

Many thanks,
Keith

--
The enclosed questions or comments are entirely mine and don't represent the
thoughts, views, or policy of my employer. Any errors or omissions are my
own.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Easy newbie question

Dim rng as Range
set rng = Range("A2:A100").Find(mystring, _
Lookin:=xlValues, Lookat:=xlpart)
if not rng is nothing then
msgbox "found in " & rng.Address
End if

--
Regards,
Tom Ogilvy


"KR" wrote in message
...
I have a string in VBA (variable "mystring") and text strings in Sheet1,
column A (A2:A100). I need to identify (in VBA) whether "mystring" is
already in the range of A2:A100, but I will be repeating this operation
thousands of times, so I'm looking for something better than just looping
A2:A100 and checking against each value individually, something more like:

If mystring (is in) Sheet1.range("A2:A100") then....

but I can't find the right keyword- I've looked for "contains",

"includes",
"in", and others.... what is the proper way to write this?

Many thanks,
Keith

--
The enclosed questions or comments are entirely mine and don't represent

the
thoughts, views, or policy of my employer. Any errors or omissions are my
own.




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Easy newbie question

Keith,

If you could have multiples of the same value:

Sub FindValues2()
Dim c As Range ' The cell found with what you want
Dim d As Range ' All the cells found with what you want
Dim myString As String
Dim firstAddress As String

'This can also be set other ways

myString = InputBox("Enter the key word for search", , "Hello")

With Range("A2:A100")

Set c = .Find(myString, LookIn:=xlValues, lookAt:=xlWhole)

If Not c Is Nothing Then
Set d = c
firstAddress = c.Address
Else:
MsgBox "Not Found"
End
End If

Set c = .FindNext(c)
If Not c Is Nothing And c.Address < firstAddress Then
Do
Set d = Union(d, c)
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address < firstAddress
End If
End With

'Then do what you want with all the cells that have been found, like:

MsgBox myString & " was found in " & d.Address(False, False)

End Sub

HTH,
Bernie
MS Excel MVP

"KR" wrote in message
...
I have a string in VBA (variable "mystring") and text strings in Sheet1,
column A (A2:A100). I need to identify (in VBA) whether "mystring" is
already in the range of A2:A100, but I will be repeating this operation
thousands of times, so I'm looking for something better than just looping
A2:A100 and checking against each value individually, something more like:

If mystring (is in) Sheet1.range("A2:A100") then....

but I can't find the right keyword- I've looked for "contains",

"includes",
"in", and others.... what is the proper way to write this?

Many thanks,
Keith

--
The enclosed questions or comments are entirely mine and don't represent

the
thoughts, views, or policy of my employer. Any errors or omissions are my
own.




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Easy newbie question

Since he said:

right keyword- I've looked for "contains", "includes",

"in", and others....

you might want LookAt:=xlPart rather than LookAt:=xlWhole

--
Regards,
Tom Ogilvy



"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Keith,

If you could have multiples of the same value:

Sub FindValues2()
Dim c As Range ' The cell found with what you want
Dim d As Range ' All the cells found with what you want
Dim myString As String
Dim firstAddress As String

'This can also be set other ways

myString = InputBox("Enter the key word for search", , "Hello")

With Range("A2:A100")

Set c = .Find(myString, LookIn:=xlValues, lookAt:=xlWhole)

If Not c Is Nothing Then
Set d = c
firstAddress = c.Address
Else:
MsgBox "Not Found"
End
End If

Set c = .FindNext(c)
If Not c Is Nothing And c.Address < firstAddress Then
Do
Set d = Union(d, c)
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address < firstAddress
End If
End With

'Then do what you want with all the cells that have been found, like:

MsgBox myString & " was found in " & d.Address(False, False)

End Sub

HTH,
Bernie
MS Excel MVP

"KR" wrote in message
...
I have a string in VBA (variable "mystring") and text strings in Sheet1,
column A (A2:A100). I need to identify (in VBA) whether "mystring" is
already in the range of A2:A100, but I will be repeating this operation
thousands of times, so I'm looking for something better than just

looping
A2:A100 and checking against each value individually, something more

like:

If mystring (is in) Sheet1.range("A2:A100") then....

but I can't find the right keyword- I've looked for "contains",

"includes",
"in", and others.... what is the proper way to write this?

Many thanks,
Keith

--
The enclosed questions or comments are entirely mine and don't represent

the
thoughts, views, or policy of my employer. Any errors or omissions are

my
own.






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Easy newbie question

As always, Tom, an excellent point.

Bernie

"Tom Ogilvy" wrote in message
...
Since he said:

right keyword- I've looked for "contains", "includes",

"in", and others....

you might want LookAt:=xlPart rather than LookAt:=xlWhole

--
Regards,
Tom Ogilvy



"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Keith,

If you could have multiples of the same value:

Sub FindValues2()
Dim c As Range ' The cell found with what you want
Dim d As Range ' All the cells found with what you want
Dim myString As String
Dim firstAddress As String

'This can also be set other ways

myString = InputBox("Enter the key word for search", , "Hello")

With Range("A2:A100")

Set c = .Find(myString, LookIn:=xlValues, lookAt:=xlWhole)

If Not c Is Nothing Then
Set d = c
firstAddress = c.Address
Else:
MsgBox "Not Found"
End
End If

Set c = .FindNext(c)
If Not c Is Nothing And c.Address < firstAddress Then
Do
Set d = Union(d, c)
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address < firstAddress
End If
End With

'Then do what you want with all the cells that have been found, like:

MsgBox myString & " was found in " & d.Address(False, False)

End Sub

HTH,
Bernie
MS Excel MVP

"KR" wrote in message
...
I have a string in VBA (variable "mystring") and text strings in

Sheet1,
column A (A2:A100). I need to identify (in VBA) whether "mystring" is
already in the range of A2:A100, but I will be repeating this

operation
thousands of times, so I'm looking for something better than just

looping
A2:A100 and checking against each value individually, something more

like:

If mystring (is in) Sheet1.range("A2:A100") then....

but I can't find the right keyword- I've looked for "contains",

"includes",
"in", and others.... what is the proper way to write this?

Many thanks,
Keith

--
The enclosed questions or comments are entirely mine and don't

represent
the
thoughts, views, or policy of my employer. Any errors or omissions are

my
own.








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
Real Newbie newbie question Dave New Users to Excel 0 January 10th 07 07:55 PM
Newbie question Vince Excel Discussion (Misc queries) 12 August 21st 06 06:22 PM
Newbie Question - Subtraction Formula Question [email protected] Excel Discussion (Misc queries) 3 May 5th 06 05:50 PM
new user with easy question? not easy for me speakeztruth New Users to Excel 5 June 3rd 05 09:40 PM
newbie question cyclingsal Excel Programming 1 May 7th 04 08:52 PM


All times are GMT +1. The time now is 12:27 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"