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.
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

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,047
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?
 

Watch MrExcel Video

Forum statistics

Threads
1,118,016
Messages
5,569,628
Members
412,284
Latest member
Daibear
Top