Consulting

Results 1 to 8 of 8

Thread: FIND function in a macro

  1. #1

    FIND function in a macro

    Hi,

    I am trying to write a macro that deletes a row if either the C or D column in that row do not have a / as part of the text.

    I tried the following but got an error that the FIND sub or function is not defined. Why? I thought FIND was a built in Function.

    Thanks in advance for your help!

    Mick


    Sub pedro_remove_ios()
    Dim LastRow As Long
        Dim Counter         As Integer
    LastRow = Cells.Find(What:="*", LookIn:=xlValues, _
        SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
       Counter = LastRow - 4
       Do While Counter > 1
         If IsError(Find("/", Cells(Counter & "C"))) _
           Or IsError(Find("/", Cells(Counter & "D"))) Then
           Cells(Counter, "A").EntireRow.Delete
         End If
         Counter = Counter - 1
       Loop
    End Sub

  2. #2
    VBAX Contributor Aaron Blood's Avatar
    Joined
    Sep 2004
    Location
    Palm Beach, Florida, USA
    Posts
    130
    Location
    I believe FIND is a method/property of a range object.


    In your code where you try to use it independently:


    If IsError(Find("/", Cells(Counter & "C")))

    It fails...

  3. #3
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    i would say you need:
    IsError(Cells.Find... etc
    on the two lines in your loop
    K :-)

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Cells.Find
    is used to search a worksheet for a specific value. If you want to use Find on a specific cell you can use the following.

    Application.Worksheetfunction.Find
    however, VBA has a function of its own that you can use the following.

    If InStr(1, Range("C" & Counter).Text, "/") > 0 Then
    InStr will be 0 if the string is not found. This means that there is no need for error handling as would be the case with Find (or Search).

    Also to use Cells to specify a range the following is the correct syntact to use:

    Cells(RowNumber, ColumnNumber)

  5. #5
    Hi Killian,

    If I try Cells.Find I get a type mismatch on that line.

    Sub pedro_remove_ios()
    Dim LastRow As Long
        Dim Counter         As Integer
    LastRow = Cells.Find(What:="*", LookIn:=xlValues, _
        SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
        Counter = LastRow - 4
        Do While Counter > 1
            If IsError(Cells.Find("/", Cells(Counter & "C"))) _
            Or IsError(Cells.Find("/", Cells(Counter & "D"))) Then
            Cells(Counter, "A").EntireRow.Delete
        End If
        Counter = Counter - 1
    Loop
    End Sub


    Hi Aaron,

    I know I can use the function
    =IsError(Find("/", A1))
    in a Cell within xl itself and that works.

    Regards,

    Mick

  6. #6
    Hi Jacob,

    Thanks once again! That worked perfectly!

    Thanks for all your help!

    Thanks Aaron and Killian,

    Mick

  7. #7
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You're Welcome

    Take Care

  8. #8
    VBAX Contributor Aaron Blood's Avatar
    Joined
    Sep 2004
    Location
    Palm Beach, Florida, USA
    Posts
    130
    Location
    Quote Originally Posted by mp_robinson_uk
    Hi Killian,

    Hi Aaron,

    I know I can use the function =IsError(Find("/", A1)) in a Cell within xl itself and that works.

    Regards,

    Mick
    Don't confuse the VBA Find method with the Find worksheet function. Just remember if you want to use a worksheet function in VBA you need to use the following syntax:

    [VBA] Set Fn = Application.WorksheetFunction
    x = Fn.Find("/", Range("A1"))
    [/VBA]

    Also, as was pointed out above, INSTR is the VBA equivalent to the FIND worksheet function.

Posting Permissions

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