How to add OnError vba on my current subroutine?

eshnider21

New Member
Joined
Jul 31, 2021
Messages
9
Office Version
  1. 2019
Platform
  1. Windows
I tried adding OnError on my current sub. I success adding it but it always shows the Error message even there is no error. How to add the OnError correctly?


Public Sub Searcher()
Dim wss As Integer
wss = Application.InputBox("Please enter Sheet Number", "Sheet Select", Type:=1)
With ThisWorkbook.Worksheets(wss)
.Select
.Activate
End With
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi welcome to forum

try

VBA Code:
Public Sub Searcher()
    Dim wss         As Variant
    
    On Error GoTo myerror
    wss = Application.InputBox("Please enter Sheet Number", "Sheet Select", Type:=1)
    'cancel pressed
    If VarType(wss) = vbBoolean Then Exit Sub
    
    'your code
    
myerror:
If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

when using an InputBox, your code should include handling user pressing Cancel button

Dave
 
Upvote 0
Solution
Welcome to the Board!

If I enter a valid worksheet number, it works fine for me, without any errors.
What exactly are you entering in?

Realize that code is going by the the worksheet index number, not the name.
 
Upvote 0
Welcome to the Board!

If I enter a valid worksheet number, it works fine for me, without any errors.
What exactly are you entering in?

Realize that code is going by the the worksheet index number, not the name.
Actually the macro is fine. I was just avoiding users to mishandle the sub so I was trying to put OnError on it.

Thanks for greetings by the way.
 
Upvote 0
OK, but I thought you said:
it but it always shows the Error message even there is no error.
but I cannot create that scenario.

Is that really an issue, or no?
 
Upvote 0
Actually the macro is fine. I was just avoiding users to mishandle the sub so I was trying to put OnError on it.

Thanks for greetings by the way.
I get
Welcome to the Board!

If I enter a valid worksheet number, it works fine for me, without any errors.
What exactly are you entering in?

Realize that code is going by the the worksheet index number, not the name.
I get it now. Now I know why the message still pops up even there is no error..I forgot to exit sub.

I am still confused using the right variables for my DIM. Originally my dim fof wss is Integer. and never used Boolean
 
Upvote 0
I get it now. Now I know why the message still pops up even there is no error..I forgot to exit sub.
There isn't any error handling code or messages in your original post, which is why I am so confused.
Maybe you had it in at one time, but didn't include it in your original post?
 
Upvote 0
There isn't any error handling code or messages in your original post, which is why I am so confused.
Maybe you had it in at one time, but didn't include it in your original post?
Yes I removed it after my trial and error. I was so frustrated I removed the OnError before I posted this on the forums. But anyways it is complete now.Thank you very much


'Public Sub Searcher()
Public Sub Searcher()
Dim wss As Variant
On Error GoTo myerror
wss = Application.InputBox("Please enter Sheet Number", "Sheet Select", Type:=1)
'cancel pressed
If VarType(wss) = vbBoolean Then Exit Sub
'your code
With ThisWorkbook.Worksheets(wss)
.Select
.Activate
End With
Call DeleteRows

myerror:
If Err <> 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

Sub DeleteRows()
Dim rng As Range
Dim InputRng As Range
Dim DeleteRng As Range
Dim DeleteStr As String
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
DeleteStr = Application.InputBox("Delete Text", xTitleId, Type:=2)
For Each rng In InputRng
If rng.Value = DeleteStr Then
If DeleteRng Is Nothing Then
Set DeleteRng = rng
Else
Set DeleteRng = Application.Union(DeleteRng, rng)
End If
End If
Next
DeleteRng.EntireRow.Delete
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
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