Results 1 to 3 of 3

Thread: [RESOLVED] Excel VBA WorksheetFunction.Find - Runtime Error 1004

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    12

    Resolved [RESOLVED] Excel VBA WorksheetFunction.Find - Runtime Error 1004

    Hi there,

    I am having problems with the FIND & SEARCH commands in Excel. I am trying to find "&" in the cell C9 and if found replace it with "and".

    While the character is there to be found I have no problem, but I am looping through the code, in case there are multiple instances. When the character is not there to be found I get "Runtime Error 1004".

    Code:
    TitleCharTest: 'Test Title to replace any '&' with 'and'
        Sheet7.Select
        Dim Title As String, NewTitle As String
        Title = Range("C9").Value
        On Error GoTo ExportXML:
        Illegal = WorksheetFunction.Find("&", Title)
        On Error GoTo 0
        NewTitle = Mid(Title, 1, Illegal - 1) & "and" & Mid(Title, Illegal + 1)
        Range("C9").Value = NewTitle
        GoTo TitleCharTest:
    I have the "On Error GoTo ExportXML:" line located before the search with the plan that when this error occurs the program should jump to the label ExportXML: and continue.

    Any help on why I am doing getting this error would be appreciated.

    Cheers,
    Tepe

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA WorksheetFunction.Find - Runtime Error 1004

    are you sure that you are looking at the correct worksheet?
    vb Code:
    1. Title = Range("C9").Value
    2.    msgbox title
    3.     On Error GoTo ExportXML:
    4.     Illegal = WorksheetFunction.Find("&", Title)

    or you can handle the error like
    vb Code:
    1. Dim Title As String, NewTitle As String
    2.     Title = Range("a2").Value
    3. On Error Resume Next
    4. Illegal = WorksheetFunction.Find("&", Title)
    5. If Not Err.Number = 0 Then MsgBox "& not found in " & vbNewLine & Title: Exit Sub
    6. On Error GoTo 0
    7.  NewTitle = Mid(Title, 1, Illegal - 1) & "and" & Mid(Title, Illegal + 1)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    12

    Re: Excel VBA WorksheetFunction.Find - Runtime Error 1004

    Thanks Westconn,

    I tried your suggestions, but was still getting the same error;
    Runtime Error 1004: unable to get the find property of the worksheetfunction class

    In the end I have used the InStr function and this seems to be working. A smaple of my working code is below.

    Thanks for your help.

    Code:
    TitleCharTest: 'Test Title to replace any '&' with 'and'
        Sheet7.Select
        Dim Title As String, NewTitle As String
        Title = Range("C9").Value
        Illegal = InStr(1, Title, "&")
        If Illegal = 0 Then GoTo ExportXML:
        NewTitle = Mid(Title, 1, Illegal - 1) & "and" & Mid(Title, Illegal + 1)
        Range("C9").Value = NewTitle
        GoTo TitleCharTest:

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width