Home » excel » excel – Modeless MsgBox, Error trapping, .Find

excel – Modeless MsgBox, Error trapping, .Find

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a subroutine that searches for an occurrence of a string in another workbook. I’m trying to get an error message to pop up if the string can’t be found (it’s most likely due to spelling mistakes), as vbModeless, and allows to user to click on the cell in the searched sheet with the correct value. Then I’d like to resume the search with the new value.

I’m at the moment stuck on making my simple MsgBox to be modeless.
Can anyone help? So far I have (simplified):

With ...
    On Error GoTo UserSelect
    celladdress = .Range("a1:bb100").Find("searchstring").Address

And my error label:

UserSelect:
    MsgBox("Select the cell with the correct spelling") vbModeless
    newstring = ActiveCell.Value
    searchstring = newstring
    Resume

I think it’s the Modeless MsgBox giving me grief.

How to&Answers:

I don’t believe that you can use vbModeless with msgbox. That is for use with the Show method of a user form.

What you probably need to do is create a user form that has a refedit control and a button on it. They can then pick a cell with the refedit control. When the user clicks on the button set a public variable on the form with the cell reference the selected.

Then you you need to use “.Show vbModal” on the user form and read off the cell they selected from the form public variable.

Edit:

Actually, you shouldn’t need the public variable as the refedit control should be a public property of the form anyway.

Answer:

I’m not 100% sure on the requirements here. Given a search string of dgo and a worksheet with cells containing bird, cat and dog. Do you want the user to:

(a) edit the cell containing dog and change it to say dgo instead

This would use the modal form and RefEdit control outlined by andynormancx. Like a MsgBox, the modal form pauses the macro until the form is closed

(b) allow the user to click on the cell containg dog and then re-run the search with dog as the search term

This is more complicated. I think that you would need to look at events here. This is fine if your subroutine is pretty much standalone but if it is part of a larger program then this could require substantial rewriting