Error Handling Question

Craw

New Member
Joined
Jul 27, 2011
Messages
32
Greetings,
I have a code linked to a command button and I get get the Error Handling to work properly... Here is the code I have so far;

Private Sub CommandButton3_Click()
Dim rng As Range
Set rng = ActiveSheet.AutoFilter.Range
On Error GoTo ErrMsg


MsgBox rng.Columns(1). _
SpecialCells(xlCellTypeVisible).Count - 1 _
& " of " & rng _
.Rows.Count - 1 & " Records"


ErrMsg:
MsgBox ("Type in your message here."), , "MESSAGE TITLE"
End Sub



The error usually occurs at "Set rng = ActiveSheet.AutoFilter.Range" and only occurs when the active cell is not in the active table.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
If the error occurs on setting the range object variable, and you wish to surpress this error, then you need to call your 'On Error' statement before you set the range object variable.

Code:
On Error GoTo ErrMsg
Set rng = ActiveSheet.AutoFilter.Range
 
Upvote 0
Excellent thank you, however now my error msg box pops up even if there is no error...
 
Upvote 0
Add an Exit Sub line BEFORE the ErrMsg: line

Code:
Exit Sub
ErrMsg:
MsgBox ("Type in your message here."), , "MESSAGE TITLE"
End Sub
 
Upvote 0
Although, I prefer to avoid using GoTo Statements.
They tend to create what is referred to as "spaghetti code"
It becomes difficult to read and follow the code when it's used alot.

Here is another approach.
Ignore the error with On Error Resume Next
Then test if rng has been assigned a range..

Try
Code:
Private Sub CommandButton3_Click()
Dim rng As Range
 
On Error Resume Next
Set rng = ActiveSheet.AutoFilter.Range
On Error GoTo 0
 
If rng Is Nothing Then
    MsgBox ("Type in your message here."), , "MESSAGE TITLE"
Else
    MsgBox rng.Columns(1).SpecialCells(xlCellTypeVisible).Count _
    - 1 & " of " & rng.Rows.Count - 1 & " Records"
End If
End Sub


Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

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