VBA error handling

450nick

Well-known Member
Joined
May 11, 2009
Messages
507
I've got a sub which does some things with a dynamic range. If there is nothing in the dynamic range though, I want it to ignore those steps. Is there a vba equivalent of "iserror" to use with an if command?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
As in, if the dynamic range finds no data and therefore returns an error in the script.

Its basically to select the contents of a table. But in some cases, the table can contain nothing but empty space, which will trip up the script. I just need it to realise that nothing is there, and using an if statement, go to the next step in the routine.
 
Upvote 0
You can also use the CountBlank WorksheetFunction to count the number of blank cells in a range, eg:

MsgBox WorksheetFunction.CountBlank(Range("A1:A20"))
 
Upvote 0
I'm not quite following, could I use that to make that part of the subroutine be bypassed if the table is empty? Or would you use that to carry on with the subroutine but make the range work with blanks?
 
Upvote 0
Do you mean that you have a lookup value and it throws an error when it is not found in the range? You can also use IsError in VBA, e.g:

Code:
If IsError(Application.Match("Something",Range("MyDyRange"),0)) Then
    goto Skip
Else
    'do something if there is a match
End If
 
Skip:
    'Rest of code here
 
Upvote 0
Not quite, its this specific bit of code i'm refering to:

Code:
Sheets("Data_Entry").Range("_CopyTable").Select
Selection.Copy

The dynamic range is formed of:

_CopyTable1:
=MAX(MATCH(REPT("Z",255),Data_Entry!$B$9:$B$65536),MATCH(REPT("Z",255),Data_Entry!$C$9:$C$65536), MATCH(REPT("Z",255),Data_Entry!$D$9:$D$65536), MATCH(REPT("Z",255),Data_Entry!$E$9:$E$65536))

_CopyTable2:
=MAX(MATCH(REPT("Z",255),Data_Entry!$F$9:$F$65536),MATCH(REPT("Z",255),Data_Entry!$G$9:$G$65536),MATCH(REPT("Z",255),Data_Entry!$H$9:$H$65536))

_CopyTable:
=Data_Entry!#REF!:INDEX(Data_Entry!$H$9:$H$65536,MAX(_Copytable1,_Copytable2))



If "_CopyTable" contains no data, then the script fails with an "out of range" error...

Pehaps the dynamic range data can be modified?
 
Upvote 0
Why is the starting reference of the range '#REF!'? Did you delete the row/column? Perhaps you should fix the starting range reference using INDIRECT worksheet function.

E.g:
=INDIRECT("Data_Entry!A1"):INDEX(Data_Entry!$H$9:$H$65536,MAX(_Copytable1,_Copytable2))
 
Upvote 0
I can't replicate your error. There shouldn't be any reason for an error to occur if the range is empty. If it is a valid range then it should still copy. Andrew has already suggested how to avoid copying the range if it is empty.

Maybe as a more generic way of handling the error you could try the code below but personally I would still rather know why the error occurts and handle it directly.

Code:
On Error GoTo Skip
    Sheets("Data_Entry").Range("_CopyTable").Copy
        'paste the range somewhere
On Error GoTo 0
Skip:
    'error trying to copy range, do something else
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,370
Members
449,080
Latest member
Armadillos

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