Consulting

Results 1 to 5 of 5

Thread: Find a word

  1. #1
    VBAX Regular
    Joined
    Jan 2009
    Posts
    89
    Location

    Find a word

    Is there a way to replicate the Control F function within VBA?

    right now I am using this but it is not very flexible. It looks for the word "Time" in column A, is it possible for it to look in both column A and B?

    [VBA]
    FindTime = Application.WorksheetFunction.Match("time", RngA, 0)
    [/VBA]

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Record a macro while doing the search. That will give you the basic code. If you need to modify it further, let us know.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Jan 2009
    Posts
    89
    Location
    I have tried that but nothing gets recorded.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I've not known the recorder to fail with such. Where does it suggest the code is being saved?

    Anyway, here is recorded code, and how it might be tidied up

    [VBA]
    Option Explicit

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    '
    Columns("A:B").Select
    Selection.Find(What:="Time", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate
    Range("C25").Select
    End Sub


    Sub Refined()
    Dim Rng as Range
    Set Rng = Columns("A:B").Find(What:="Time", LookIn:=xlFormulas, _
    LookAt:=xlWhole)
    Rng.Interior.ColorIndex = 6
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Jan 2009
    Posts
    89
    Location
    thank you, that worked great.

    I've tried on a couple of occassions and nothing was recorded when using Control + F, I just tried it again and it did record. Maybe I goofed on the other occassions, or maybe it was some glitch.

Posting Permissions

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