Searching and storing Column Number

  • Thread starter Thread starter rita.michail
  • Start date Start date
R

rita.michail

Hi,

Is there a way in VB I can search for a word in an excel file, and
then once found I store the column number of the cell where the word
is found?


Thanks
 
One way:
Sub oneWay()
Dim r As Long
Dim sString As String
sString = "fred"
On Error Resume Next
r = Cells.Find(What:=sString, After:=Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Column
On Error GoTo 0
If r = 0 Then
MsgBox "not found"
Else
MsgBox r
End If
End Sub
 
Thanks,

I tried that and I keep getting 1 as my answer but that's not where
the word is found..

Any idea why?
 
Did you look through column A to see if your string was anywhere in that column?

If you did, you may want to post your code and some details about what's in your
worksheet and where.
 
My Macro is exactly as JW wrote it: nothing before or after... also,
there's only one occurance of the word.

Thanks
 
Sub oneWay()
Dim r As Range
Dim sString As String


sString = "fred" ' change this to the word you are looking for

set r = Cells.Find(What:=sString, _
After:=Cells(rows.count, columns.count), _
LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False)

If r is nothing then
MsgBox "not found"
Else
application.Goto r, true
MsgBox r.column
End If
End Sub

--
Regards,
Tom Ogilvy

My Macro is exactly as JW wrote it: nothing before or after... also,
there's only one occurance of the word.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top