Need help getting application.inpiutbox to run correctly

BrianExcel

Well-known Member
Joined
Apr 21, 2010
Messages
975
Hello, I am running the following macro, which is almost running correctly now...

  1. User enters a string in the first application.inputbox (working correctly)
    1. OR the user can click cancel and the sub will exit (working correctly)
  2. User selects range via second application.inputbox (NOT workinng)

In an ideal world, the remaining code would then run (which would search the range for that string entered in step 1, and do the remaining steps.

I had to add the capability for the user to cancel the sub at any time, thus the "If application.inputbox = "" then" lines...

But I keep getting a "Run-time error 13: Type Mismatch" when I try and run the code. Here is the code itself...

Code:
Sub SISearch(control As IRibbonControl)Dim r As Range


sTerm = Application.InputBox(prompt:="Enter text to search for", Title:="SEARCH TEXT", Type:=2)
    If sTerm = "" Then
        Exit Sub
    Else
Set rAll = Application.InputBox(prompt:="Select range to search", Title:="RANGE SELECTION", Type:=8)
    If rAll = "" Then [COLOR=#ff0000]<---[/COLOR] [COLOR=#ff0000]THIS IS WHERE MY CODE KEEPS ERRORING OUT[/COLOR]
        Exit Sub
    Else
        For Each r In rAll
            If InStr(UCase(r), UCase(sTerm)) Then
                r.Offset(0, 1) = "Text located"
            End If
        Next r
    End If
    End If
End Sub

Before I added the cancel options, the code was working fine, UNLESS the user selected cancel, in which case it was going to debug mode, which is why I needed to add the ability for my users to cancel.

Can anyone point me in the right direction on why this isn't working? If I can just get this last step done, I should be good to go...
 
Last edited:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Also FYI, if I modify the code to the below (which is the exact same only removing the "Cancel" button option, it runs fine...

Code:
Sub SISearch(control As IRibbonControl)Dim r As Range


sTerm = Application.InputBox(prompt:="Enter text to search for", Title:="SEARCH TEXT", Type:=2)
    If sTerm = "" Then
        Exit Sub
    Else
Set rAll = Application.InputBox(prompt:="Select range to search", Title:="RANGE SELECTION", Type:=8)
        
        For Each r In rAll
            If InStr(UCase(r), UCase(sTerm)) Then
                r.Offset(0, 1) = "Text located"
            End If
        Next r
    End If


End Sub
 
Upvote 0
Code:
Sub SISearch()
    Dim s           As String
    Dim r           As Range
    Dim cell        As Range

    s = Application.InputBox(Prompt:="Enter text to search for", Title:="SEARCH TEXT", Type:=2)
    If Len(s) Then
        On Error GoTo NeverMind
        Set r = Application.InputBox(Prompt:="Select range to search", Title:="RANGE SELECTION", Type:=8)
        For Each cell In Intersect(r, r.Worksheet.UsedRange)
            If InStr(1, cell.Value2, s, vbTextCompare) Then
                cell.Offset(0, 1) = "Text located"
            End If
        Next cell
    End If
NeverMind:
End Sub
 
Upvote 0
Thanks shg -

I am running Excel 2010. When I run your code, I get a "Wrong Number of Arguments or invalid property assignment" error.

Any thoughts on modification?
 
Upvote 0
That works for me in Excel 2010. Which line of code causes the error?
 
Upvote 0
It actually doesn't give me a line of code. I have the VBA editing window open, and when I run the code, it throws up that msgbox indicating the error and then automatically returns me to Excel - not the VBA window but the actual application.

Weird.
 
Upvote 0
GOT IT!!!

It was the fact that I was running this from the Ribbon, and the lack of (control as iRibbonControl) in the Sub name was throwing the error. Adding that made it work perfectly. THANKS!!!
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,572
Members
448,972
Latest member
Shantanu2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top