Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default simple code that does a search for me and returns a string

I've been recording macros to figure how to do what I need to do, but can't
quite get it...

I need my code to search column A, and find the cell that contains (in it's
value) the string strText. strText will just be part of it's value, but
there will only be one cell in column A that matches.
I need this asigned to a new string, strFound
thanks, much.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default simple code that does a search for me and returns a string

Option Explicit
Sub testme()

Dim strText as string
Dim strFound as string
Dim FoundCell as range

strText = "sometexthere"

with worksheets("Sheet9999999") '<-- change the sheet name here
with .range("a:a")
set foundcell = .cells.Find(What:=strtext, _
After:=.cells(.cells.count), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
end with
end with

if foundcell is nothing then
msgbox "Not found"
else
strFound = foundcell.value
end if
end sub

Southern at Heart wrote:

I've been recording macros to figure how to do what I need to do, but can't
quite get it...

I need my code to search column A, and find the cell that contains (in it's
value) the string strText. strText will just be part of it's value, but
there will only be one cell in column A that matches.
I need this asigned to a new string, strFound
thanks, much.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default simple code that does a search for me and returns a string

We will search for "happiness"

Sub findit()
strText = "happiness"
n = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To n
v = Cells(i, 1).Value
If InStr(v, strText) 0 Then
strFound = v
End If
Next
MsgBox (strFound)
End Sub

The entire sentence containing "happiness" gets output.
--
Gary''s Student - gsnu200826


"Southern at Heart" wrote:

I've been recording macros to figure how to do what I need to do, but can't
quite get it...

I need my code to search column A, and find the cell that contains (in it's
value) the string strText. strText will just be part of it's value, but
there will only be one cell in column A that matches.
I need this asigned to a new string, strFound
thanks, much.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default simple code that does a search for me and returns a string

Another possibility (using Gary''s Student's search word "happiness")...

Sub FindMe()
Dim strText As String, strFound As String
strText = "happiness"
On Error Resume Next
strFound = WorksheetFunction.Index(Range("A:A"), WorksheetFunction. _
Match("*" & strText & "*", Range("A:A"), 0))
MsgBox strFound
End Sub

--
Rick (MVP - Excel)


"Southern at Heart" wrote in
message ...
I've been recording macros to figure how to do what I need to do, but
can't
quite get it...

I need my code to search column A, and find the cell that contains (in
it's
value) the string strText. strText will just be part of it's value, but
there will only be one cell in column A that matches.
I need this asigned to a new string, strFound
thanks, much.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default Thanks.

thanks. I ended up with:

Function findit(strText As String) As String

n = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To n
v = Cells(i, 1).Value
If InStr(v, strText) 0 Then
strFound = v
End If
Next
findit = strFound
End Function

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
Search web source code for specific string [email protected] Excel Programming 7 November 7th 08 06:14 AM
Ascertaining whether a search in VBA code returns a string or a reserved word Paul Martin Excel Programming 8 August 31st 06 07:33 AM
search string command to answer simple Yes or No [email protected] Excel Worksheet Functions 0 February 21st 06 04:01 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 12:47 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"