{"id":2067,"date":"2024-12-02T06:56:32","date_gmt":"2024-12-02T06:56:32","guid":{"rendered":"https:\/\/blog.ezworkapp.com\/index.php\/2024\/12\/02\/huong-dan-xu-ly-loi-vba-day-du\/"},"modified":"2024-12-02T06:56:32","modified_gmt":"2024-12-02T06:56:32","slug":"huong-dan-xu-ly-loi-vba-day-du","status":"publish","type":"post","link":"https:\/\/blog.ezworkapp.com\/index.php\/2024\/12\/02\/huong-dan-xu-ly-loi-vba-day-du\/","title":{"rendered":"H\u01af\u1edaNG D\u1eaaN X\u1eec L\u00dd L\u1ed6I VBA \u0110\u1ea6Y \u0110\u1ee6"},"content":{"rendered":"<div class=\"uk-panel uk-margin\" id=\"template-dQf5b79p#1\">\n<p>\u00a0<\/p>\n<p><em><span style=\"font-weight: 400\">B\u00e0i vi\u1ebft d\u01b0\u1edbi \u0111\u00e2y h\u01b0\u1edbng d\u1eabn \u0111\u1ea7y \u0111\u1ee7 v\u1ec1 <strong>x\u1eed l\u00fd l\u1ed7i VBA<\/strong>. N\u1ebfu b\u1ea1n \u0111ang t\u00ecm ki\u1ebfm m\u1ed9t b\u1ea3ng t\u1ed5ng h\u1ee3p t\u00f3m t\u1eaft nhanh th\u00ec c\u00f3 th\u1ec3 xem m\u1ee5c \u0111\u1ea7u ti\u00ean trong b\u00e0i vi\u1ebft n\u00e0y c\u1ee7a <a href=\"http:\/\/blog.ezworkapp.com\/\"><strong>H\u1ecdc Excel Online<\/strong> <\/a>h\u01b0\u1edbng d\u1eabn.<\/span><\/em><\/p>\n<p><span style=\"font-weight: 400\">Trong tr\u01b0\u1eddng h\u1ee3p b\u1ea1n mu\u1ed1n t\u00ecm gi\u1ea3i ph\u00e1p c\u1ee5 th\u1ec3 cho t\u1eebng l\u1ed7i th\u00ec c\u00f3 th\u1ec3 xem ti\u1ebfp c\u00e1c m\u1ee5c ph\u00eda d\u01b0\u1edbi. B\u1ea1n c\u0169ng c\u00f3 th\u1ec3 t\u00ecm hi\u1ec3u hi\u1ec3u to\u00e0n b\u1ed9 c\u00e1ch x\u1eed l\u00fd l\u1ed7i VNA qua to\u00e0n b\u1ed9 b\u00e0i vi\u1ebft .\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">H\u01b0\u1edbng d\u1eabn x\u1eed l\u00fd nhanh v\u1ec1 l\u1ed7i<\/span><\/p>\n<ul>\n<li>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_61 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<p class=\"ez-toc-title\">Xem nhanh<\/p>\n<p><label for=\"ez-toc-cssicon-toggle-item-674d5a1e874c7\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><\/span><\/span><\/label><\/p>\n<nav>\n<ul class=\"ez-toc-list ez-toc-list-level-1 \">\n<li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#Huong_dan_xu_ly_nhanh_ve_loi\" title=\"H\u01b0\u1edbng d\u1eabn x\u1eed l\u00fd nhanh v\u1ec1 l\u1ed7i\">H\u01b0\u1edbng d\u1eabn x\u1eed l\u00fd nhanh v\u1ec1 l\u1ed7i<\/a>\n<ul class=\"ez-toc-list-level-2\">\n<li class=\"ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#Gioi_thieu\" title=\"Gi\u1edbi thi\u1ec7u\">Gi\u1edbi thi\u1ec7u<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#Loi_VBA\" title=\"L\u1ed7i VBA\">L\u1ed7i VBA<\/a>\n<ul class=\"ez-toc-list-level-3\">\n<li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#31_Loi_cu_phap\" title=\"3.1 L\u1ed7i c\u00fa ph\u00e1p\">3.1 L\u1ed7i c\u00fa ph\u00e1p<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#32_Loi_bien_dich\" title=\"3.2 L\u1ed7i bi\u00ean d\u1ecbch\">3.2 L\u1ed7i bi\u00ean d\u1ecbch<\/a>\n<ul class=\"ez-toc-list-level-4\">\n<li class=\"ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#321_Su_dung_Debug_de_go_loi_bien_dich\" title=\"3.2.1 S\u1eed d\u1ee5ng Debug \u0111\u1ec3 g\u1ee1 l\u1ed7i bi\u00ean d\u1ecbch\">3.2.1 S\u1eed d\u1ee5ng Debug \u0111\u1ec3 g\u1ee1 l\u1ed7i bi\u00ean d\u1ecbch<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#322_Tom_tat_su_dung_tuy_chon_debug_xu_ly_loi_bien_dich\" title=\"3.2.2 T\u00f3m t\u1eaft s\u1eed d\u1ee5ng t\u00f9y ch\u1ecdn debug x\u1eed l\u00fd l\u1ed7i bi\u00ean d\u1ecbch\">3.2.2 T\u00f3m t\u1eaft s\u1eed d\u1ee5ng t\u00f9y ch\u1ecdn debug x\u1eed l\u00fd l\u1ed7i bi\u00ean d\u1ecbch<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-4\"><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#323_Cach_su_dung_Debug_go_loi_bien_dich\" title=\"3.2.3 C\u00e1ch s\u1eed d\u1ee5ng Debug g\u1ee1 l\u1ed7i bi\u00ean d\u1ecbch\">3.2.3 C\u00e1ch s\u1eed d\u1ee5ng Debug g\u1ee1 l\u1ed7i bi\u00ean d\u1ecbch<\/a><\/li>\n<\/ul>\n<\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#33_Loi_Runtime\" title=\"3.3 L\u1ed7i Runtime\">3.3 L\u1ed7i Runtime<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#34_Loi_Runtime_khong_phai_la_loi_VBA\" title=\"3.4 L\u1ed7i Runtime kh\u00f4ng ph\u1ea3i l\u00e0 l\u1ed7i VBA\">3.4 L\u1ed7i Runtime kh\u00f4ng ph\u1ea3i l\u00e0 l\u1ed7i VBA<\/a><\/li>\n<\/ul>\n<\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#4_Su_dung_cau_lenh_On_error\" title=\"4. S\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh On error\">4. S\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh On error<\/a>\n<ul class=\"ez-toc-list-level-3\">\n<li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#41_On_error_Goto_0\" title=\"4.1 On error Goto 0\">4.1 On error Goto 0<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#42_Cau_lenh_On_Error_Resume_Next\" title=\"4.2 C\u00e2u l\u1ec7nh On Error Resume Next\">4.2 C\u00e2u l\u1ec7nh On Error Resume Next<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#43_Lenh_On_Error_GoTo_label\" title=\"4.3 L\u1ec7nh On Error GoTo [label]\">4.3 L\u1ec7nh On Error GoTo [label]<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#44_On_Error_GoTo_-1\" title=\"4.4 On Error GoTo -1\">4.4 On Error GoTo -1<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#45_Su_dung_lenh_On_Error\" title=\"4.5 S\u1eed d\u1ee5ng l\u1ec7nh On Error\">4.5 S\u1eed d\u1ee5ng l\u1ec7nh On Error<\/a><\/li>\n<\/ul>\n<\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#5_Resume_Next\" title=\"5. Resume Next\">5. Resume Next<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#6_Err_Objective\" title=\"6. Err Objective\">6. Err Objective<\/a>\n<ul class=\"ez-toc-list-level-3\">\n<li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#61_Lay_so_dong\" title=\"6.1 L\u1ea5y s\u1ed1 d\u00f2ng\">6.1 L\u1ea5y s\u1ed1 d\u00f2ng<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#62_Su_dung_ErrRaise\" title=\"6.2 S\u1eed d\u1ee5ng Err.Raise\">6.2 S\u1eed d\u1ee5ng Err.Raise<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#63_Su_dung_Err_Clear\" title=\"6.3 S\u1eed d\u1ee5ng Err Clear\">6.3 S\u1eed d\u1ee5ng Err Clear<\/a><\/li>\n<\/ul>\n<\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#7_Logging\" title=\"7. Logging\">7. Logging<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#8_Cac_muc_lien_quan_den_loi_khac\" title=\"8. C\u00e1c m\u1ee5c li\u00ean quan \u0111\u1ebfn l\u1ed7i kh\u00e1c\">8. C\u00e1c m\u1ee5c li\u00ean quan \u0111\u1ebfn l\u1ed7i kh\u00e1c<\/a>\n<ul class=\"ez-toc-list-level-3\">\n<li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#81_Ham_Error\" title=\"8.1 H\u00e0m Error\">8.1 H\u00e0m Error<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#82_Lenh_Error\" title=\"8.2 L\u1ec7nh Error\">8.2 L\u1ec7nh Error<\/a><\/li>\n<\/ul>\n<\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#9_Mot_so_chien_luoc_xu_ly_loi_don_gian\" title=\"9. M\u1ed9t s\u1ed1 chi\u1ebfn l\u01b0\u1ee3c x\u1eed l\u00fd l\u1ed7i \u0111\u01a1n gi\u1ea3n\">9. M\u1ed9t s\u1ed1 chi\u1ebfn l\u01b0\u1ee3c x\u1eed l\u00fd l\u1ed7i \u0111\u01a1n gi\u1ea3n<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-27\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#10_Mot_chien_luoc_xu_ly_loi_hoan_chinh\" title=\"10. M\u1ed9t chi\u1ebfn l\u01b0\u1ee3c x\u1eed l\u00fd l\u1ed7i ho\u00e0n ch\u1ec9nh\">10. M\u1ed9t chi\u1ebfn l\u01b0\u1ee3c x\u1eed l\u00fd l\u1ed7i ho\u00e0n ch\u1ec9nh<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-28\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-xu-ly-loi-vba-day-du.html#11_Xu_ly_loi_trong_Nutshell\" title=\"11. X\u1eed l\u00fd l\u1ed7i trong Nutshell\">11. X\u1eed l\u00fd l\u1ed7i trong Nutshell<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/nav>\n<\/div>\n<h1><span class=\"ez-toc-section\" id=\"Huong_dan_xu_ly_nhanh_ve_loi\"><\/span><b>H\u01b0\u1edbng d\u1eabn x\u1eed l\u00fd nhanh v\u1ec1 l\u1ed7i<\/b><span class=\"ez-toc-section-end\"><\/span><\/h1>\n<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><b>M\u1ee5c<\/b><\/td>\n<td><b>M\u00f4 t\u1ea3<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">On Error Goto 0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Khi x\u1ea3y ra l\u1ed7i, m\u00e3 s\u1ebd d\u1eebng v\u00e0 hi\u1ec3n th\u1ecb l\u1ed7i.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\"> On Error Goto -1<\/span><\/td>\n<td><span style=\"font-weight: 400\"> X\u00f3a c\u00e0i \u0111\u1eb7t l\u1ed7i hi\u1ec7n t\u1ea1i v\u00e0 tr\u1ea3 v\u1ec1 v\u1ec1 m\u1eb7c \u0111\u1ecbnh.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\"> On Error Resume Next<\/span><\/td>\n<td><span style=\"font-weight: 400\"> B\u1ecf qua l\u1ed7i v\u00e0 ti\u1ebfp t\u1ee5c<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\"> On Error Goto [Label]<\/span><\/td>\n<td><span style=\"font-weight: 400\"> Chuy\u1ec3n \u0111\u1ebfn m\u1ed9t label c\u1ee5 th\u1ec3 \u0111\u1ec3 x\u1eed l\u00fd l\u1ed7i<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\"> Err Object<\/span><\/td>\n<td><span style=\"font-weight: 400\">Khi x\u1ea3y ra l\u1ed7i, th\u00f4ng tin l\u1ed7i \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef \u1edf \u0111\u00e2y<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\"> Err.Number<\/span><\/td>\n<td><span style=\"font-weight: 400\"> S\u1ed1 l\u1ed7i.<\/span><\/p>\n<p><span style=\"font-weight: 400\">(Ch\u1ec9 h\u1eefu \u00edch n\u1ebfu b\u1ea1n c\u1ea7n ki\u1ec3m tra m\u1ed9t l\u1ed7i c\u1ee5 th\u1ec3 \u0111\u00e3 x\u1ea3y ra.)<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\"> Err.Description<\/span><\/td>\n<td><span style=\"font-weight: 400\"> Ch\u1ee9a v\u0103n b\u1ea3n l\u1ed7i<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\"> Err.Source<\/span><\/td>\n<td><span style=\"font-weight: 400\"> B\u1ea1n c\u00f3 th\u1ec3 \u0111i\u1ec1n \u0111i\u1ec1u n\u00e0y khi b\u1ea1n s\u1eed d\u1ee5ng Err.Raise.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\"> Err.Raise<\/span><\/td>\n<td><span style=\"font-weight: 400\"> M\u1ed9t ch\u1ee9c n\u0103ng cho ph\u00e9p b\u1ea1n t\u1ea1o ra l\u1ed7i c\u1ee7a ri\u00eang m\u00ecnh.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\"> Error Function<\/span><\/td>\n<td><span style=\"font-weight: 400\"> Tr\u1ea3 v\u1ec1 v\u0103n b\u1ea3n l\u1ed7i t\u1eeb m\u1ed9t s\u1ed1 l\u1ed7i.<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u0110\u00e3 l\u1ed7i th\u1eddi.<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\"> Error Statement<\/span><\/td>\n<td><span style=\"font-weight: 400\"> M\u00f4 ph\u1ecfng m\u1ed9t l\u1ed7i. S\u1eed d\u1ee5ng Err.Raise \u0111\u1ec3 thay th\u1ebf.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong><br \/>\n<b><\/b><\/strong><\/p>\n<ul>\n<li>\n<h2><span class=\"ez-toc-section\" id=\"Gioi_thieu\"><\/span><b>Gi\u1edbi thi\u1ec7u<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">X\u1eed l\u00fd l\u1ed7i th\u00f4ng th\u01b0\u1eddng li\u00ean quan \u0111\u1ebfn nh\u1eefng m\u00e3 \u0111\u01b0\u1ee3c vi\u1ebft \u0111\u1ec3 x\u1eed l\u00fd l\u1ed7i x\u1ea3y ra khi \u1ee9ng d\u1ee5ng \u0111ang ch\u1ea1y. Nh\u1eefng l\u1ed7i n\u00e0y th\u01b0\u1eddng n\u1eb1m ngo\u00e0i s\u1ef1 ki\u1ec3m so\u00e1t nh\u01b0 thi\u1ebfu t\u1ec7p, c\u01a1 s\u1edf d\u1eef li\u1ec7u kh\u00f4ng kh\u1ea3 d\u1ee5ng, d\u1eef li\u1ec7u kh\u00f4ng h\u1ee3p l\u1ec7, v.v. \u0110\u00f4i khi nh\u1eefng l\u1ed7i n\u00e0y s\u1ebd g\u00e2y kh\u00f3 kh\u0103n cho ng\u01b0\u1eddi s\u1eed d\u1ee5ng, \u0111\u1eb7c bi\u1ec7t l\u00e0 ng\u01b0\u1eddi m\u1edbi s\u1eed d\u1ee5ng l\u1eadp tr\u00ecnh VBA. Cho n\u00ean h\u00e3y xem k\u1ef9 <a href=\"http:\/\/blog.ezworkapp.com\/bat-dau-voi-excel-macros-va-lap-trinh-vba.html\"><strong>h\u01b0\u1edbng d\u1eabn l\u1eadp tr\u00ecnh VBA cho ng\u01b0\u1eddi m\u1edbi<\/strong> <\/a>b\u1eaft \u0111\u1ea7u \u0111\u1ec3 hi\u1ec3u m\u1ee9c \u0111\u1ed9 nghi\u00eam tr\u1ecdng c\u1ee7a l\u1ed7i<\/span><\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px 0;clear: both\">\n<div class=\"ad-inserterpro\">\n<a href=\"https:\/\/hocexcel.online\/course\/preview\/vba101-tu-dong-hoa-excel-voi-lap-trinh-vba-cho-nguoi-moi-bat-dau\"><br \/>\n<img decoding=\"async\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/vba101_92019-nho-65.gif\" title=\"vba101_92019-nho-65\"><br \/>\n<\/a>\n<\/div>\n<\/div>\n<div class=\"code-block code-block-1\" style=\"margin: 8px 0;clear: both\">\n<div class=\"ad-inserterpro\">\n<a href=\"https:\/\/hocexcel.online\/course\/preview\/ex101-excel-tu-co-ban-den-chuyen-gia-danh-cho-nguoi-di-lam\"><br \/>\n<img decoding=\"async\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/ex101_92019-nho-158.gif\" title=\"ex101_92019-nho-158\"><br \/>\n<\/a>\n<\/div>\n<\/div>\n<p><span style=\"font-weight: 400\">N\u1ebfu b\u1ea1n c\u00f3 th\u1ec3 d\u1ef1 \u0111o\u00e1n \u0111\u01b0\u1ee3c c\u00e1c l\u1ed7i c\u00f3 th\u1ec3 x\u1ea3y ra th\u00ec c\u00e1ch t\u1ed1t nh\u1ea5t l\u00e0 b\u1ea1n vi\u1ebft m\u00e3 c\u1ee5 th\u1ec3 \u0111\u1ec3 x\u1eed l\u00fd l\u1ed7i.<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u0110\u1ed1i v\u1edbi t\u1ea5t c\u1ea3 c\u00e1c l\u1ed7i kh\u00e1c m\u00e0 b\u1ea1n kh\u00f4ng d\u1ef1 \u0111o\u00e1n \u0111\u01b0\u1ee3c th\u00ec c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00e1c l\u1ec7nh \u0111\u1ec3 x\u1eed l\u00fd. \u0110\u00e2y l\u00e0 l\u00fac c\u00e2u l\u1ec7nh x\u1eed l\u00fd l\u1ed7i VBA ph\u00e1t huy t\u00e1c d\u1ee5ng.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u0110\u1ec3 hi\u1ec3u c\u00e1ch x\u1eed l\u00fd l\u1ed7i, tr\u01b0\u1edbc ti\u00ean ch\u00fang ta ph\u1ea3i hi\u1ec3u c\u00e1c lo\u1ea1i l\u1ed7i kh\u00e1c nhau trong VBA.<\/span><\/p>\n<ul>\n<li>\n<h2><span class=\"ez-toc-section\" id=\"Loi_VBA\"><\/span><b>L\u1ed7i VBA<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">C\u00f3 3 lo\u1ea1i l\u1ed7i trong VBA:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">C\u00fa ph\u00e1p<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">L\u1ed7i bi\u00ean d\u1ecbch<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">L\u1ed7i Runtime<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400\">Ch\u00fang ta s\u1eed d\u1ee5ng Error Handling \u0111\u1ec3 gi\u1ea3i quy\u1ebft l\u1ed7i runtime error. Trong b\u00e0i vi\u1ebft n\u00e0y s\u1ebd xem x\u00e9t t\u1eebng lo\u1ea1i l\u1ed7i \u0111\u1ec3 t\u1eeb \u0111\u00f3 s\u1ebd c\u00f3 c\u00e1ch x\u1eed l\u00fd hi\u1ec7u qu\u1ea3.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"31_Loi_cu_phap\"><\/span><b>3.1 L\u1ed7i c\u00fa ph\u00e1p<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400\">N\u1ebfu b\u1ea1n th\u01b0\u1eddng xuy\u00ean s\u1eed d\u1ee5ng VBA th\u00ec s\u1ebd th\u1ea5y l\u1ed7i c\u00fa ph\u00e1p. Khi b\u1ea1n nh\u1eadp m\u1ed9t d\u00f2ng k\u00fd t\u1ef1\u00a0 v\u00e0 nh\u1ea5n return th\u00ec VBA s\u1ebd \u0111\u00e1nh gi\u00e1 c\u00fa ph\u00e1p n\u1ebfu sai s\u1ebd hi\u1ec3n th\u1ecb th\u00f4ng b\u00e1o l\u1ed7i.<\/span><\/p>\n<p><span style=\"font-weight: 400\">V\u00ed d\u1ee5: n\u1ebfu b\u1ea1n g\u00f5 IF v\u00e0 qu\u00ean t\u1eeb kh\u00f3a Then, VBA s\u1ebd hi\u1ec3n th\u1ecb th\u00f4ng b\u00e1o l\u1ed7i sau:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33644\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h1.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-1\" width=\"249\" height=\"176\" title=\"h1\"><\/p>\n<p><span style=\"font-weight: 400\">M\u1ed9t s\u1ed1 v\u00ed d\u1ee5 v\u1ec1 l\u1ed7i c\u00fa ph\u00e1p:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33645\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h2.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-2\" width=\"727\" height=\"221\" title=\"h2\"><\/p>\n<p><span style=\"font-weight: 400\">L\u1ed7i c\u00fa ph\u00e1p ch\u1ec9 li\u00ean quan \u0111\u1ebfn m\u1ed9t d\u00f2ng v\u00e0 ch\u00fang x\u1ea3y ra khi c\u00fa ph\u00e1p m\u1ed9t d\u00f2ng kh\u00f4ng ch\u00ednh x\u00e1c.<\/span><\/p>\n<p><b>L\u01b0u \u00fd<\/b><span style=\"font-weight: 400\">: B\u1ea1n c\u00f3 th\u1ec3 t\u1eaft h\u1ed9p tho\u1ea1i l\u1ed7i c\u00fa ph\u00e1p b\u1eb1ng c\u00e1ch nh\u1eadp v\u00e0o <\/span><b>Tools\u2013&gt; Options <\/b><span style=\"font-weight: 400\">v\u00e0 ch\u1ecdn <\/span><b><i>\u201cAuto Syntax Check\u201d<\/i><\/b><span style=\"font-weight: 400\">. C\u00e1c d\u00f2ng b\u1ecb l\u1ed7i v\u1eabn hi\u1ec3n th\u1ecb m\u00e0u \u0111\u1ecf nh\u01b0ng h\u1ed9p tho\u1ea1i s\u1ebd kh\u00f4ng xu\u1ea5t hi\u1ec7n.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"32_Loi_bien_dich\"><\/span><b>3.2 L\u1ed7i bi\u00ean d\u1ecbch<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400\">L\u1ed7i bi\u00ean d\u1ecbch x\u1ea3y ra tr\u00ean m\u1ed9t d\u00f2ng. C\u00fa ph\u00e1p \u0111\u00fang tr\u00ean m\u1ed9t d\u00f2ng nh\u01b0ng kh\u00f4ng ch\u00ednh x\u00e1c khi t\u00ednh \u0111\u1ebfn to\u00e0n b\u1ed9 m\u00e3 m\u00e3.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">V\u00ed d\u1ee5 v\u1ec1 l\u1ed7i bi\u00ean d\u1ecbch:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">S\u1eed d\u1ee5ng l\u1ec7nh <\/span><b>IF<\/b><span style=\"font-weight: 400\"> m\u00e0 kh\u00f4ng c\u00f3 l\u1ec7nh <\/span><b>End If.<\/b><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">S\u1eed d\u1ee5ng l\u1ec7nh <\/span><b>For<\/b><span style=\"font-weight: 400\"> m\u00e0 kh\u00f4ng c\u00f3 l\u1ec7nh <\/span><b>Next<\/b><span style=\"font-weight: 400\">.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">S\u1eed d\u1ee5ng l\u1ec7nh <\/span><b>Select<\/b><span style=\"font-weight: 400\"> m\u00e0 kh\u00f4ng c\u00f3 l\u1ec7nh <\/span><b>End Select.<\/b><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">G\u1ecdi m\u1ed9t l\u1ec7nh ho\u1eb7c Sub kh\u00f4ng t\u1ed3n t\u1ea1i.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Kh\u00f4ng khai b\u00e1o bi\u1ebfn (ch\u1ec9 ho\u1ea1t \u0111\u1ed9ng khi t\u00f9y ch\u1ecdn n\u00e0y \u0111\u01b0\u1ee3c k\u00edch ho\u1ea1t (<\/span><b><i>Option Explicit ).<\/i><\/b><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">\u1ea2nh ch\u1ee5p m\u00e0n h\u00ecnh sau \u0111\u00e2y cho th\u1ea5y l\u1ed7i bi\u00ean d\u1ecbch x\u1ea3y ra khi v\u00f2ng l\u1eb7p c\u00e2u l\u1ec7nh For m\u00e0 kh\u00f4ng c\u00f3 c\u00e2u l\u1ec7nh Next.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33646\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h3.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-3\" width=\"503\" height=\"458\" title=\"h3\"><\/p>\n<h4><span class=\"ez-toc-section\" id=\"321_Su_dung_Debug_de_go_loi_bien_dich\"><\/span><b>3.2.1 S\u1eed d\u1ee5ng Debug \u0111\u1ec3 g\u1ee1 l\u1ed7i bi\u00ean d\u1ecbch<\/b><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><span style=\"font-weight: 400\">\u0110\u1ec3 t\u00ecm l\u1ed7i bi\u00ean d\u1ecbch ch\u00fang ta s\u1eed d\u1ee5ng Debug tr\u00ean thanh c\u00f4ng c\u1ee5, sau \u0111\u00f3 ch\u1ecdn compile VBAproject.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Khi b\u1ea1n ch\u1ecdn <\/span><b><i>Debug \u2013&gt; Compile<\/i><\/b><span style=\"font-weight: 400\">, VBA s\u1ebd hi\u1ec3n th\u1ecb l\u1ed7i \u0111\u1ea7u ti\u00ean m\u00e0 n\u00f3 g\u1eb7p ph\u1ea3i. Sau khi \u0111\u00e3 kh\u1eafc ph\u1ee5c l\u1ed7i VBA s\u1ebd ch\u1ea1y l\u1ea1i tr\u00ecnh bi\u00ean d\u1ecbch l\u1ea7n n\u1eefa \u0111\u1ec3 t\u00ecm ra l\u1ed7i kh\u00e1c (n\u1ebfu c\u00f3).<\/span><\/p>\n<p><span style=\"font-weight: 400\">S\u1eed d\u1ee5ng Debug g\u1ee1 l\u1ed7i bi\u00ean d\u1ecbch r\u1ea5t h\u1eefu \u00edch v\u00ec n\u00f3 c\u0169ng c\u00f3 th\u1ec3 x\u00e1c \u0111\u1ecbnh l\u1ed7i c\u00fa ph\u00e1p trong t\u00ecm ki\u1ebfm c\u1ee7a n\u00f3.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">N\u1ebfu m\u00e3 kh\u00f4ng c\u00f3 l\u1ed7i th\u00ec t\u00f9y ch\u1ecdn Compile VBAProject s\u1ebd chuy\u1ec3n sang m\u00e0u x\u00e1m.\u00a0<\/span><\/p>\n<h4><span class=\"ez-toc-section\" id=\"322_Tom_tat_su_dung_tuy_chon_debug_xu_ly_loi_bien_dich\"><\/span><b>3.2.2 T\u00f3m t\u1eaft s\u1eed d\u1ee5ng t\u00f9y ch\u1ecdn debug x\u1eed l\u00fd l\u1ed7i bi\u00ean d\u1ecbch<\/b><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Debug\u2013&gt; Compile t\u00ecm ra l\u1ed7i to\u00e0n d\u1ef1 \u00e1n.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Debug c\u0169ng s\u1ebd t\u00ecm th\u1ea5y l\u1ed7i c\u00fa ph\u00e1p.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Debug t\u00ecm th\u1ea5y l\u1ed7i \u0111\u1ea7u ti\u00ean sau \u0111\u00f3 ch\u1ea1y l\u1ea1i tr\u00ecnh bi\u00ean d\u1ecbch \u0111\u1ec3 t\u00ecm ki\u1ebfm.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Khi kh\u00f4ng c\u00f2n l\u1ed7i bi\u00ean d\u1ecbch th\u00ec t\u00f9y ch\u1ecdn s\u1ebd chuy\u1ec3n sang m\u00e0u x\u00e1m.<\/span><\/li>\n<\/ul>\n<h4><span class=\"ez-toc-section\" id=\"323_Cach_su_dung_Debug_go_loi_bien_dich\"><\/span><b>3.2.3 C\u00e1ch s\u1eed d\u1ee5ng Debug g\u1ee1 l\u1ed7i bi\u00ean d\u1ecbch<\/b><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">B\u1ea1n n\u00ean s\u1eed d\u1ee5ng debug tr\u01b0\u1edbc khi ch\u1ea1y m\u00e3, \u0111i\u1ec1u n\u00e0y \u0111\u1ea3m b\u1ea3o m\u00e3 c\u1ee7a b\u1ea1n kh\u00f4ng c\u00f3 l\u1ed7i.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">N\u1ebfu b\u1ea1n kh\u00f4ng s\u1eed d\u1ee5ng debug th\u00ec VBA c\u00f3 th\u1ec3 ph\u00e1t hi\u1ec7n l\u1ed7i bi\u00ean d\u1ecbch khi n\u00f3 ch\u1ea1y v\u00e0 n\u00f3 s\u1ebd tr\u1edf n\u00ean ph\u1ee9c t\u1ea1p n\u1ebfu xu\u1ea5t hi\u1ec7n l\u1ed7i runtime.<\/span><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"33_Loi_Runtime\"><\/span><b>3.3 L\u1ed7i Runtime<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400\">L\u1ed7i runtime x\u1ea3y ra khi khi \u1ee9ng d\u1ee5ng \u0111ang ch\u1ea1y. \u0110i\u1ec1u n\u00e0y xu\u1ea5t ph\u00e1t t\u1eeb l\u1ed7i trong m\u00e3 v\u00e0 th\u01b0\u1eddng n\u1eb1m ngo\u00e0i t\u1ea7m ki\u1ec3m so\u00e1t.<\/span><\/p>\n<p><strong> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33647\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h4.jpg\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-4\" width=\"500\" height=\"333\" title=\"h4\"><\/strong><\/p>\n<p><span style=\"font-weight: 400\">V\u00ed d\u1ee5 n\u1ebfu ch\u1ea1y \u0111o\u1ea1n m\u00e3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 m\u1edf workbook Excel nh\u01b0ng workbook kh\u00f4ng kh\u1ea3 d\u1ee5ng (\u0111\u00e3 b\u1ecb x\u00f3a ho\u1eb7c \u0111\u01b0\u1ee3c \u0111\u1ed5i t\u00ean), m\u00e3 s\u1ebd tr\u1ea3 v\u1ec1 th\u00f4ng b\u00e1o l\u1ed7i run-time.<\/span><\/p>\n<p><span style=\"font-weight: 400\">C\u00e1c v\u00ed d\u1ee5 kh\u00e1c v\u1ec1 l\u1ed7i th\u1eddi gian ch\u1ea1y:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">M\u1ed9t c\u01a1 s\u1edf d\u1eef li\u1ec7u kh\u00f4ng c\u00f3 s\u1eb5n.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Ng\u01b0\u1eddi d\u00f9ng nh\u1eadp d\u1eef li\u1ec7u kh\u00f4ng h\u1ee3p l\u1ec7.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">M\u1ed9t \u00f4 ch\u1ee9a v\u0103n b\u1ea3n thay v\u00ec ch\u1ee9a s\u1ed1.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">M\u1ee5c \u0111\u00edch x\u1eed l\u00fd c\u00e1c l\u1ed7i bi\u00ean d\u1ecbch hay c\u00fa ph\u00e1p l\u00e0 \u0111\u1ec3 \u0111\u1ed1i ph\u00f3 v\u1edbi c\u00e1c l\u1ed7i runtime khi ch\u00fang x\u1ea3y ra.<\/span><\/p>\n<p><span style=\"font-weight: 400\">N\u1ebfu b\u1ea1n ngh\u0129 l\u1ed7i runtime c\u00f3 th\u1ec3 x\u1ea3y ra th\u00ec h\u00e3y \u0111\u1eb7t m\u00e3 v\u00e0o v\u1ecb tr\u00ed \u0111\u1ec3 x\u1eed l\u00fd n\u00f3. V\u00ed d\u1ee5, b\u1ea1n c\u00f3 th\u1ec3 \u0111\u1eb7t m\u00e3 v\u00e0o v\u1ecb tr\u00ed x\u1eed l\u00fd t\u1ec7p kh\u00f4ng \u0111\u01b0\u1ee3c t\u00ecm th\u1ea5y.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Sau \u0111\u00f3, b\u1ea1n ki\u1ec3m tra li\u1ec7u t\u1ec7p c\u00f3 t\u1ed3n t\u1ea1i tr\u01b0\u1edbc khi n\u00f3 c\u1ed1 g\u1eafng m\u1edf ra. N\u1ebfu t\u1ec7p kh\u00f4ng t\u1ed3n t\u1ea1i s\u1ebd c\u00f3 m\u1ed9t th\u00f4ng b\u00e1o hi\u1ec3n th\u1ecb .<\/span><\/p>\n<p><strong> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33648\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h5.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-5\" width=\"512\" height=\"312\" title=\"h5\"><\/strong><\/p>\n<p><b><i>L\u1ed7i mong \u0111\u1ee3i v\u00e0 kh\u00f4ng mong \u0111\u1ee3i<\/i><\/b><\/p>\n<p><span style=\"font-weight: 400\">N\u1ebfu b\u1ea1n d\u1ef1 \u0111o\u00e1n l\u1ed7i s\u1ebd xu\u1ea5t hi\u1ec7n trong qu\u00e1 tr\u00ecnh ch\u1ea1y th\u00ec b\u1ea1n c\u00f3 th\u00eam m\u00e3 \u0111\u1ec3 x\u1eed l\u00fd t\u00ecnh hu\u1ed1ng \u0111\u00f3. Tr\u01b0\u1eddng h\u1ee3p n\u00e0y l\u00e0 l\u1ed7i mong \u0111\u1ee3i.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Tuy nhi\u00ean trong tr\u01b0\u1eddng h\u1ee3p b\u1ea1n kh\u00f4ng c\u00f3 m\u00e3 c\u1ee5 th\u1ec3 \u0111\u1ec3 x\u1eed l\u00fd t\u00ecnh hu\u1ed1ng \u0111\u00f3 th\u00ec \u0111\u00e2y \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 l\u1ed7i kh\u00f4ng mong mu\u1ed1n. \u0110\u1ec3 kh\u1eafc ph\u1ee5c \u0111i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00e1c c\u00e2u l\u1ec7nh x\u1eed l\u00fd l\u1ed7i VBA.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"34_Loi_Runtime_khong_phai_la_loi_VBA\"><\/span><b>3.4 L\u1ed7i Runtime kh\u00f4ng ph\u1ea3i l\u00e0 l\u1ed7i VBA<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400\">C\u00f3 m\u1ed9t s\u1ed1 l\u1ed7i runtime kh\u00f4ng \u0111\u01b0\u1ee3c coi l\u00e0 l\u1ed7i c\u1ee7a VBA m\u00e0 do l\u1ed7i c\u1ee7a ng\u01b0\u1eddi d\u00f9ng.<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u0110\u1ec3 hi\u1ec3u r\u00f5 h\u01a1n th\u00ec s\u1ebd xem x\u00e9t m\u1ed9t v\u00ed d\u1ee5. Gi\u1ea3 s\u1eed c\u00f3 m\u1ed9t \u1ee9ng d\u1ee5ng y\u00eau c\u1ea7u b\u1ea1n th\u00eam c\u00e1c gi\u00e1 tr\u1ecb v\u00e0o c\u00e1c bi\u1ebfn a v\u00e0 b.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33649\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h6.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-6\" width=\"726\" height=\"58\" title=\"h6\"><\/p>\n<p><span style=\"font-weight: 400\">V\u00e0 b\u1ea1n s\u1eed d\u1ee5ng d\u1ea5u hoa th\u1ecb thay cho d\u1ea5u c\u1ed9ng<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33650\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h7.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-7\" width=\"721\" height=\"55\" title=\"h7\"><\/p>\n<p><span style=\"font-weight: 400\">\u0110i\u1ec1u n\u00e0y kh\u00f4ng ph\u1ea3i l\u00e0 l\u1ed7i VBA. L\u1ed7i n\u00e0y thu\u1ed9c v\u1ec1 c\u00fa ph\u00e1p v\u00e0 kh\u00f4ng kh\u1edbp v\u1edbi y\u00eau c\u1ea7u ban \u0111\u1ea7u.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Nh\u01b0ng l\u1ed7i nay kh\u00f4ng th\u1ec3 x\u1eed l\u00fd v\u1edbi b\u1ea5t k\u1ef3 error handling n\u00e0o. C\u00f3 th\u1ec3 kh\u1eafc ph\u1ee5c l\u1ed7i n\u00e0y b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng<\/span> <i><span style=\"font-weight: 400\">Unit Testing and Assertions<\/span><\/i><span style=\"font-weight: 400\">.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"4_Su_dung_cau_lenh_On_error\"><\/span><b>4. S\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh On error<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">Nh\u01b0 ch\u00fang ta \u0111\u00e3 th\u1ea5y, c\u00f3 2 c\u00e1ch x\u1eed l\u00fd l\u1ed7i runtime<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">L\u1ed7i mong \u0111\u1ee3i: vi\u1ebft m\u00e3 c\u1ee5 th\u1ec3 \u0111\u1ec3 x\u1eed l\u00fd.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">C\u00e1c l\u1ed7i kh\u00f4ng mong mu\u1ed1n: s\u1eed d\u1ee5ng c\u00e1c c\u00e2u l\u1ec7nh x\u1eed l\u00fd l\u1ed7i VBA.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400\">C\u00e2u l\u1ec7nh VBA On Error \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 x\u1eed l\u00fd l\u1ed7i. C\u00e2u l\u1ec7nh n\u00e0y th\u1ef1c hi\u1ec7n m\u1ed9t s\u1ed1 h\u00e0nh \u0111\u1ed9ng khi x\u1ea3y ra l\u1ed7i runtime.<\/span><\/p>\n<p><span style=\"font-weight: 400\">C\u00f3 b\u1ed1n c\u00e1ch \u0111\u1ec3 s\u1eed d\u1ee5ng l\u1ec7nh n\u00e0y:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><b>On error Goto 0<\/b><span style=\"font-weight: 400\">: m\u00e3 d\u1eebng l\u1ea1i d\u00f2ng g\u00e2y ra l\u1ed7i v\u00e0 hi\u1ec3n th\u1ecb th\u00f4ng b\u00e1o.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">\u00a0\u00a0<\/span><b>On Error Resume Next: <\/b><span style=\"font-weight: 400\">c\u00e1c l\u1ed7i x\u1ea3y ra s\u1ebd b\u1ecb b\u1ecf qua v\u00e0 m\u00e3 s\u1ebd ti\u1ebfp t\u1ee5c ch\u1ea1y.<\/span><\/li>\n<li style=\"font-weight: 400\"><b>On Error Go [Label]: <\/b><span style=\"font-weight: 400\">\u00a0ch\u1ec9 \u0111\u1ecbnh nh\u1eefng g\u00ec m\u00e0 b\u1ea1n mu\u1ed1n l\u00e0m trong tr\u01b0\u1eddng h\u1ee3p n\u1ebfu m\u00e3 x\u1ea3y ra l\u1ed7i.<\/span><\/li>\n<li style=\"font-weight: 400\"><b>On Error GoTo -1:<\/b><span style=\"font-weight: 400\"> X\u00f3a l\u1ed7i hi\u1ec7n t\u1ea1i.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400\">B\u00e2y gi\u1edd s\u1ebd xem x\u00e9t l\u1ea7n l\u01b0\u1ee3t c\u00e1c c\u00e2u l\u1ec7nh.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"41_On_error_Goto_0\"><\/span><b>4.1 On error Goto 0<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400\">\u0110\u00e2y l\u00e0 m\u1ed9t c\u00e2u l\u1ec7nh m\u1eb7c \u0111\u1ecbnh trong VBA. Khi x\u1ea3y ra l\u1ed7i, VBA d\u1eebng tr\u00ean d\u00f2ng c\u00f3 l\u1ed7i v\u00e0 hi\u1ec3n th\u1ecb th\u00f4ng b\u00e1o. \u1ee8ng d\u1ee5ng n\u00e0y y\u00eau c\u1ea7u can thi\u1ec7p c\u1ee7a ng\u01b0\u1eddi d\u00f9ng v\u1edbi m\u00e3 tr\u01b0\u1edbc khi ti\u1ebfp t\u1ee5c. B\u1ea1n c\u00f3 th\u1ec3 s\u1eeda l\u1ed7i ho\u1eb7c kh\u1edfi \u0111\u1ed9ng l\u1ea1i \u1ee9ng d\u1ee5ng. Trong tr\u01b0\u1eddng h\u1ee3p n\u00e0y x\u1eed l\u00fd l\u1ed7i kh\u00f4ng di\u1ec5n ra.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Xem\u00a0 x\u00e9t m\u1ed9t v\u00ed d\u1ee5. Trong m\u00e3 d\u01b0\u1edbi \u0111\u00e2y ch\u00fang ta kh\u00f4ng s\u1eed d\u1ee5ng l\u1ec7nh On error\u00a0 tr\u00ean b\u1ea5t k\u1ef3 d\u00f2ng n\u00e0o v\u00ec v\u1eady VBA s\u1ebd d\u1ee5ng <\/span><i><span style=\"font-weight: 400\">On error Goto 0<\/span><\/i><span style=\"font-weight: 400\"> theo m\u1eb7c \u0111\u1ecbnh.<\/span><\/p>\n<p><span style=\"font-weight: 400\"> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33651\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h8.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-8\" width=\"726\" height=\"268\" title=\"h8\"><\/span><\/p>\n<p><span style=\"font-weight: 400\">\u1ede d\u00f2ng g\u00e1n th\u1ee9 2 d\u1eabn \u0111\u1ebfn l\u1ed7i chia cho 0. Khi m\u00e3 ch\u1ea1y b\u1ea1n s\u1ebd nh\u1eadn \u0111\u01b0\u1ee3c th\u00f4ng b\u00e1o l\u1ed7i hi\u1ec3n th\u1ecb nh\u01b0 \u1ea3nh ch\u1ee5p m\u00e0n h\u00ecnh d\u01b0\u1edbi \u0111\u00e2y.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33652\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h9.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-9\" width=\"613\" height=\"446\" title=\"h9\"><\/p>\n<p><span style=\"font-weight: 400\">Khi l\u1ed7i xu\u1ea5t hi\u1ec7n, b\u1ea1n c\u00f3 th\u1ec3 ch\u1ecdn END ho\u1eb7c Debug.<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">N\u1ebfu b\u1ea1n ch\u1ecdn End\u00a0 th\u00ec \u1ee9ng d\u1ee5ng d\u1eebng l\u1ea1i.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">N\u1ebfu b\u1ea1n ch\u1ecdn Debug th\u00ec \u1ee9ng d\u1ee5ng d\u1eebng tr\u00ean d\u00f2ng l\u1ed7i\u00a0 nh\u01b0 \u1ea3nh ch\u1ee5p d\u01b0\u1edbi \u0111\u00e2y.<\/span><\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33653\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/vooo.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-10\" width=\"372\" height=\"339\" title=\"vooo\"><\/p>\n<p><span style=\"font-weight: 400\">\u0110i\u1ec1u n\u00e0y s\u1ebd t\u1ed1t n\u1ebfu b\u1ea1n \u0111ang vi\u1ebft m\u00e3 VBA v\u00ec n\u00f3 hi\u1ec3n th\u1ecb cho b\u1ea1n d\u00f2ng l\u1ed7i ch\u00ednh x\u00e1c.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Tuy nhi\u00ean n\u00f3 l\u1ea1i kh\u00f4ng ph\u00f9 h\u1ee3p v\u1edbi m\u1ed9t s\u1ed1 \u1ee9ng d\u1ee5ng \u0111\u00e3 \u0111\u01b0\u1ee3c c\u1ea5p cho ng\u01b0\u1eddi d\u00f9ng. Nh\u1eefng l\u1ed7i n\u00e0y th\u1ef1c ch\u1ea5t l\u00e0 \u1ee9ng d\u1ee5ng b\u1ecb treo, kh\u00f4ng \u1ed5n \u0111\u1ecbnh v\u00ec ng\u01b0\u1eddi d\u00f9ng ph\u1ea3i kh\u1edfi \u0111\u1ed9ng l\u1ea1i \u1ee9ng d\u1ee5ng. N\u1ebfu b\u1ea1n kh\u00f4ng s\u1eeda l\u1ed7i th\u00ec qu\u00e1 tr\u00ecnh c\u00f3 th\u1ec3 b\u1ecb d\u1eebng l\u1ea1i.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">Thay v\u00e0o \u0111\u00f3 b\u1ea1n s\u1eed d\u1ee5ng On Error Go To (Label) ng\u01b0\u1eddi d\u00f9ng c\u00f3 th\u1ec3 ki\u1ec3m so\u00e1t t\u1ed1t h\u01a1n, \u1ee9ng d\u1ee5ng c\u00f3 th\u1ec3 ti\u1ebfp t\u1ee5c ch\u1ea1y ho\u1eb7c theo c\u00f3 th\u1ec3 theo ch\u1ec9 \u0111\u1ecbnh kh\u00e1c c\u1ee7a ng\u01b0\u1eddi d\u00f9ng.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"42_Cau_lenh_On_Error_Resume_Next\"><\/span><b>4.2 C\u00e2u l\u1ec7nh On Error Resume Next<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400\">S\u1eed d\u1ee5ng\u00a0<\/span><b>On Error Resume <\/b><span style=\"font-weight: 400\">m\u1ee5c \u0111\u00edch<\/span> <span style=\"font-weight: 400\">y\u00eau c\u1ea7u VBA b\u1ecf qua l\u1ed7i v\u00e0 ti\u1ebfp t\u1ee5c.<\/span><\/p>\n<p><span style=\"font-weight: 400\">C\u00e2u l\u1ec7nh n\u00e0y c\u0169ng r\u1ea5t h\u1eefu \u00edch nh\u01b0ng kh\u00f4ng ph\u1ea3i l\u00fac n\u00e0o c\u0169ng c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng.<\/span><\/p>\n<p><span style=\"font-weight: 400\">N\u1ebfu ch\u00fang ta th\u00eam\u00a0<\/span><b>Resume Next<\/b><span style=\"font-weight: 400\">\u00a0v\u00e0o trong Sub c\u1ee7a v\u00ed d\u1ee5\u00a0 d\u01b0\u1edbi \u0111\u00e2y th\u00ec VBA s\u1ebd b\u1ecf qua l\u1ed7i chia cho 0.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33655\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/bs1.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-11\" width=\"723\" height=\"310\" title=\"bs1\"><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-weight: 400\">\u0110i\u1ec1u n\u00e0y kh\u00f4ng h\u1eb3n t\u1ed1t. N\u1ebfu b\u1ecf qua nh\u1eefng l\u1ed7i nh\u01b0 th\u1ebf n\u00e0y b\u1ea1n c\u00f3 th\u1ec3 nh\u1eadn \u0111\u01b0\u1ee3c d\u1eef li\u1ec7u kh\u00f4ng h\u1ee3p l\u1ec7.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">M\u00e3 d\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t v\u00ed d\u1ee5 v\u1ec1 s\u1eed d\u1ee5ng Resume Next l\u00e0 h\u1eefu \u00edch.\u00a0<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33656\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/bs2.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-12\" width=\"726\" height=\"444\" title=\"bs2\"><\/p>\n<p><span style=\"font-weight: 400\">Trong m\u00e3 n\u00e0y, m\u1ee5c \u0111\u00edch ch\u1ec9 ki\u1ec3m tra xem Microsoft Outlook c\u00f3 s\u1eb5n tr\u00ean m\u00e1y t\u00ednh hay kh\u00f4ng v\u00e0 kh\u00f4ng quan t\u00e2m \u0111\u1ebfn l\u1ed7i c\u1ee5 th\u1ec3.<\/span><\/p>\n<p>\u00a0<\/p>\n<p><span style=\"font-weight: 400\">Trong m\u00e3 tr\u00ean, \u1ee9ng d\u1ee5ng v\u1eabn s\u1ebd ti\u1ebfp t\u1ee5c n\u1ebfu n\u00f3 xu\u1ea5t hi\u1ec7n l\u1ed7i. Trong d\u00f2ng ti\u1ebfp theo s\u1ebd ki\u1ec3m tra gi\u00e1 tr\u1ecb bi\u1ebfn Outlook. N\u1ebfu ph\u00e1t sinh l\u1ed7i gi\u00e1 tr\u1ecb c\u1ee7a bi\u1ebfn n\u00e0y \u0111\u01b0\u1ee3c \u0111\u1eb7t th\u00e0nh Nothing.<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u0110\u00e2y l\u00e0 v\u00ed d\u1ee5 cho th\u1ea5y s\u1ef1 h\u1eefu \u00edch c\u1ee7a h\u00e0m Resume. Trong h\u1ea7u h\u1ebft nhi\u1ec1u m\u00e3 th\u00ec Resume r\u1ea5t h\u1ea1n ch\u1ebf s\u1eed d\u1ee5ng. Ch\u1ec9 n\u00ean s\u1eed d\u1ee5ng l\u1ec7nh On Error Resume Next khi ch\u00fang ta bi\u1ebft r\u00f5 lo\u1ea1i l\u1ed7i m\u00e0 m\u00e3 VBA s\u1ebd b\u1ecf qua v\u00e0 c\u00f3 th\u1ec3 b\u1ecf qua l\u1ed7i \u0111\u00f3.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"43_Lenh_On_Error_GoTo_label\"><\/span><b>4.3 L\u1ec7nh On Error GoTo [label]<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400\">\u0110\u00e2y l\u00e0 m\u1ed9t trong nh\u1eefng ph\u01b0\u01a1ng ph\u00e1p x\u1eed l\u00fd l\u1ed7i VBA. N\u00f3 gi\u1ed1ng v\u1edbi c\u00e2u l\u1ec7nh Try v\u00e0 Catch trong C# v\u00e0 Java.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Khi x\u1ea3y ra l\u1ed7i b\u1ea1n \u0111\u1eb7t n\u00f3 v\u00e0o m\u1ed9t label c\u1ee5 th\u1ec3. Th\u00f4ng th\u01b0\u1eddng l\u00e0 \u1edf d\u01b0\u1edbi c\u00f9ng c\u1ee7a Sub.<\/span><\/p>\n<p><span style=\"font-weight: 400\">H\u00e3y xem x\u00e9t v\u00ed d\u1ee5 d\u01b0\u1edbi \u0111\u00e2y:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33657\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h10-1.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-13\" width=\"725\" height=\"410\" title=\"h10-1\"><\/p>\n<p><span style=\"font-weight: 400\">\u1ea2nh ch\u1ee5p m\u00e0n h\u00ecnh cho th\u1ea5y khi x\u1ea3y ra l\u1ed7i<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33658\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h11.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-14\" width=\"814\" height=\"448\" title=\"h11\"><\/p>\n<p><span style=\"font-weight: 400\">VBA nh\u1ea3y \u0111\u1ebfn\u00a0nh\u00e3n\u00a0<\/span><b>eh<\/b><span style=\"font-weight: 400\">\u00a0v\u00ec ch\u00fang ta \u0111\u00e3 ch\u1ec9 \u0111\u1ecbnh \u0111i\u1ec1u n\u00e0y trong d\u00f2ng\u00a0<\/span><b>On Error Goto<\/b><span style=\"font-weight: 400\">.<\/span><\/p>\n<p><b>L\u01b0u \u00fd 1:<\/b><span style=\"font-weight: 400\">\u00a0Label ch\u00fang ta s\u1eed d\u1ee5ng trong c\u00e2u l\u1ec7nh On\u2026 GoTo, ph\u1ea3i n\u1eb1m trong Sub\/Function hi\u1ec7n t\u1ea1i.\u00a0N\u1ebfu kh\u00f4ng, b\u1ea1n s\u1ebd g\u1eb7p l\u1ed7i bi\u00ean d\u1ecbch.<\/span><\/p>\n<p><b>L\u01b0u \u00fd 2:<\/b><span style=\"font-weight: 400\">\u00a0N\u1ebfu x\u1ea3y ra l\u1ed7i khi s\u1eed d\u1ee5ng\u00a0<\/span><i><span style=\"font-weight: 400\">On Error GoTo [Label]<\/span><\/i><span style=\"font-weight: 400\">, vi\u1ec7c x\u1eed l\u00fd l\u1ed7i tr\u1edf v\u1ec1 h\u00e0nh vi m\u1eb7c \u0111\u1ecbnh t\u1ee9c l\u00e0 m\u00e3 s\u1ebd d\u1eebng tr\u00ean d\u00f2ng c\u00f3 l\u1ed7i v\u00e0 hi\u1ec3n th\u1ecb th\u00f4ng b\u00e1o l\u1ed7i.\u00a0<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"44_On_Error_GoTo_-1\"><\/span><b>4.4 On Error GoTo -1<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400\">L\u1ec7nh n\u00e0y kh\u00e1c v\u1edbi 3 l\u1ec7nh c\u00f2n l\u1ea1i. N\u00f3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 x\u00f3a l\u1ed7i hi\u1ec7n t\u1ea1i thay v\u00ec thi\u1ebft l\u1eadp m\u1ed9t h\u00e0nh vi c\u1ee5 th\u1ec3.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Khi x\u1ea3y ra l\u1ed7i b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng On Error GoTo [label], h\u00e0nh vi x\u1eed l\u00fd l\u1ed7i s\u1ebd tr\u1edf v\u1ec1 h\u00e0nh vi m\u1eb7c \u0111\u1ecbnh \u201c<\/span><i><span style=\"font-weight: 400\">On Error GoTo 0<\/span><\/i><span style=\"font-weight: 400\">\u201d. C\u00f3 ngh\u0129a l\u00e0 n\u1ebfu m\u1ed9t l\u1ed7i kh\u00e1c x\u1ea3y ra, m\u00e3 s\u1ebd d\u1eebng tr\u00ean d\u00f2ng hi\u1ec7n t\u1ea1i.<\/span><\/p>\n<p><span style=\"font-weight: 400\">H\u00e0nh vi n\u00e0y ch\u1ec9 \u00e1p d\u1ee5ng cho Sub hi\u1ec7n t\u1ea1i.\u00a0Khi ch\u00fang ta exit sub, l\u1ed7i s\u1ebd t\u1ef1 \u0111\u1ed9ng \u0111\u01b0\u1ee3c x\u00f3a.<\/span><\/p>\n<p><span style=\"font-weight: 400\">H\u00e3y xem \u0111o\u1ea1n m\u00e3 d\u01b0\u1edbi \u0111\u00e2y.\u00a0L\u1ed7i \u0111\u1ea7u ti\u00ean s\u1ebd l\u00e0m cho m\u00e3 nh\u1ea3y \u0111\u1ebfn\u00a0nh\u00e3n\u00a0<\/span><i><span style=\"font-weight: 400\">eh<\/span><\/i><span style=\"font-weight: 400\">.\u00a0L\u1ed7i th\u1ee9 hai s\u1ebd d\u1eebng tr\u00ean d\u00f2ng v\u1edbi l\u1ed7i 1034.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33659\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h12.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-15\" width=\"583\" height=\"287\" title=\"h12\"><\/p>\n<p><span style=\"font-weight: 400\">N\u1ebfu ch\u00fang ta th\u00eam error handling th\u00ec n\u00f3 s\u1ebd kh\u00f4ng ho\u1ea1t \u0111\u1ed9ng v\u00ec l\u1ed7i ch\u01b0a \u0111\u01b0\u1ee3c x\u00f3a.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Trong \u0111o\u1ea1n m\u00e3 d\u01b0\u1edbi \u0111\u00e2y, ch\u00fang ta th\u00eam m\u1ed9t d\u00f2ng.\u00a0<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33660\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h13.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-16\" width=\"719\" height=\"60\" title=\"h13\"><\/p>\n<p><span style=\"font-weight: 400\">Sau khi ch\u00fang ta b\u1eaft \u0111\u01b0\u1ee3c l\u1ed7i \u0111\u1ea7u ti\u00ean.<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u0110i\u1ec1u n\u00e0y kh\u00f4ng c\u00f3 t\u00e1c d\u1ee5ng v\u00ec l\u1ed7i v\u1eabn ch\u01b0a \u0111\u01b0\u1ee3c x\u00f3a.\u00a0N\u00f3i c\u00e1ch kh\u00e1c, m\u00e3 s\u1ebd d\u1eebng tr\u00ean d\u00f2ng c\u00f3 l\u1ed7i v\u00e0 hi\u1ec3n th\u1ecb th\u00f4ng b\u00e1o.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33661\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h14.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-17\" width=\"728\" height=\"456\" title=\"h14\"><\/p>\n<p><span style=\"font-weight: 400\">\u0110\u1ec3 x\u00f3a l\u1ed7i, ch\u00fang t\u00f4i s\u1eed d\u1ee5ng\u00a0<\/span><i><span style=\"font-weight: 400\">On Error GoTo -1<\/span><\/i><span style=\"font-weight: 400\">.<\/span> <span style=\"font-weight: 400\">H\u00e3y xem n\u00f3 nh\u01b0 b\u1eaby chu\u1ed9t khi b\u1eaby xong b\u1ea1n c\u1ea7n \u0111\u1eb7t l\u1ea1i.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Trong \u0111o\u1ea1n m\u00e3 b\u00ean d\u01b0\u1edbi, ch\u00fang ta th\u00eam d\u00f2ng n\u00e0y v\u00e0 l\u1ed7i th\u1ee9 hai b\u00e2y gi\u1edd s\u1ebd khi\u1ebfn m\u00e3 nh\u1ea3y sang\u00a0nh\u00e3n\u00a0<\/span><i><span style=\"font-weight: 400\">eh_other<\/span><\/i><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33662\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h15.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-18\" width=\"580\" height=\"427\" title=\"h15\"><\/p>\n<p><b><i>L\u01b0u \u00fd 1: <\/i><\/b><span style=\"font-weight: 400\">Trong nhi\u1ec1u tr\u01b0\u1eddng th\u00ec s\u1eed d\u1ee5ng l\u1ec7nh On Error Goto \u2013 1 kh\u00f4ng th\u1ef1c s\u1ef1 h\u1eefu \u00edch. Trong nhi\u1ec1u t\u00ecnh hu\u1ed1ng s\u1eed d\u1ee5ng Resume Next s\u1ebd t\u1ed1t h\u01a1n v\u00ec n\u00f3 s\u1ebd x\u00f3a l\u1ed7i v\u00e0 m\u00e3 s\u1ebd ti\u1ebfp t\u1ee5c ch\u1ea1y.<\/span><\/p>\n<p><b><i>L\u01b0u \u00fd 2<\/i><\/b><span style=\"font-weight: 400\">: C\u00e1c Err Object c\u00f3 \u0111\u1ed1i t\u01b0\u1ee3ng\u00a0 r\u00f5 r\u00e0ng. S\u1eed d\u1ee5ng Clear s\u1ebd x\u00f3a v\u0103n b\u1ea3n v\u00e0 s\u1ed1 trong Err Objective, nh\u01b0ng n\u00f3 KH\u00d4NG \u0111\u1eb7t l\u1ea1i l\u1ed7i.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"45_Su_dung_lenh_On_Error\"><\/span><b>4.5 S\u1eed d\u1ee5ng l\u1ec7nh On Error<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400\">Nh\u01b0 \u0111\u00e3 n\u00f3i, VBA s\u1ebd thao t\u00e1c m\u1ed9t trong 3 \u0111i\u1ec1u sau khi x\u1ea3y ra l\u1ed7i:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">D\u1eebng l\u1ea1i v\u00e0 hi\u1ec3n th\u1ecb.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">B\u1ecf qua v\u00e0 ti\u1ebfp t\u1ee5c.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Chuy\u1ec3n \u0111\u1ebfn m\u1ed9t d\u00f2ng c\u1ee5 th\u1ec3.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400\">VBA s\u1ebd lu\u00f4n th\u1ef1c hi\u1ec7n m\u1ed9t trong nh\u1eefng thao t\u00e1c n\u00e0y. Khi s\u1eed d\u1ee5ng l\u1ec7nh On Error VBA s\u1ebd thay \u0111\u1ed5i h\u00e0nh vi m\u00e0 b\u1ea1n ch\u1ec9 \u0111\u1ecbnh v\u00e0 x\u00f3a \u0111i c\u00e1c h\u00e0nh vi tr\u01b0\u1edbc \u0111\u00f3.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Trong SUB sau, VBA thay \u0111\u1ed5i h\u00e0nh vi l\u1ed7i m\u1ed7i khi s\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh On error.\u00a0<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33663\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h16.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-19\" width=\"740\" height=\"542\" title=\"h16\"><\/p>\n<h2><span class=\"ez-toc-section\" id=\"5_Resume_Next\"><\/span><b>5. Resume Next<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">C\u00e2u l\u1ec7nh n\u00e0y s\u1eed d\u1ee5ng \u0111\u1ec3 x\u00f3a l\u1ed7i v\u00e0 m\u00e3 s\u1ebd ti\u1ebfp t\u1ee5c ch\u1ea1y t\u1eeb d\u00f2ng sau khi s\u1eeda l\u1ed7i. N\u1ebfu m\u00e3 c\u1ee7a b\u1ea1n c\u00f3 nhi\u1ec1u l\u1ed7i v\u00e0 mu\u1ed1n ti\u1ebfp t\u1ee5c ph\u00e1t hi\u1ec7n th\u00ec l\u1ec7nh n\u00e0y h\u1eefu \u00edch.<\/span><\/p>\n<p><span style=\"font-weight: 400\">V\u00ed d\u1ee5: trong \u0111o\u1ea1n m\u00e3 sau, m\u00e3 s\u1ebd ti\u1ebfp t\u1ee5c ch\u1ea1y sau khi l\u1ed7i xu\u1ea5t hi\u1ec7n:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33664\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h17.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-20\" width=\"705\" height=\"382\" title=\"h17\"><\/p>\n<p><span style=\"font-weight: 400\">Ch\u00fang ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng On Error Goto -1 \u0111\u1ec3 x\u00f3a m\u00e3 v\u00e0 sau \u0111\u00f3 s\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh n\u00e0y \u0111\u1ec3 quay l\u1ea1i m\u00e3 nh\u01b0 sau:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33665\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h18.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-21\" width=\"722\" height=\"457\" title=\"h18\"><\/p>\n<p><span style=\"font-weight: 400\">C\u00e1c\u00a0<\/span><i><span style=\"font-weight: 400\">Resume Next<\/span><\/i><span style=\"font-weight: 400\"> cung c\u1ea5p c\u00e1ch ho\u1ea1t \u0111\u1ed9ng d\u1ec5 d\u00e0ng h\u01a1n v\u1edbi c\u00e1c m\u00e3 \u0111\u01b0\u1ee3c r\u00f5 r\u00e0ng v\u00e0 d\u1ec5 hi\u1ec3u h\u01a1n<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"6_Err_Objective\"><\/span><b>6. Err Objective<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">Khi l\u1ed7i x\u1ea3y ra, b\u1ea1n c\u00f3 th\u1ec3 xem chi ti\u1ebft v\u1ec1 l\u1ed7i b\u1eb1ng\u00a0\u0111\u1ed1i t\u01b0\u1ee3ng\u00a0<\/span><b>Err Objective<\/b><span style=\"font-weight: 400\">\u00a0.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Khi x\u1ea3y ra l\u1ed7i runtime VBA s\u1ebd t\u1ef1 \u0111\u1ed9ng \u0111i\u1ec1n th\u00f4ng\u00a0tin chi ti\u1ebft v\u00e0o Err Objective.<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u0110o\u1ea1n m\u00e3 d\u01b0\u1edbi \u0111\u00e2y s\u1ebd xu\u1ea5t hi\u1ec7n <\/span><i><span style=\"font-weight: 400\">Error Number \u201c13 Type Mismatch\u201d<\/span><\/i><span style=\"font-weight: 400\">\u00a0 khi ch\u00fang ta c\u1ed1 g\u1eafng \u0111\u1eb7t m\u1ed9t gi\u00e1 tr\u1ecb chu\u1ed7i trong total.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33666\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h19.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-22\" width=\"742\" height=\"372\" title=\"h19\"><\/p>\n<p><span style=\"font-weight: 400\">C\u00e1c Err.Description cung c\u1ea5p chi ti\u1ebft v\u1ec1 c\u00e1c l\u1ed7i x\u1ea3y ra v\u00ed d\u1ee5: \u201cType Mismatch\u201d<\/span><\/p>\n<p><span style=\"font-weight: 400\">C\u00e1c\u00a0<\/span><b>Err.Number<\/b><span style=\"font-weight: 400\">\u00a0l\u00e0 s\u1ed1 ID c\u1ee7a l\u1ed7i v\u00ed d\u1ee5 nh\u01b0 s\u1ed1 l\u01b0\u1ee3ng l\u1ed7i cho \u201cType mismatch\u201d l\u00e0 13. \u0110i\u1ec1u n\u00e0y chi th\u1eadt s\u1ef1 c\u1ea7n thi\u1ebft khi b\u1ea1n mu\u1ed1n ki\u1ec3m tra c\u00e1c l\u1ed7i c\u1ee5 th\u1ec3 v\u00e0 n\u00f3 kh\u00f4ng th\u1eadt s\u1ef1 th\u01b0\u1eddng xuy\u00ean.<\/span><\/p>\n<p><span style=\"font-weight: 400\">C\u00e1c\u00a0<\/span><b>Err.Source<\/b><span style=\"font-weight: 400\">\u00a0l\u00e0 m\u1ed9t \u00fd t\u01b0\u1edfng tuy\u1ec7t v\u1eddi nh\u01b0ng n\u00f3 l\u1ea1i kh\u00f4ng l\u00e0m vi\u1ec7c tr\u01b0\u1eddng h\u1ee3p ch\u1ec9 c\u00f3 1 l\u1ed7i VBA.\u00a0Ngu\u1ed3n s\u1ebd ch\u1ec9 tr\u1ea3 v\u1ec1 t\u00ean n\u00ean v\u1ecb tr\u00ed x\u1ea3y ra l\u1ed7i kh\u00f4ng \u0111\u01b0\u1ee3c thu h\u1eb9p.\u00a0Tuy nhi\u00ean, n\u1ebfu b\u1ea1n t\u1ea1o ra l\u1ed7i khi s\u1eed d\u1ee5ng\u00a0<\/span><b>Err.Raise,<\/b><span style=\"font-weight: 400\">\u00a0b\u1ea1n c\u00f3 th\u1ec3 t\u1ef1 \u0111\u1eb7t ngu\u1ed3n v\u00e0 \u0111i\u1ec1u n\u00e0y c\u00f3 th\u1ec3 r\u1ea5t h\u1eefu \u00edch.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"61_Lay_so_dong\"><\/span><b>6.1 L\u1ea5y s\u1ed1 d\u00f2ng<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400\">H\u00e0m\u00a0<\/span><b>Erl<\/b><span style=\"font-weight: 400\">\u00a0\u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 tr\u1ea3 v\u1ec1 s\u1ed1 d\u00f2ng x\u1ea3y ra l\u1ed7i.<\/span><\/p>\n<p><span style=\"font-weight: 400\">N\u00f3 th\u01b0\u1eddng g\u00e2y ra s\u1ef1 nh\u1ea7m l\u1eabn. Trong \u0111o\u1ea1n m\u00e3 sau, Erl s\u1ebd tr\u1ea3 v\u1ec1 0<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33667\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h20.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-23\" width=\"721\" height=\"341\" title=\"h20\"><\/p>\n<p><span style=\"font-weight: 400\">\u0110i\u1ec1u n\u00e0y l\u00e0 do kh\u00f4ng c\u00f3 s\u1ed1 d\u00f2ng n\u00e0o.\u00a0H\u1ea7u h\u1ebft m\u1ecdi ng\u01b0\u1eddi kh\u00f4ng nh\u1eadn ra \u0111i\u1ec1u \u0111\u00f3 nh\u01b0ng VBA cho ph\u00e9p b\u1ea1n c\u00f3 s\u1ed1 d\u00f2ng.<\/span><\/p>\n<p><span style=\"font-weight: 400\">N\u1ebfu ch\u00fang ta thay \u0111\u1ed5i Sub \u1edf tr\u00ean \u0111\u1ec3 c\u00f3 s\u1ed1 d\u00f2ng th\u00ec b\u00e2y gi\u1edd n\u00f3 s\u1ebd th\u1ec3 hi\u1ec7n ra 20.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33668\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/bs3.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-24\" width=\"579\" height=\"278\" title=\"bs3\"><\/p>\n<p><span style=\"font-weight: 400\">Th\u00eam s\u1ed1 d\u00f2ng v\u00e0o m\u00e3 c\u1ee7a b\u1ea1n theo c\u00e1ch th\u1ee7 c\u00f4ng l\u00e0 r\u1ea5t ph\u1ee9c t\u1ea1p. Tuy nhi\u00ean, c\u00f3 nh\u1eefng c\u00f4ng c\u1ee5 c\u00f3 s\u1eb5n s\u1ebd cho ph\u00e9p b\u1ea1n d\u1ec5 d\u00e0ng th\u00eam v\u00e0 x\u00f3a s\u1ed1 d\u00f2ng cho sub.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Khi b\u1ea1n ho\u00e0n th\u00e0nh v\u00e0 giao n\u00f3 cho ng\u01b0\u1eddi d\u00f9ng, b\u1ea1n c\u00f3 th\u1ec3 th\u00eam s\u1ed1 d\u00f2ng. N\u1ebfu b\u1ea1n s\u1eed d\u1ee5ng chi\u1ebfn l\u01b0\u1ee3c x\u1eed l\u00fd l\u1ed7i trong ph\u1ea7n cu\u1ed1i c\u00f9ng c\u1ee7a b\u00e0i \u0111\u0103ng n\u00e0y, th\u00ec VBA s\u1ebd b\u00e1o c\u00e1o d\u00f2ng x\u1ea3y ra l\u1ed7i.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"62_Su_dung_ErrRaise\"><\/span><b>6.2 S\u1eed d\u1ee5ng Err.Raise<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400\">Err.Raise cho ph\u00e9p ch\u00fang ta t\u1ea1o ra l\u1ed7i. Ch\u00fang ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng \u0111\u1ec3 t\u1ea1o l\u1ed7i t\u00f9y ch\u1ec9nh cho \u1ee9ng d\u1ee5ng c\u1ee7a m\u00ecnh. N\u00f3 t\u01b0\u01a1ng \u0111\u01b0\u01a1ng v\u1edbi c\u00e2u l\u1ec7nh Throw trong Java\u00a0c#.<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u0110\u1ecbnh d\u1ea1ng nh\u01b0 sau<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33669\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h21.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-25\" width=\"724\" height=\"46\" title=\"h21\"><\/p>\n<p><span style=\"font-weight: 400\">H\u00e3y xem m\u1ed9t v\u00ed d\u1ee5 \u0111\u01a1n gi\u1ea3n.\u00a0T\u01b0\u1edfng t\u01b0\u1ee3ng b\u1ea1n mu\u1ed1n nh\u1eadp c\u00f3 \u0111\u1ed9 d\u00e0i 5 k\u00fd t\u1ef1 v\u00e0o m\u1ed9t \u00f4.\u00a0Ch\u00fang t\u00f4i c\u00f3 th\u1ec3 c\u00f3 m\u1ed9t th\u00f4ng b\u00e1o c\u1ee5 th\u1ec3 cho \u0111i\u1ec1u n\u00e0y<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-33670 size-full\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h22.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-26\" width=\"574\" height=\"485\" title=\"h22\"><\/p>\n<p><span style=\"font-weight: 400\">Khi t\u1ea1o l\u1ed7i b\u1eb1ng\u00a0<\/span><i><span style=\"font-weight: 400\">Err.Raise,<\/span><\/i><span style=\"font-weight: 400\">\u00a0ch\u00fang ta c\u1ea7n cung c\u1ea5p cho n\u00f3 m\u1ed9t s\u1ed1.\u00a0Ch\u00fang ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng b\u1ea5t k\u1ef3 s\u1ed1 n\u00e0o t\u1eeb 513 \u0111\u1ebfn 65535 cho l\u1ed7i.\u00a0Ch\u00fang ta ph\u1ea3i s\u1eed d\u1ee5ng\u00a0<\/span><i><span style=\"font-weight: 400\">vbObjectError<\/span><\/i><span style=\"font-weight: 400\">.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33671\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h23.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-27\" width=\"577\" height=\"39\" title=\"h23\"><\/p>\n<h3><span class=\"ez-toc-section\" id=\"63_Su_dung_Err_Clear\"><\/span><b>6.3 S\u1eed d\u1ee5ng Err Clear<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400\">Err.Clear s\u1eed d\u1ee5ng \u0111\u1ec3 x\u00f3a v\u0103n b\u1ea3n v\u00e0 s\u1ed1 kh\u1ecfi Err.Object. V\u1edbi nh\u1eefng l\u1ed7i th\u1ef1c t\u1ebf th\u00ec ch\u00fang ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng <\/span><i><span style=\"font-weight: 400\">On Error Go To -1<\/span><\/i><span style=\"font-weight: 400\"> ho\u1eb7c <\/span><i><span style=\"font-weight: 400\">Resume Next.<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400\">Err.Clear hi\u1ebfm khi s\u1eed d\u1ee5ng nh\u01b0ng m\u1ed9t khi s\u1eed d\u1ee5ng th\u00ec r\u1ea5t h\u1eefu \u00edch. B\u1ea1n c\u00f3 th\u1ec3 xem m\u1ed9t v\u00ed d\u1ee5 d\u01b0\u1edbi \u0111\u00e2y.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Trong \u0111o\u1ea1n m\u00e3 d\u01b0\u1edbi \u0111\u00e2y, ch\u00fang ta \u0111\u1ebfm s\u1ed1 l\u1ed7i s\u1ebd x\u1ea3y ra. \u0110\u1ec3 \u0111\u01a1n gi\u1ea3n, v\u1edbi m\u1ed7i s\u1ed1 l\u1ebb ch\u00fang ta t\u1ea1o ra m\u1ed9t l\u1ed7i.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Ch\u00fang ta ki\u1ec3m tra s\u1ed1 l\u1ed7i m\u1ed7i l\u1ea7n \u0111i qua v\u00f2ng l\u1eb7p. N\u1ebfu s\u1ed1 kh\u00e1c 0 th\u00ec \u0111\u00e3 x\u1ea3y ra l\u1ed7i. Khi ch\u00fang ta \u0111\u1ebfm l\u1ed7i, ch\u00fang t\u00f4i c\u1ea7n \u0111\u1eb7t s\u1ed1 l\u1ed7i v\u1ec1 0 \u0111\u1ec3 n\u00f3 s\u1eb5n s\u00e0ng ki\u1ec3m tra l\u1ed7i ti\u1ebfp theo.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33672\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h24.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-28\" width=\"579\" height=\"427\" title=\"h24\"><\/p>\n<p><b>L\u01b0u \u00fd 1:<\/b><span style=\"font-weight: 400\"> Err.Clear \u0111\u1eb7t l\u1ea1i v\u0103n b\u1ea3n v\u00e0 s\u1ed1 trong \u0111\u1ed1i t\u01b0\u1ee3ng l\u1ed7i nh\u01b0ng n\u00f3 kh\u00f4ng x\u00f3a l\u1ed7i\u00a0<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"7_Logging\"><\/span><b>7. Logging<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">Logging c\u00f3 ngh\u0129a l\u00e0 b\u1ea1n ghi l\u1ea1i nh\u1eefng th\u00f4ng tin t\u1eeb \u1ee9ng d\u1ee5ng khi \u1ee9ng d\u1ee5ng \u0111ang ch\u1ea1y. Khi x\u1ea3y ra l\u1ed7i b\u1ea1n c\u00f3 th\u1ec3 vi\u1ebft chi ti\u1ebft v\u1ec1 c\u00e1c l\u1ed7i \u0111\u00f3 v\u00e0o m\u1ed9t t\u1ec7p v\u0103n b\u1ea3n.<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u0110o\u1ea1n m\u00e3 d\u01b0\u1edbi \u0111\u00e2y cho th\u1ea5y quy tr\u00ecnh logging r\u1ea5t \u0111\u01a1n gi\u1ea3n:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33673\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h25.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-29\" width=\"577\" height=\"473\" title=\"h25\"><\/p>\n<p><span style=\"font-weight: 400\">B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng n\u00f3 nh\u01b0 th\u1ebf n\u00e0y<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33674\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h26.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-30\" width=\"576\" height=\"359\" title=\"h26\"><\/p>\n<p><span style=\"font-weight: 400\">Logging kh\u00f4ng ch\u1ec9 \u0111\u1ec3 ghi l\u1ea1i c\u00e1c l\u1ed7i.\u00a0B\u1ea1n c\u00f3 th\u1ec3 ghi l\u1ea1i c\u00e1c th\u00f4ng tin kh\u00e1c khi \u1ee9ng d\u1ee5ng ch\u1ea1y.\u00a0Khi x\u1ea3y ra l\u1ed7i, b\u1ea1n c\u00f3 th\u1ec3 ki\u1ec3m tra chu\u1ed7i s\u1ef1 ki\u1ec7n tr\u01b0\u1edbc khi x\u1ea3y ra l\u1ed7i.<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u00a0D\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t v\u00ed d\u1ee5 v\u1ec1 logging.\u00a0C\u00e1ch b\u1ea1n tri\u1ec3n khai logging th\u1ef1c s\u1ef1 ph\u1ee5 thu\u1ed9c v\u00e0o b\u1ea3n ch\u1ea5t c\u1ee7a \u1ee9ng d\u1ee5ng v\u00e0 m\u1ee9c \u0111\u1ed9 h\u1eefu \u00edch c\u1ee7a n\u00f3.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33675\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h27.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-31\" width=\"584\" height=\"365\" title=\"h27\"><\/p>\n<p><span style=\"font-weight: 400\">B\u1ea1n c\u00f3 th\u1ec3 t\u1eadn d\u1ee5ng th\u00f4ng tin x\u1eed l\u00fd l\u1ed7i r\u1ea5t h\u1eefu \u00edch.\u00a0Th\u01b0\u1eddng th\u00ec ng\u01b0\u1eddi d\u00f9ng c\u00f3 th\u1ec3 kh\u00f4ng cung c\u1ea5p cho b\u1ea1n th\u00f4ng tin ch\u00ednh x\u00e1c v\u1ec1 l\u1ed7i \u0111\u00e3 x\u1ea3y ra.\u00a0B\u1eb1ng c\u00e1ch nh\u00ecn v\u00e0o nh\u1eadt k\u00fd, b\u1ea1n c\u00f3 th\u1ec3 nh\u1eadn \u0111\u01b0\u1ee3c th\u00f4ng tin ch\u00ednh x\u00e1c h\u01a1n v\u1ec1 th\u00f4ng tin.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"8_Cac_muc_lien_quan_den_loi_khac\"><\/span><b>8. C\u00e1c m\u1ee5c li\u00ean quan \u0111\u1ebfn l\u1ed7i kh\u00e1c<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">Ph\u1ea7n n\u00e0y bao g\u1ed3m m\u1ed9t s\u1ed1 c\u00f4ng c\u1ee5 X\u1eed l\u00fd L\u1ed7i kh\u00e1c m\u00e0 VBA c\u00f3. Nh\u1eefng m\u1ee5c n\u00e0y hi\u1ec7n nay r\u1ea5t \u00edt s\u1eed d\u1ee5ng nh\u01b0ng v\u1eabn c\u00f3 nh\u1eefng th\u1ebf m\u1ea1nh nh\u1ea5t \u0111\u1ecbnh.<\/span><\/p>\n<h3><span class=\"ez-toc-section\" id=\"81_Ham_Error\"><\/span><b>8.1 H\u00e0m Error<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400\">H\u00e0m Error \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 m\u00f4 t\u1ea3 l\u1ed7i t\u1eeb m\u1ed9t s\u1ed1 l\u1ed7i c\u1ee5 th\u1ec3. N\u00f3 \u0111\u01b0\u1ee3c bao g\u1ed3m trong VBA v\u00e0 kh\u00f4ng c\u1ea7n thi\u1ebft v\u00ec b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng Err.Description \u0111\u1ec3 thay th\u1ebf.<\/span><\/p>\n<p><span style=\"font-weight: 400\">D\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t s\u1ed1 v\u00ed d\u1ee5:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33676\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h28.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-32\" width=\"577\" height=\"133\" title=\"h28\"><\/p>\n<h3><span class=\"ez-toc-section\" id=\"82_Lenh_Error\"><\/span><b>8.2 L\u1ec7nh Error<\/b><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><span style=\"font-weight: 400\">C\u00e2u l\u1ec7nh Error cho ph\u00e9p b\u1ea1n m\u00f4 ph\u1ecfng m\u1ed9t l\u1ed7i.\u00a0N\u00f3 \u0111\u01b0\u1ee3c bao g\u1ed3m trong VBA\u00a0b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng\u00a0<\/span><b>Err.Raise<\/b><span style=\"font-weight: 400\">\u00a0\u0111\u1ec3 thay th\u1ebf.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Trong \u0111o\u1ea1n m\u00e3 sau, ch\u00fang ta m\u00f4 ph\u1ecfng l\u1ed7i \u201cDivide by 0\u201d.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33677\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h29.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-33\" width=\"574\" height=\"253\" title=\"h29\"><\/p>\n<p><span style=\"font-weight: 400\">C\u00e2u l\u1ec7nh n\u00e0y \u0111\u01b0\u1ee3c bao g\u1ed3m trong VBA \u0111\u1ec3 t\u01b0\u01a1ng th\u00edch ng\u01b0\u1ee3c (backward compatibility).\u00a0B\u1ea1n n\u00ean s\u1eed d\u1ee5ng Err.Raise \u0111\u1ec3 thay th\u1ebf.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"9_Mot_so_chien_luoc_xu_ly_loi_don_gian\"><\/span><b>9. M\u1ed9t s\u1ed1 chi\u1ebfn l\u01b0\u1ee3c x\u1eed l\u00fd l\u1ed7i \u0111\u01a1n gi\u1ea3n<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">B\u1ea1n c\u00f3 th\u1ec3 nh\u1ea7m l\u1eabn v\u1ec1 c\u00e1ch s\u1eed d\u1ee5ng x\u1eed l\u00fd l\u1ed7i trong VBA khi c\u00f3 qu\u00e1 nhi\u1ec1u l\u1ef1a ch\u1ecdn kh\u00e1c nhau.\u00a0Trong ph\u1ea7n n\u00e0y s\u1ebd h\u01b0\u1edbng d\u1eabn cho b\u1ea1n c\u00e1ch th\u1ef1c hi\u1ec7n m\u1ed9t chi\u1ebfn l\u01b0\u1ee3c x\u1eed l\u00fd l\u1ed7i \u0111\u01a1n gi\u1ea3n m\u00e0 b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng trong t\u1ea5t c\u1ea3 c\u00e1c \u1ee9ng d\u1ee5ng c\u1ee7a m\u00ecnh.<\/span><\/p>\n<p><b><i>Tri\u1ec3n khai c\u01a1 b\u1ea3n<\/i><\/b><\/p>\n<p><span style=\"font-weight: 400\">\u0110\u00e2y l\u00e0 t\u1ed5ng quan \u0111\u01a1n gi\u1ea3n v\u1ec1 chi\u1ebfn l\u01b0\u1ee3c nh\u01b0 sau:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">\u0110\u1eb7t d\u00f2ng\u00a0<\/span><i><span style=\"font-weight: 400\">On Error GoTo Label<\/span><\/i><span style=\"font-weight: 400\">\u00a0\u1edf Sub \u0111\u1ea7u trang tr\u00ean c\u00f9ng.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">\u0110\u1eb7t\u00a0<\/span><i><span style=\"font-weight: 400\">Label<\/span><\/i><span style=\"font-weight: 400\">\u00a0x\u1eed l\u00fd l\u1ed7i\u00a0\u1edf sub cu\u1ed1i trang d\u01b0\u1edbi c\u00f9ng.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">N\u1ebfu m\u1ed9t l\u1ed7i d\u1ef1 ki\u1ebfn \u200b\u200bx\u1ea3y ra th\u00ec h\u00e3y x\u1eed l\u00fd n\u00f3 v\u00e0 ti\u1ebfp t\u1ee5c.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">N\u1ebfu \u1ee9ng d\u1ee5ng kh\u00f4ng th\u1ec3 ti\u1ebfp t\u1ee5c th\u00ec h\u00e3y s\u1eed d\u1ee5ng Err.Raise \u0111\u1ec3 chuy\u1ec3n \u0111\u1ebfn Label x\u1eed l\u00fd l\u1ed7i.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">N\u1ebfu x\u1ea3y ra l\u1ed7i kh\u00f4ng mong mu\u1ed1n, m\u00e3 s\u1ebd t\u1ef1 \u0111\u1ed9ng chuy\u1ec3n \u0111\u1ebfn Label x\u1eed l\u00fd l\u1ed7i.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400\">H\u00ecnh \u1ea3nh sau \u0111\u00e2y cho th\u1ea5y t\u1ed5ng quan v\u1ec1 c\u00e1ch th\u1ee9c n\u00e0y<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33678\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h30.jpg\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-34\" width=\"630\" height=\"395\" title=\"h30\"><\/p>\n<p><span style=\"font-weight: 400\">\u0110o\u1ea1n m\u00e3 sau cho th\u1ea5y m\u1ed9t c\u00e1ch tri\u1ec3n khai \u0111\u01a1n gi\u1ea3n c\u1ee7a chi\u1ebfn l\u01b0\u1ee3c n\u00e0y:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33679\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h31.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-35\" width=\"362\" height=\"534\" title=\"h31\"><\/p>\n<p><span style=\"font-weight: 400\">\u0110\u00e2y l\u00e0 m\u1ed9t c\u00e1ch t\u1ed1t \u0111\u1ec3 tri\u1ec3n khai x\u1eed l\u00fd l\u1ed7i v\u00ec<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Ch\u00fang ta kh\u00f4ng c\u1ea7n th\u00eam m\u00e3 x\u1eed l\u00fd l\u1ed7i Sub.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">N\u1ebfu x\u1ea3y ra l\u1ed7i th\u00ec VBA s\u1ebd tho\u00e1t \u1ee9ng d\u1ee5ng.<\/span><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"10_Mot_chien_luoc_xu_ly_loi_hoan_chinh\"><\/span><b>10. M\u1ed9t chi\u1ebfn l\u01b0\u1ee3c x\u1eed l\u00fd l\u1ed7i ho\u00e0n ch\u1ec9nh<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">Chi\u1ebfn l\u01b0\u1ee3c tr\u00ean c\u00f3 m\u1ed9t nh\u01b0\u1ee3c \u0111i\u1ec3m l\u1edbn l\u00e0 n\u00f3 kh\u00f4ng cung c\u1ea5p b\u1ea5t k\u1ef3 th\u00f4ng tin n\u00e0o v\u1ec1 l\u1ed7i.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Trong ph\u1ea7n n\u00e0y s\u1ebd gi\u1edbi thi\u1ec7u m\u1ed9t chi\u1ebfn l\u01b0\u1ee3c x\u1eed l\u00fd l\u1ed7i ho\u00e0n ch\u1ec9nh h\u01a1n.\u00a0S\u1ebd c\u00f3 hai sub ph\u1ee5 h\u1ed7 tr\u1ee3 cho b\u1ea1n trong vi\u1ec7c x\u1eed l\u00fd l\u1ed7i.<\/span><\/p>\n<p><span style=\"font-weight: 400\">M\u1ee5c \u0111\u00edch c\u1ee7a chi\u1ebfn l\u01b0\u1ee3c n\u00e0y l\u00e0 cung c\u1ea5p cho b\u1ea1n: <\/span><i><span style=\"font-weight: 400\">Stack *<\/span><\/i><span style=\"font-weight: 400\">\u00a0v\u00e0 s\u1ed1 d\u00f2ng khi c\u00f3 l\u1ed7i.<\/span><\/p>\n<p><span style=\"font-weight: 400\">(*\u00a0<\/span><i><span style=\"font-weight: 400\">Stack <\/span><\/i><span style=\"font-weight: 400\">\u00a0l\u00e0 danh s\u00e1ch c\u00e1c ch\u1ee9c n\u0103ng con \/ ch\u1ee9c n\u0103ng hi\u1ec7n \u0111ang \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng khi x\u1ea3y ra l\u1ed7i.)<\/span><\/p>\n<p><span style=\"font-weight: 400\">Chi\u1ebfn l\u01b0\u1ee3c nh\u01b0 sau:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">\u0110\u1eb7t x\u1eed l\u00fd l\u1ed7i trong t\u1ea5t c\u1ea3 c\u00e1c sub.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Khi m\u1ed9t l\u1ed7i x\u1ea3y ra, tr\u00ecnh x\u1eed l\u00fd l\u1ed7i s\u1ebd th\u00eam c\u00e1c chi ti\u1ebft c\u1ee7a l\u1ed7i v\u00e0 ch\u1ea1y th\u00eam m\u1ed9t l\u1ea7n n\u1eefa.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Khi l\u1ed7i ti\u1ebfn \u0111\u1ebfn sub tr\u00ean c\u00f9ng, n\u00f3 s\u1ebd \u0111\u01b0\u1ee3c hi\u1ec3n th\u1ecb.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400\">\u0110i\u1ec1u n\u00e0y \u0111\u01a1n gi\u1ea3n l\u00e0 l\u00e0m n\u1ed5i\u00a0 l\u1ed7i l\u00ean \u0111\u1ea7u \u0111\u1ec3 d\u1ec5 th\u1ea5y.\u00a0S\u01a1 \u0111\u1ed3 sau \u0111\u00e2y cho th\u1ea5y m\u1ed9t h\u00ecnh \u1ea3nh tr\u1ef1c quan \u0111\u01a1n gi\u1ea3n v\u1ec1 nh\u1eefng g\u00ec s\u1ebd x\u1ea3y ra khi m\u1ed9t l\u1ed7i x\u1ea3y ra trong\u00a0<\/span><i><span style=\"font-weight: 400\">Sub3.<\/span><\/i><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33680\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h32.jpg\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-36\" width=\"164\" height=\"264\" title=\"h32\"><\/p>\n<p><span style=\"font-weight: 400\">Ph\u1ea7n ph\u1ee9c t\u1ea1p duy nh\u1ea5t c\u1ee7a vi\u1ec7c n\u00e0y l\u00e0 \u0111\u1ecbnh d\u1ea1ng c\u00e1c chu\u1ed7i m\u1ed9t c\u00e1ch ch\u00ednh x\u00e1c.\u00a0c\u00f3 2 sub s\u1ebd gi\u00fap b\u1ea1n x\u1eed l\u00fd \u0111i\u1ec1u n\u00e0y l\u00e0 \u00a0<\/span><b>Raiserror<\/b><span style=\"font-weight: 400\">\u00a0v\u00e0\u00a0<\/span><b>DisplayError<\/b><span style=\"font-weight: 400\">.<\/span><\/p>\n<p><i><span style=\"font-weight: 400\">V\u00ed d\u1ee5 v\u1ec1 vi\u1ec7c s\u1eed d\u1ee5ng chi\u1ebfn l\u01b0\u1ee3c n\u00e0y<\/span><\/i><\/p>\n<p><span style=\"font-weight: 400\">\u0110\u00e2y l\u00e0 m\u1ed9t v\u00ed d\u1ee5 m\u00e3 h\u00f3a \u0111\u01a1n gi\u1ea3n s\u1eed d\u1ee5ng c\u00e1c sub.\u00a0Trong chi\u1ebfn l\u01b0\u1ee3c n\u00e0y, ch\u00fang ta kh\u00f4ng \u0111\u1eb7t b\u1ea5t k\u1ef3 m\u00e3 n\u00e0o trong m\u00e3 ph\u1ee5 tr\u00ean c\u00f9ng.\u00a0Ch\u00fang ta g\u1ecdi sub t\u1eeb n\u00f3.<\/span><\/p>\n<p><span style=\"font-weight: 400\">K\u1ebft qu\u1ea3<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33681\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h33.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-37\" width=\"210\" height=\"232\" title=\"h33\"><\/p>\n<p><span style=\"font-weight: 400\">N\u1ebfu d\u1ef1 \u00e1n c\u1ee7a b\u1ea1n c\u00f3 s\u1ed1 d\u00f2ng, k\u1ebft qu\u1ea3 s\u1ebd bao g\u1ed3m s\u1ed1 d\u00f2ng c\u1ee7a l\u1ed7i:<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-33682\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/h34.png\" alt=\"huong-dan-xu-ly-loi-VBA-day-du-38\" width=\"210\" height=\"248\" title=\"h34\"><\/p>\n<h2><span class=\"ez-toc-section\" id=\"11_Xu_ly_loi_trong_Nutshell\"><\/span><b>11. X\u1eed l\u00fd l\u1ed7i trong Nutshell<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">X\u1eed l\u00fd L\u1ed7i \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c l\u1ed7i x\u1ea3y ra khi \u1ee9ng d\u1ee5ng c\u1ee7a b\u1ea1n \u0111ang ch\u1ea1y.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">B\u1ea1n vi\u1ebft m\u00e3 c\u1ee5 th\u1ec3 \u0111\u1ec3 x\u1eed l\u00fd c\u00e1c l\u1ed7i mong \u0111\u1ee3i.\u00a0B\u1ea1n s\u1eed d\u1ee5ng c\u00e2u l\u1ec7nh x\u1eed l\u00fd l\u1ed7i VBA\u00a0<\/span><i><span style=\"font-weight: 400\">On error GoTo [Label]<\/span><\/i><span style=\"font-weight: 400\">\u00a0\u0111\u1ec3 g\u1eedi VBA \u0111\u1ebfn m\u1ed9t Label khi x\u1ea3y ra l\u1ed7i kh\u00f4ng mong mu\u1ed1n.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">B\u1ea1n c\u00f3 th\u1ec3 l\u1ea5y th\u00f4ng tin chi ti\u1ebft v\u1ec1 l\u1ed7i t\u1eeb\u00a0<\/span><i><span style=\"font-weight: 400\">Err.Description<\/span><\/i><span style=\"font-weight: 400\">\u00a0.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">B\u1ea1n c\u00f3 th\u1ec3 t\u1ea1o l\u1ed7i c\u1ee7a ri\u00eang m\u00ecnh b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng\u00a0<\/span><i><span style=\"font-weight: 400\">Err.Raise<\/span><\/i><span style=\"font-weight: 400\">\u00a0.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">S\u1eed d\u1ee5ng m\u1ed9t\u00a0c\u00e2u l\u1ec7nh\u00a0<\/span><i><span style=\"font-weight: 400\">On Error<\/span><\/i><span style=\"font-weight: 400\">\u00a0\u1edf sub \u0111\u1ea7u s\u1ebd b\u1eaft \u0111\u01b0\u1ee3c t\u1ea5t c\u1ea3 c\u00e1c l\u1ed7i trong ph\u1ea7n sub \u0111\u01b0\u1ee3c g\u1ecdi t\u1eeb \u0111\u00e2y.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">N\u1ebfu mu\u1ed1n ghi t\u00ean Sub b\u1ecb l\u1ed7i, b\u1ea1n c\u00f3 th\u1ec3 c\u1eadp nh\u1eadt l\u1ed7i v\u00e0 ch\u1ea1y l\u1ea1i.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng logging \u0111\u1ec3 ghi l\u1ea1i th\u00f4ng tin v\u1ec1 \u1ee9ng d\u1ee5ng khi \u1ee9ng d\u1ee5ng \u0111ang ch\u1ea1y.<\/span><\/li>\n<\/ul>\n<p>V\u00e0 \u0111\u00f3 l\u00e0 to\u00e0n b\u1ed9 nh\u1eefng l\u1ed7i VBA th\u01b0\u1eddng g\u1eb7p c\u00f9ng v\u1edbi c\u00e1ch gi\u1ea3i quy\u1ebft ch\u00fang m\u00e0 Blog H\u1ecdc Excel Online g\u1eedi t\u1edbi c\u00e1c b\u1ea1n \u0111\u1ec3 gi\u00fap vi\u1ec7c vi\u1ebft c\u00e1c macro c\u0169ng nh\u01b0 t\u1ef1 \u0111\u1ed9ng h\u00f3a t\u00e1c v\u1ee5 di\u1ec5n ra tr\u01a1n tru v\u00e0 hi\u1ec7u qu\u1ea3. Theo d\u00f5i to\u00e0n b\u1ed9<a href=\"http:\/\/blog.ezworkapp.com\/excel-vba\"> kh\u00f3a h\u1ecdc Online VBA<\/a> c\u1ee7a ch\u00fang t\u00f4i \u0111\u1ec3 Excel tr\u1edf n\u00ean d\u1ec5 d\u00e0ng.<\/p>\n<p>\u00a0<\/p>\n<\/div>\n<div class=\"uk-panel uk-margin uk-width-xlarge\">\n<p>Click \u0111\u0103ng k\u00fd h\u1ecdc t\u1ea1i:<\/p>\n<p><a href=\"http:\/\/blog.ezworkapp.com\/khoa-hoc\" target=\"_blank\" rel=\"noopener\">T\u1ea5t c\u1ea3 kho\u00e1 h\u1ecdc\u00a0<span>\ud83d\udcd6<\/span><\/a><\/p>\n<p><a href=\"http:\/\/blog.ezworkapp.com\/huong-dan-kich-hoat-khoa-hoc-tai-hoc-excel-online.html\" target=\"_blank\" rel=\"noopener\">H\u01b0\u1edbng d\u1eabn k\u00edch ho\u1ea1t<\/a><\/p>\n<p>HKD H\u1ecdc Excel Online<br \/>S\u1ed1 \u0110K: 17A80048102<\/p>\n<\/div>\n<div class=\"uk-panel uk-text-small uk-margin uk-margin-remove-bottom uk-text-center\">\n<p>\u00a9  document.currentScript.insertAdjacentHTML(&#8216;afterend&#8217;, &#8221; + new Intl.DateTimeFormat(document.documentElement.lang, {year: &#8216;numeric&#8217;}).format() + &#8221;);  H\u1ecdc Excel Online. All rights reserved.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<div class=\"ad-inserterpro\">\n<a href=\"https:\/\/hocexcel.online\/course\/preview\/vba101-tu-dong-hoa-excel-voi-lap-trinh-vba-cho-nguoi-moi-bat-dau\"><br \/>\n<img decoding=\"async\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/vba101_92019-nho-65.gif\"><br \/>\n<\/a>\n<\/div>\n","protected":false},"author":1,"featured_media":2068,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-2067","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-excel-nang-cao"],"_links":{"self":[{"href":"https:\/\/blog.ezworkapp.com\/index.php\/wp-json\/wp\/v2\/posts\/2067","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ezworkapp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ezworkapp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ezworkapp.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ezworkapp.com\/index.php\/wp-json\/wp\/v2\/comments?post=2067"}],"version-history":[{"count":0,"href":"https:\/\/blog.ezworkapp.com\/index.php\/wp-json\/wp\/v2\/posts\/2067\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ezworkapp.com\/index.php\/wp-json\/wp\/v2\/media\/2068"}],"wp:attachment":[{"href":"https:\/\/blog.ezworkapp.com\/index.php\/wp-json\/wp\/v2\/media?parent=2067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ezworkapp.com\/index.php\/wp-json\/wp\/v2\/categories?post=2067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ezworkapp.com\/index.php\/wp-json\/wp\/v2\/tags?post=2067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}