|
-
Aug 22nd, 2011, 08:58 AM
#1
Thread Starter
New Member
[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
-
Aug 23rd, 2011, 04:58 AM
#2
Re: Excel VBA WorksheetFunction.Find - Runtime Error 1004
are you sure that you are looking at the correct worksheet?
vb Code:
Title = Range("C9").Value msgbox title On Error GoTo ExportXML: Illegal = WorksheetFunction.Find("&", Title)
or you can handle the error like
vb Code:
Dim Title As String, NewTitle As String Title = Range("a2").Value On Error Resume Next Illegal = WorksheetFunction.Find("&", Title) If Not Err.Number = 0 Then MsgBox "& not found in " & vbNewLine & Title: Exit Sub On Error GoTo 0 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
-
Aug 24th, 2011, 05:05 AM
#3
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|