on error explination

chris2727272727

Board Regular
Joined
Jul 10, 2005
Messages
152
range ("a2").formula = (vlookup(WHAT TO LOOK UP)

Can anybody explain to me the way the on error statement works ie if i have the above text and want it to break the sub do i put an on error end sub before or after the above vlookup statment and do i need a on error statement for every line of code or will one on error end sub at the beggining of the sub routine suffice for all the text.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

andy3816

Board Regular
Joined
Mar 22, 2005
Messages
135
on error in itself will just cause the code to break and do what ever you tell it to, one on eror statemnet will work at the beggining of your code if you alway swant it to do the same thing when it errors, if you want the code to just continue on the next instruction after an error then
on error resume next

is what you need. If you want the code to exit the routine altogether then try something like this

Sub yourcode ()

On error gotto errortrap:

your code goes here

exit sub
errortrap:
exit sub

end sub
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,344
Office Version
  1. 365
Platform
  1. Windows
Chris

On Error works with errors in VBA code.

Are you trying to trap an error in the Vlookup?
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,137
Messages
5,835,611
Members
430,370
Latest member
contentment

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
Top