closing inputbox causing run time error 13

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi, if I close the Inputbox without entering anything, excel will give me this error message:
"Run-Time error '13" - Type mismatch". How can I improve the code so I wont get that error message when I close inputbox suddenly. Thank you so much.
+++++

Sub colorall()
Dim x As Integer
x = InputBox("enter sheet")
Workbooks(1).Worksheets(x).Range("D1:f10").Interior.ColorIndex = 11
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi

As you Dim x as Integer I assume that you are entering only Numeric Values for the Sheets Index?

If this is correct, try this:

Code:
Sub colorall()
    Dim x As Variant
    Do
        x = InputBox("enter sheet index")
'cancel pressed
        If StrPtr(x) = 0 Then Exit Sub
    Loop Until IsNumeric(x)
    Workbooks(1).Worksheets(CInt(x)).Range("D1:f10").Interior.ColorIndex = 11
End Sub


Dave
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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