+ Reply to Thread
Results 1 to 4 of 4

Find Text String via variable

  1. #1
    Registered User
    Join Date
    12-14-2009
    Location
    home
    MS-Off Ver
    Excel 2007
    Posts
    8

    Find Text String via variable

    Hi

    I need to find a word in another workbook, I think the problem is with "Cells.Find(What:=text).Select"
    The error is receive is runtime error 91 object variable or with block variable not set



    Sub Find()
    Dim text As String
    Dim text1 As String

    ThisWorkbook.Activate

    'Find row/cell to pick up string
    locate = Cells.Find(What:="Operator", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate


    text = ActiveCell.text


    'Filter out required string - string contains the word normally contained in <> brackets
    text = Mid(text, 21, 6)

    'MsgBox Right(text, 8)

    'Activate Workbook
    On Error GoTo msg
    Workbooks("Test.csv").Activate

    'This is where I want to find the word in new workbook, but fails
    Cells.Find(What:=text).Select


    'Move left to User ID and Copy
    ActiveCell.Offset(0, -3).Activate
    ActiveCell.Copy

    'Paste User ID in this workbook
    ThisWorkbook.Activate
    ActiveCell.Offset(0, 3).Activate
    ActiveSheet.Paste


    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    02-09-2012
    Location
    Mauritius
    MS-Off Ver
    Excel 2007
    Posts
    1,055

    Re: Find Text String via variable

    Hi.

    Try this:

    Please Login or Register  to view this content.
    Click *, if my suggestion helps you. Have a good day!!

  3. #3
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Find Text String via variable

    Any .Find statement can fail to find a match for the value it's search for, in which case it returns an empty range.

    If you try to .Select an empty range then you'll get an error (actually, if you try to do pretty much anything with an empty range you'll get an error), so you really want some checking in there to make sure you've found the value you're looking for:

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    12-14-2009
    Location
    home
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Find Text String via variable

    Hi Guys, thanks for the assistance. I got it working:

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1