Share via

VBA: With "Errorcatch" command, error window pops up, but without the command, macro runs without error

Anonymous
2014-07-03T19:57:47+00:00

Hello!

This may be kinda picky, but I noticed that in my macro code, if I include the following code (the dots representing the actual script):

On Error GoTo Errorcatch

.

.

.

.

.

Errorcatch:

MsgBox Err.Description

An blank error window pops up. If I remove the above code, the program seems to run well (although it is slow, which may or may not have anything to do with some kind of "hidden error").

I am wanting to confirm that my code has no errors in it, even an error that simply slows down the calculations.

The purpose of the code is to take a big table with lots of gaps...

Col1 Col2 Col3 Col4
Text0 3 64 5 Text3
Text1 21 5 77 Text4
Text5 15 2 33 Text6

And compress it into a smaller table with no gaps in another worksheet...

Col1 Col2 Col3 Col4
Text0 3 64 5 Text3
Text1 21 5 77 Text4
Text5 15 2 33 Text6

Here is my code:

Sub Transfer()

'This macro copy-pastes rows from the Sheet2 into Sheet1

'Find rows that contain any value in column A and copy them...

Dim cell As Range

Dim selectRange As Range

On Error GoTo Errorcatch

Sheets("Sheet2").Select

For Each cell In ActiveSheet.Range("A:A")

    If (cell.Value <> "") Then

        If selectRange Is Nothing Then

            Set selectRange = cell

        Else

            Set selectRange = Union(cell, selectRange)

        End If

    End If

Next cell

selectRange.EntireRow.Select

selectRange.EntireRow.Copy

'Paste copied selection to the worksheet 'Sheet1' on the next blank row...

Sheets("Sheet2").Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial _

     Paste:=xlPasteValues

Errorcatch:

MsgBox Err.Description

End Sub

Any ideas?

Thanks!

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2014-07-03T20:17:28+00:00

When you use a GoTo like this, it skips everything inbetween and drops to your error handler. In normal practice, you would have an Exit Sub just before your error handler, otherwise it will also always run after the rest of your code. Your error handler throws a message box, so the message box will appear even if there is no error description (e.g. no error) to show.

Without going into depth on your code or what you are trying to accomplish, I think what you want is:

On Error GoTo Errorcatch

.

.

.

.

.

Exit Sub

Errorcatch:

MsgBox Err.Description

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2014-07-03T20:41:55+00:00

    Ahhh. So basically that was a VBA Fail on my part.

    Thank you!

    #VBAishard

    Was this answer helpful?

    0 comments No comments