{"id":1836,"date":"2024-12-02T06:13:19","date_gmt":"2024-12-02T06:13:19","guid":{"rendered":"https:\/\/blog.ezworkapp.com\/index.php\/2024\/12\/02\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba\/"},"modified":"2024-12-02T06:13:19","modified_gmt":"2024-12-02T06:13:19","slug":"huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba","status":"publish","type":"post","link":"https:\/\/blog.ezworkapp.com\/index.php\/2024\/12\/02\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba\/","title":{"rendered":"H\u01af\u1edaNG D\u1eaaN HO\u00c0N CH\u1ec8NH V\u1ec0 \u0110\u1ed0I T\u01af\u1ee2NG WORKBOOK TRONG VBA"},"content":{"rendered":"<div class=\"uk-panel uk-margin\" id=\"template-dQf5b79p#1\">\n<p><em><span style=\"font-weight: 400\">C\u00e1ch s\u1eed d\u1ee5ng workbook trong VBA l\u00e0 nh\u01b0 th\u1ebf n\u00e0o? L\u1ee3i \u00edch c\u1ee7a vi\u1ec7c s\u1eed d\u1ee5ng workbook? Ngay sau \u0111\u00e2y,<a href=\"http:\/\/blog.ezworkapp.com\/\"><strong> H\u1ecdc Excel Online<\/strong><\/a> s\u1ebd cung c\u1ea5p h\u01b0\u1edbng d\u1eabn \u0111\u1ea7y \u0111\u1ee7 \u0111\u1ec3 s\u1eed d\u1ee5ng \u0111\u1ed1i t\u01b0\u1ee3ng<strong> workbook trong VBA<\/strong>.<\/span><b>\u00a0<\/b><\/em><\/p>\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-674d4ffe81c61\" 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-2\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Bang_huong_dan_nhanh_ve_workbook_trong_VBA\" title=\"B\u1ea3ng h\u01b0\u1edbng d\u1eabn nhanh v\u1ec1 workbook trong VBA\">B\u1ea3ng h\u01b0\u1edbng d\u1eabn nhanh v\u1ec1 workbook trong VBA<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Bat_dau_voi_doi_tuong_Workbook_trong_VBA\" title=\"B\u1eaft \u0111\u1ea7u v\u1edbi \u0111\u1ed1i t\u01b0\u1ee3ng Workbook trong VBA\">B\u1eaft \u0111\u1ea7u v\u1edbi \u0111\u1ed1i t\u01b0\u1ee3ng Workbook trong VBA<\/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-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Khac_phuc_su_co_bo_suu_tap_workbooks\" title=\"Kh\u1eafc ph\u1ee5c s\u1ef1 c\u1ed1 b\u1ed9 s\u01b0u t\u1eadp workbooks\">Kh\u1eafc ph\u1ee5c s\u1ef1 c\u1ed1 b\u1ed9 s\u01b0u t\u1eadp workbooks<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Cac_vi_du_su_dung_workbook_trong_VBA\" title=\"C\u00e1c v\u00ed d\u1ee5 s\u1eed d\u1ee5ng workbook trong VBA\">C\u00e1c v\u00ed d\u1ee5 s\u1eed d\u1ee5ng workbook trong VBA<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Truy_cap_Workbook_bang_index_chi_muc\" title=\"Truy c\u1eadp Workbook b\u1eb1ng index (ch\u1ec9 m\u1ee5c)\">Truy c\u1eadp Workbook b\u1eb1ng index (ch\u1ec9 m\u1ee5c)<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Truy_cap_tat_ca_cac_workbook_dang_mo\" title=\"Truy c\u1eadp t\u1ea5t c\u1ea3 c\u00e1c workbook \u0111ang m\u1edf\">Truy c\u1eadp t\u1ea5t c\u1ea3 c\u00e1c workbook \u0111ang m\u1edf<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Mo_workbook\" title=\"M\u1edf workbook\">M\u1edf workbook<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Kiem_tra_Open_workbook\" title=\"Ki\u1ec3m tra Open workbook\">Ki\u1ec3m tra Open workbook<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Close_workbook_dong_workbook\" title=\"Close workbook (\u0111\u00f3ng workbook)\">Close workbook (\u0111\u00f3ng workbook)<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Sao_chep_workbook\" title=\"Sao ch\u00e9p workbook\">Sao ch\u00e9p workbook<\/a><\/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-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Su_dung_hop_thoai_File_de_mo_Workbook\" title=\"S\u1eed d\u1ee5ng h\u1ed9p tho\u1ea1i File \u0111\u1ec3 m\u1edf Workbook\">S\u1eed d\u1ee5ng h\u1ed9p tho\u1ea1i File \u0111\u1ec3 m\u1edf Workbook<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Su_dung_Thisworkbook\" title=\"S\u1eed d\u1ee5ng Thisworkbook\">S\u1eed d\u1ee5ng Thisworkbook<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Su_dung_ActiveWorkbook\" title=\"S\u1eed d\u1ee5ng ActiveWorkbook\">S\u1eed d\u1ee5ng ActiveWorkbook<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Cac_vi_du_ve_truy_cap_workbook\" title=\"C\u00e1c v\u00ed d\u1ee5 v\u1ec1 truy c\u1eadp workbook\">C\u00e1c v\u00ed d\u1ee5 v\u1ec1 truy c\u1eadp workbook<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Khai_bao_bien_workbook\" title=\"Khai b\u00e1o bi\u1ebfn workbook\">Khai b\u00e1o bi\u1ebfn workbook<\/a><\/li>\n<li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/blog.hocexcel.online\/huong-dan-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Tao_mot_workbook_moi\" title=\"T\u1ea1o m\u1ed9t workbook m\u1edbi\">T\u1ea1o m\u1ed9t workbook m\u1edbi<\/a><\/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-hoan-chinh-ve-doi-tuong-workbook-trong-vba.html#Su_dung_WITH_voi_workbook\" title=\"S\u1eed d\u1ee5ng WITH v\u1edbi workbook\">S\u1eed d\u1ee5ng WITH v\u1edbi workbook<\/a><\/li>\n<\/ul>\n<\/nav>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Bang_huong_dan_nhanh_ve_workbook_trong_VBA\"><\/span><b>B\u1ea3ng h\u01b0\u1edbng d\u1eabn nhanh v\u1ec1 workbook trong VBA<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<table>\n<tbody>\n<tr>\n<td><b>T\u00e1c\u00a0 v\u1ee5<\/b><\/td>\n<td><b>C\u00e1ch th\u1ef1c hi\u1ec7n<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Truy c\u1eadp workbook \u0111ang m\u1edf b\u1eb1ng t\u00ean<\/span><\/td>\n<td><span style=\"font-weight: 400\">Workbooks(\u201cExample.xlsx\u201d)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Truy c\u1eadp workbook \u0111ang m\u1edf (s\u1ed5 \u0111\u01b0\u1ee3c m\u1edf \u0111\u1ea7u ti\u00ean)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Workbooks(1)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Truy c\u1eadp workbook \u0111ang m\u1edf (s\u1ed5 \u0111\u01b0\u1ee3c m\u1edf sau c\u00f9ng)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Workbooks(Workbooks.Count)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Truy c\u1eadp workbook \u0111ang ho\u1ea1t \u0111\u1ed9ng<\/span><\/td>\n<td><span style=\"font-weight: 400\">ActiveWorkbook<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Truy c\u1eadp workbook c\u00f3 ch\u1ee9a m\u00e3 VBA<\/span><\/td>\n<td><span style=\"font-weight: 400\">ThisWorkbook<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Khai b\u00e1o m\u1ed9t bi\u1ebfn workbook<\/span><\/td>\n<td><b>Dim wk As Workbook<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">G\u00e1n m\u1ed9t bi\u1ebfn workbook<\/span><\/td>\n<td><b>Set<\/b><span style=\"font-weight: 400\">\u00a0wk = Workbooks (\u00a0\u201cExample.xlsx\u201d\u00a0)<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><b>Set<\/b><span style=\"font-weight: 400\">\u00a0wk = ThisWorkbook<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><b>Set<\/b><span style=\"font-weight: 400\">\u00a0wk = Workbooks (1)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">K\u00edch ho\u1ea1t workbook<\/span><\/td>\n<td><span style=\"font-weight: 400\">wk.Activate<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\u0110\u00f3ng workbook m\u00e0 kh\u00f4ng l\u01b0u<\/span><\/td>\n<td><span style=\"font-weight: 400\">wk.C Close SaveChanges: =\u00a0<\/span><b>False<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\u0110\u00f3ng workbook v\u00e0 l\u01b0u<\/span><\/td>\n<td><span style=\"font-weight: 400\">wk.C Close SaveChanges: =\u00a0<\/span><b>True<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">T\u1ea1o workbook m\u1edbi<\/span><\/td>\n<td><b>Set <\/b><span style=\"font-weight: 400\">wk = Workbooks.Add<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">M\u1edf workbook<\/span><\/td>\n<td><b>Set<\/b><span style=\"font-weight: 400\">\u00a0wk = Workbooks.Open\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">(\u00a0\u201cC:Docs Example.xlsx\u201d\u00a0)<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">M\u1edf workbook \u1edf d\u1ea1ng ch\u1ec9 \u0111\u1ecdc<\/span><\/td>\n<td><span style=\"font-weight: 400\">Setwk<\/span><span style=\"font-weight: 400\">=Workbooks.Open (\u201cC:DocsExample.xlsx\u201d, ReadOnly:=True)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Ki\u1ec3m tra workbook t\u1ed3n t\u1ea1i<\/span><\/td>\n<td><span style=\"font-weight: 400\">If Dir(\u201cC:Docsbook1.xlsx\u201d) = \u201c\u201d Then<\/span><\/p>\n<p><span style=\"font-weight: 400\">MsgBox \u201cFile does not exist.\u201d<\/span><\/p>\n<p><span style=\"font-weight: 400\">EndIf<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Ki\u1ec3m tra workbook \u0111ang m\u1edf<\/span><\/td>\n<td><span style=\"font-weight: 400\">Xem ph\u1ea7n b\u00ean d\u01b0\u1edbi trong b\u00e0i<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Li\u1ec7t k\u00ea t\u1ea5t c\u1ea3 c\u00e1c workbook \u0111ang m\u1edf<\/span><\/td>\n<td><b>For Each wk In <\/b><span style=\"font-weight: 400\">Application.Workbooks<\/span><\/p>\n<p><b>\u00a0\u00a0\u00a0\u00a0Debug.Print wk.<\/b><span style=\"font-weight: 400\">FullName<\/span><\/p>\n<p><b>Next <\/b><span style=\"font-weight: 400\">wk<\/span><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">M\u1edf workbook b\u1eb1ng H\u1ed9p tho\u1ea1i T\u1ec7p<\/span><\/td>\n<td><span style=\"font-weight: 400\">Xem ph\u1ea7n n\u00e0y \u1edf n\u1ed9i dung b\u00ean d\u01b0\u1edbi<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">L\u01b0u workbook<\/span><\/td>\n<td><span style=\"font-weight: 400\">wk.Save<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">L\u01b0u b\u1ea3n sao workbook<\/span><\/td>\n<td><span style=\"font-weight: 400\">wk.SaveCopyAs\u00a0\u201cC:Copy.xlsm\u201d<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Sao ch\u00e9p workbook n\u1ebfu \u0111\u00e3 \u0111\u00f3ng<\/span><\/td>\n<td><span style=\"font-weight: 400\">FileCopy\u00a0\u201cC:file1.xlsx\u201d\u00a0,\u00a0\u201cC:Copy.xlsx\u201d<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">SaveAs Workbook<\/span><\/td>\n<td><span style=\"font-weight: 400\">wk.SaveAs\u00a0\u201cBackup.xlsx\u201d<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span class=\"ez-toc-section\" id=\"Bat_dau_voi_doi_tuong_Workbook_trong_VBA\"><\/span><b>B\u1eaft \u0111\u1ea7u v\u1edbi \u0111\u1ed1i t\u01b0\u1ee3ng Workbook trong VBA<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">Ch\u00fang ta c\u00f3 th\u1ec3 truy c\u1eadp b\u1ea5t k\u1ef3 workbook b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng m\u00e3 Workbooks (\u201cExample.xlsm\u201d), ch\u1ec9 c\u1ea7n thay th\u1ebf Example.xlsm b\u1eb1ng t\u00ean c\u1ee7a workbook m\u00e0 b\u1ea1n mu\u1ed1n s\u1eed d\u1ee5ng.<\/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-55.gif\" title=\"vba101_92019-nho-55\"><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-133.gif\" title=\"ex101_92019-nho-133\"><br \/>\n<\/a>\n<\/div>\n<\/div>\n<p><span style=\"font-weight: 400\">V\u00ed d\u1ee5 sau \u0111\u00e2y h\u01b0\u1edbng d\u1eabn c\u00e1ch ghi d\u1eef li\u1ec7u <a href=\"http:\/\/blog.ezworkapp.com\/huong-dan-cach-viet-code-voi-doi-tuong-worksheet-trong-vba-excel.html\"><strong>worksheet trong VBA<\/strong><\/a>, ch\u00fang ta ph\u1ea3i ch\u1ec9 \u0111\u1ecbnh workbook, worksheet v\u00e0 v\u00f9ng d\u1eef li\u1ec7u.<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Sub WriteToA1()\r\n\r\n    ' Writes 100 to cell A1 of worksheet \"Sheet1\" in MyVBA.xlsm\r\nWorkbooks(\"MyVBA.xlsm\").Worksheets(\"Sheet1\").Range(\"A1\") = 100\r\n\r\nEnd Sub<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">Ph\u1ea7n \u0111\u1ea7u ti\u00ean l\u00e0 workbook, ph\u1ea7n th\u1ee9 hai l\u00e0 worksheet v\u00e0 ph\u1ea7n th\u1ee9 3 l\u00e0 range (v\u00f9ng d\u1eef li\u1ec7u). D\u01b0\u1edbi \u0111\u00e2y l\u00e0 v\u00ed d\u1ee5 kh\u00e1c c\u00e1ch ghi v\u00e0o \u00f4:<\/span><\/p>\n<p><span style=\"font-weight: 400\">Ch\u00fang ta c\u00f3 th\u1ec3 ghi v\u00e0o b\u1ea5t k\u1ef3 \u00f4 n\u00e0o trong b\u1ea5t k\u1ef3<strong> worksheet t\u1eeb workbook<\/strong>. B\u1ea1n ch\u1ec9 c\u1ea7n thay \u0111\u1ed5i t\u00ean workbook, t\u00ean worksheet v\u00e0 v\u00f9ng d\u1eef li\u1ec7u ph\u00f9 h\u1ee3p v\u1edbi y\u00eau c\u1ea7u c\u1ee7a m\u00ecnh,<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Sub WriteToMulti()\r\n\r\n' Writes 100 to cell A1 of worksheet \"Sheet1\" in MyVBA.xlsm\r\nWorkbooks(\"MyVBA.xlsm\").Worksheets(\"Sheet1\").Range(\"A1\") = 100\r\n\r\n' Writes \"John\" to cell B1 of worksheet \"Sheet1\" in MyVBA.xlsm\r\nWorkbooks(\"MyVBA.xlsm\").Worksheets(\"Sheet1\").Range(\"B1\") = \"John\"\r\n\r\n' Writes 100 to cell A1 of worksheet \"Accounts\" in MyVBA.xlsm\r\nWorkbooks(\"MyVBA.xlsm\").Worksheets(\"Accounts\").Range(\"A1\") = 100\r\n\r\n' Writes the date to cell D3 of worksheet \"Sheet2\" in Book.xlsc\r\nWorkbooks(\"Book.xlsx\").Worksheets(\"Sheet2\").Range(\"D3\") = \"112016\"\r\n\r\nEnd Sub<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">Xem x\u00e9t workbook sau \u0111\u00e2y:<\/span><\/p>\n<pre><strong>Workbooks(\"Example.xlsx\")<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">Workbooks l\u00e0 b\u1ed9 s\u01b0u t\u1eadp t\u1ea5t c\u1ea3 c\u00e1c workbook. \u0110\u1ec3 truy c\u1eadp v\u00e0o workbook b\u1ea1n ph\u1ea3i cung c\u1ea5p t\u00ean workbook cho b\u1ed9 s\u01b0u t\u1eadp workbooks. Khi x\u00e1c \u0111\u1ecbnh \u0111\u1ed1i t\u01b0\u1ee3ng ch\u00fang ta s\u1eed d\u1ee5ng n\u00f3 \u0111\u1ec3 th\u1ef1c hi\u1ec7n c\u00e1c t\u00e1c v\u1ee5 v\u1edbi workbook.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Khac_phuc_su_co_bo_suu_tap_workbooks\"><\/span><b>Kh\u1eafc ph\u1ee5c s\u1ef1 c\u1ed1 b\u1ed9 s\u01b0u t\u1eadp workbooks<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">Khi b\u1ea1n s\u1eed d\u1ee5ng b\u1ed9 s\u01b0u t\u1eadp workbooks \u0111\u1ec3 truy c\u1eadp workbook c\u00f3 th\u1ec3 s\u1ebd xu\u1ea5t hi\u1ec7n l\u1ed7i.<\/span><\/p>\n<p><b>Run-time Error 9: Subscript out of Range.<\/b><\/p>\n<p><span style=\"font-weight: 400\">\u0110i\u1ec1u n\u00e0y c\u00f3 ngh\u0129a VBA kh\u00f4ng t\u00ecm th\u1ea5y workbook b\u1ea1n \u0111\u00e3 truy\u1ec1n d\u01b0\u1edbi d\u1ea1ng tham s\u1ed1. C\u00f3 th\u1ec3 v\u00ec c\u00e1c l\u00fd do sau:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Workbook c\u00f3 v\u1ea5n \u0111\u1ec1.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">T\u00ean kh\u00f4ng \u0111\u00fang.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">B\u1ea1n t\u1ea1o workbook l\u00e0m vi\u1ec7c m\u1edbi (v\u00ed d\u1ee5: Book1) nh\u01b0ng qu\u00ean l\u01b0u t\u00ean v\u00e0 sau \u0111\u00f3 c\u1ed1 g\u1eafng truy c\u1eadp n\u00f3 b\u1eb1ng workbooks.(\u201cBook1.xlsx\u201d) .<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">(D\u00e0nh cho Excel 2007\/2010). N\u1ebfu b\u1ea1n ch\u1ea1y hai phi\u00ean b\u1ea3n Excel th\u00ec Workbooks() ch\u1ec9 tham chi\u1ebfu \u0111\u1ebfn c\u00e1c workbook \u0111ang m\u1edf hi\u1ec7n t\u1ea1i.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">B\u1ea1n truy\u1ec1n m\u1ed9t s\u1ed1 \u0111\u1ec3 l\u00e0m Index(ch\u1ec9 m\u1ee5c) cho c\u00e1c workbook v\u00e0 n\u00f3 l\u1edbn h\u01a1n s\u1ed1 l\u01b0\u1ee3ng workbook \u0111ang m\u1edf, v\u00ed d\u1ee5: b\u1ea1n s\u1eed d\u1ee5ng workbook(3) nh\u01b0ng th\u1ef1c t\u1ebf ch\u1ec9 c\u00f3 2 workbook \u0111ang m\u1edf.\u00a0<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400\">N\u1ebfu kh\u00f4ng th\u1ec3 x\u1eed l\u00fd l\u1ed7i h\u00e3y s\u1eed d\u1ee5ng c\u00e1c ch\u1ee9c n\u0103ng trong ph\u1ea7n \u201cFinding all open workbooks\u201d ( t\u00ecm t\u1ea5t workbook trong b\u1ed9 s\u01b0u t\u1eadp workbooks) v\u00e0 ch\u00fang s\u1ebd xu\u1ea5t t\u00ean c\u1ee7a t\u1ea5t c\u1ea3 c\u00e1c workbook \u0111ang m\u1edf v\u00e0o Immediate window (Ctrl+G).<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Cac_vi_du_su_dung_workbook_trong_VBA\"><\/span><b>C\u00e1c v\u00ed d\u1ee5 s\u1eed d\u1ee5ng workbook trong VBA<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">L\u01b0u \u00fd: \u0111\u1ec3 th\u1eed v\u00ed d\u1ee5 n\u00e0y, ch\u00fang ta s\u1ebd t\u1ea1o hai workbook c\u00f3 t\u00ean l\u00e0 Test.xlsx v\u00e0 Test2.xlsx.<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Sub WorkbookProperties()\r\n\r\n    ' Prints the number of open workbooks\r\n    Debug.Print Workbooks.Count\r\n\r\n    ' Prints the full workbook name\r\n    Debug.Print Workbooks(\"Test1.xlsx\").FullName\r\n\r\n    ' Displays the full workbook name in a message dialog\r\n    MsgBox Workbooks(\"Test1.xlsx\").FullName\r\n\r\n    ' Prints the number of worksheets in Test2.xlsx\r\n    Debug.Print Workbooks(\"Test2.xlsx\").Worksheets.Count\r\n\r\n    ' Prints the name of currently active sheet of Test2.xlsx\r\n    Debug.Print Workbooks(\"Test2.xlsx\").ActiveSheet.Name\r\n\r\n    ' Closes workbook called Test1.xlsx\r\n    Workbooks(\"Test1.xlsx\").Close\r\n\r\n    ' Closes workbook Test2.xlsx and saves changes\r\n    Workbooks(\"Test2.xlsx\").Close SaveChanges:=True\r\n\r\nEnd Sub<\/strong><\/pre>\n<p><span style=\"font-weight: 400\"><strong>Ch\u00fa \u00fd:<\/strong> trong v\u00ed d\u1ee5 tr\u00ean Debug.Print s\u1eed d\u1ee5ng t\u01b0\u01a1ng \u0111\u1ed1i nhi\u1ec1u. H\u00e0m n\u00e0y s\u1ebd xu\u1ea5t c\u00e1c gi\u00e1 tr\u1ecb v\u00e0o Immediate window. \u0110\u1ec3 xem Immediate window ch\u1ecdn View\u2013&gt;Immediate window t\u1eeb menu (ph\u00edm t\u1eaft l\u00e0 Ctrl+G).<\/span><\/p>\n<figure id=\"attachment_34232\" aria-describedby=\"caption-attachment-34232\" style=\"width: 528px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-34232\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/1-immediatewindow2.jpg\" alt=\"1-ImmediateWindow2\" width=\"528\" height=\"440\" title=\"1-immediatewindow2\"><figcaption id=\"caption-attachment-34232\" class=\"wp-caption-text\">1-ImmediateWindow2<\/figcaption><\/figure>\n<figure id=\"attachment_34231\" aria-describedby=\"caption-attachment-34231\" style=\"width: 714px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-34231\" src=\"http:\/\/blog.ezworkapp.com\/wp-content\/uploads\/2024\/12\/2-immediatewindow2.jpg\" alt=\"2-ImmediateWindow2\" width=\"714\" height=\"491\" title=\"2-immediatewindow2\"><figcaption id=\"caption-attachment-34231\" class=\"wp-caption-text\">2-ImmediateWindow2<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Truy_cap_Workbook_bang_index_chi_muc\"><\/span><b>Truy c\u1eadp Workbook b\u1eb1ng index (ch\u1ec9 m\u1ee5c)<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">Ch\u00fang c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng s\u1ed1 Index v\u1edbi worbooks(). Index \u0111\u1ec1 c\u1eadp \u0111\u1ebfn s\u1ed1 th\u1ee9 t\u1ef1 workbook.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Workbook (1) \u0111\u1ec1 c\u1eadp \u0111\u1ebfn workbook l\u00e0m vi\u1ec7c d\u01b0\u1ee3c m\u1edf \u0111\u1ea7u ti\u00ean, workbooks (2) \u0111\u1ec1 c\u1eadp \u0111\u1ebfn workbook \u0111\u01b0\u1ee3c m\u1edf th\u1ee9 2 v\u00e0 ti\u1ebfp t\u1ee5c nh\u01b0 v\u1eady.<\/span><\/p>\n<pre><strong>' First workbook that was opened\r\nDebug.Print Workbooks(1).Name\r\n\r\n' Third workbook that was opened\r\nDebug.Print Workbooks(3).Name\r\n\r\n' The last workbook that was opened\r\nDebug.Print Workbooks(Workbooks.Count).Name<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">Trong v\u00ed d\u1ee5 n\u00e0y ch\u00fang ta s\u1eed d\u1ee5ng workbooks.count, c\u00e1i n\u00e0y c\u00f3 ngh\u0129a l\u00e0 s\u1ed1 l\u01b0\u1ee3ng workbook hi\u1ec7n c\u00f3 trong b\u1ed9 s\u01b0u t\u1eadp workbooks. S\u1eed d\u1ee5ng Index kh\u00f4ng th\u1ef1c s\u1ef1 h\u1eefu \u00edch tr\u1eeb khi b\u1ea1n c\u1ea7n \u0111\u1ebfn th\u1ee9 t\u1ef1. Thay v\u00e0o \u0111\u00f3, s\u1eed d\u1ee5ng workbook v\u1edbi workbooks() s\u1ebd hi\u1ec7u qu\u1ea3 h\u01a1n.\u00a0<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Truy_cap_tat_ca_cac_workbook_dang_mo\"><\/span><b>Truy c\u1eadp t\u1ea5t c\u1ea3 c\u00e1c workbook \u0111ang m\u1edf<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">N\u1ebfu b\u1ea1n mu\u1ed1n truy c\u1eadp t\u1ea5t c\u1ea3 c\u00e1c workbook \u0111ang m\u1edf hay n\u00f3i c\u00e1ch kh\u00e1c c\u00e1c m\u1ee5c trong b\u1ed9 s\u01b0u t\u1eadp workbooks()th\u00ec c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng For each Loop.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Ch\u00fang ta c\u0169ng c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng For Loop:<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Sub PrintWrkFileName()\r\n\r\n    ' Prints out the full filename of all open workbooks\r\n    Dim wrk As Workbook\r\n    For Each wrk In Workbooks\r\n        Debug.Print wrk.FullName\r\n    Next wrk\r\n\r\nEnd Sub<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">\u0110\u1ec3 truy c\u1eadp t\u1ea5t c\u1ea3 Workbook th\u00ec c\u00f3 th\u1ec3 ch\u1ecdn m\u1ed9t trong hai v\u00f2ng l\u1eb7p tr\u00ean. For Loop s\u1ebd h\u1eefu \u00edch n\u1ebfu b\u1ea1n mu\u1ed1n s\u1eed d\u1ee5ng th\u1ee9 t\u1ef1 ho\u1eb7c b\u1ed9 \u0111\u1ebfm.<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Sub PrintWrkFileNameIdx()\r\n\r\n    ' Prints out the full filename of all open workbooks\r\n    Dim i As Long\r\n    For i = 1 To Workbooks.Count\r\n        Debug.Print Workbooks(i).FullName\r\n    Next i\r\n\r\nEnd Sub<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">L\u01b0u \u00fd: c\u1ea3 hai v\u00ed d\u1ee5 \u0111\u1ec1u \u0111\u1ecdc theo th\u1ee9 t\u1eeb t\u1eeb l\u1ea7n truy c\u1eadp \u0111\u1ea7u ti\u00ean \u0111\u1ebfn l\u1ea7n truy c\u1eadp cu\u1ed1i c\u00f9ng. N\u1ebfu mu\u1ed1n \u0111\u1ecdc theo th\u1ee9 t\u1ef1 ng\u01b0\u1ee3c l\u1ea1i th\u00ec l\u00e0m nh\u01b0 sau:<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Sub PrintWrkFileNameIdxRev()\r\n\r\n    ' Prints out the full filename of all open workbooks\r\n    ' in reverse order.\r\n    Dim i As Long\r\n    For i = Workbooks.Count To 1 Step -1\r\n        Debug.Print Workbooks(i).FullName\r\n    Next i\r\n\r\nEnd Sub<\/strong><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Mo_workbook\"><\/span><b>M\u1edf workbook<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">Ch\u00fang ta c\u00f3 th\u1ec3 m\u1edf workbook b\u1eb1ng c\u00e1c c\u00e1ch th\u1ee7 c\u00f4ng nh\u01b0 tr\u00ean \u0111\u00e2y tr\u01b0\u1edbc khi ch\u1ea1y Macro nh\u01b0ng s\u1ebd h\u1eefu \u00edch h\u01a1n n\u00eau m\u1edf workbook m\u1ed9t c\u00e1ch t\u1ef1 \u0111\u1ed9ng thao t\u00e1c b\u1eb1ng VBA.<\/span><\/p>\n<p><span style=\"font-weight: 400\">M\u00e3 VBA s\u1ebd m\u1edf Workbook <\/span><i><span style=\"font-weight: 400\">\u201cBook1.xlsm\u201d<\/span><\/i><span style=\"font-weight: 400\">\u00a0trong\u00a0th\u01b0 m\u1ee5c\u00a0<\/span><i><span style=\"font-weight: 400\">\u201cC:  Docs\u201d<\/span><\/i><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Sub OpenWrk()\r\n\r\n    ' Open the workbook and print the number of sheets it contains\r\n    Workbooks.Open (\"C:DocsBook1.xlsm\")\r\n\r\n    Debug.Print Workbooks(\"Book1.xlsm\").Worksheets.Count\r\n\r\n    ' Close the workbook without saving\r\n    Workbooks(\"Book1.xlsm\").Close saveChanges:=False\r\n\r\nEnd Sub<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">B\u1ea1n s\u1ebd ki\u1ec3m tra workbook c\u00f3 t\u1ed3n t\u1ea1i hay kh\u00f4ng, v\u00ec v\u1eady gi\u00fap b\u1ea1n tr\u00e1nh \u0111\u01b0\u1ee3c l\u1ed7i. C\u00e1c h\u00e0m Dir h\u1ed7 tr\u1ee3 b\u1ea1n th\u1ef1c hi\u1ec7n \u0111i\u1ec1u n\u00e0y:<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Sub OpenWrkDir()\r\n\r\n    If Dir(\"C:DocsBook1.xlsm\") = \"\" Then\r\n        ' File does not exist - inform user\r\n        MsgBox \"Could not open the workbook. Please check it exists\"\r\n    Else\r\n        ' open workbook and do something with it\r\n        Workbooks.Open(\"C:DocsBook1.xlsm\")\r\n    End If\r\n\r\nEnd Sub<\/strong><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Kiem_tra_Open_workbook\"><\/span><b>Ki\u1ec3m tra Open workbook<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">N\u1ebfu b\u1ea1n m\u1edf workbook d\u01b0\u1edbi d\u1ea1ng ch\u1ec9 \u0111\u1ecdc, th\u00ec kh\u00f4ng th\u00e0nh v\u1ea5n \u0111\u1ec1 n\u1ebfu workbook \u0111\u00e3 m\u1edf. Tuy nhi\u00ean n\u1ebfu mu\u1ed1n c\u1eadp nh\u1eadt d\u1eef li\u1ec7u trong workbook th\u00ec b\u1ea1n ph\u1ea3i ki\u1ec3m tra n\u00f3 \u0111\u01b0\u1ee3c m\u1edf hay ch\u01b0a.<\/span><\/p>\n<p><span style=\"font-weight: 400\">H\u00e0m d\u01b0\u1edbi \u0111\u00e2y c\u00f3 th\u1ec3 ki\u1ec3m tra xem workbook m\u1edf hay ch\u01b0a, n\u1ebfu ch\u01b0a n\u00f3 s\u1ebd m\u1edf workbook.\u00a0<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nFunction GetWorkbook(ByVal sFullFilename As String) As Workbook\r\n    \r\n    Dim sFilename As String\r\n    sFilename = Dir(sFullFilename)\r\n    \r\n    On Error Resume Next\r\n    Dim wk As Workbook\r\n    Set wk = Workbooks(sFilename)\r\n    \r\n    If wk Is Nothing Then\r\n        Set wk = Workbooks.Open(sFullFilename)\r\n    End If\r\n    \r\n    On Error Goto 0\r\n    Set GetWorkbook = wk\r\n    \r\nEnd Function<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng nh\u01b0 th\u1ebf n\u00e0y:<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nSub ExampleOpenWorkbook()\r\n\r\n    Dim sFilename As String\r\n    sFilename = \"C:DocsBook2.xlsx\"\r\n\r\n    Dim wk As Workbook\r\n    Set wk = GetWorkbook(sFilename)\r\n    \r\nEnd Sub<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">M\u00e3 n\u00e3y c\u00f3 th\u1ec3 gi\u1ea3i quy\u1ebft t\u1ed1t t\u00ecnh hu\u1ed1ng tr\u00ean. Tuy nhi\u00ean n\u1ebfu workbook hi\u1ec7n \u0111ang m\u1edf \u1edf ch\u1ebf \u0111\u1ed9 ch\u1ec9 \u0111\u1ecdc ho\u1eb7c \u0111\u01b0\u1ee3c m\u1edf b\u1edfi ng\u01b0\u1eddi d\u00f9ng kh\u00e1c th\u00ec b\u1ea1n c\u00f3 th\u1ec3 x\u1eed l\u00fd t\u00ecnh hu\u1ed1ng n\u00e0y nh\u01b0 c\u00e1ch d\u01b0\u1edbi \u0111\u00e2y.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">Ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng h\u00e0m d\u01b0\u1edbi \u0111\u00e2y \u0111\u1ec3 ki\u1ec3m tra t\u1ec7p \u0111\u00e3 \u0111\u01b0\u1ee3c m\u1edf hay ch\u01b0a v\u00e0 n\u1ebfu c\u00f3 th\u00ec th\u00f4ng b\u00e1o cho ng\u01b0\u1eddi d\u00f9ng \u0111\u00f3ng l\u1ea1i \u0111\u1ec3 macro ch\u1ea1y th\u00e0nh c\u00f4ng.<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\n' Function to check if workbook is already open\r\nFunction IsWorkBookOpen(strBookName As String) As Boolean\r\n    \r\n    Dim oBk As Workbook\r\n    \r\n    On Error Resume Next\r\n    Set oBk = Workbooks(strBookName)\r\n    On Error GoTo 0\r\n    \r\n    If Not oBk Is Nothing Then\r\n        IsWorkBookOpen = True\r\n    End If\r\n    \r\nEnd Function<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">D\u01b0\u1edbi \u0111\u00e2y l\u00e0 v\u00ed d\u1ee5 v\u1ec1 vi\u1ec7c s\u1eed d\u1ee5ng ch\u1ee9c n\u0103ng n\u00e0y. Trong tr\u01b0\u1eddng h\u1ee3p n\u1ebfu workbook \u0111\u00e3 \u0111\u01b0\u1ee3c m\u1edf th\u00ec b\u1ea1n ph\u1ea3i th\u00f4ng b\u00e1o ng\u01b0\u1eddi d\u00f9ng ph\u1ea3i \u0111\u00f3ng \u0111\u1ec3 macro ti\u1ebfp t\u1ee5c ch\u1ea1y.<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nSub ExampleUse()\r\n\r\n    Dim sFilename As String\r\n    sFilename = \"C:tempwritedata.xlsx\"\r\n\r\n    If IsWorkBookOpen(Dir(sFilename)) = True Then\r\n        MsgBox \"File is already open. Please close file and run macro again.\"\r\n        Exit Sub\r\n    End If\r\n    \r\n    ' Write to workbook here\r\n    \r\nEnd Sub<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">N\u1ebfu b\u1ea1n c\u1ea7n ki\u1ec3m tra workbook c\u00f3 \u0111ang m\u1edf trong phi\u00ean b\u1ea3n excel kh\u00e1c th\u00ec c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng thu\u1ed9c t\u00ednh Readonly c\u1ee7a workbook. N\u00f3 s\u1ebd \u0111\u1eb7t th\u00e0nh True n\u1ebfu m\u1edf trong c\u00e1c ho\u00e0n c\u1ea3nh kh\u00e1c.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Close_workbook_dong_workbook\"><\/span><b>Close workbook (\u0111\u00f3ng workbook)<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">\u0110\u1ec3 \u0111\u00f3ng workbook trong VBA kh\u00e1 \u0111\u01a1n gi\u1ea3n, ch\u1ec9 c\u1ea7n d\u00f9ng l\u1ec7nh Close c\u1ee7a workbook.<\/span><\/p>\n<pre><strong>wk.Close<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">Th\u00f4ng th\u01b0\u1eddng khi \u0111\u00f3ng m\u1ed9t workbook trong VBA v\u00e0 kh\u00f4ng mu\u1ed1n th\u1ea5y th\u00f4ng b\u00e1o c\u00f3 mu\u1ed1n l\u01b0u t\u1ec7p t\u1eeb Excel. B\u1ea1n c\u00f3 th\u1ec3 ch\u1ec9 \u0111\u1ecbnh cho workbook c\u00f3 l\u01b0u hay kh\u00f4ng, sau \u0111\u00f3 th\u00f4ng b\u00e1o c\u1ee7a excel kh\u00f4ng xu\u1ea5t hi\u1ec7n n\u1eefa:<\/span><\/p>\n<pre><strong>' Don't save changes\r\nwk.Close SaveChanges:= False\r\n\r\n' Do save changes\r\nwk.Close SaveChanges:= True<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">Ta kh\u00f4ng th\u1ec3 l\u01b0u c\u00e1c thay \u0111\u1ed5i \u0111\u1ed1i v\u1edbi workbook \u0111ang m\u1edf d\u01b0\u1edbi d\u1ea1ng ch\u1ec9 \u0111\u1ecdc.<\/span><\/p>\n<p><b>L\u01b0u workbook<\/b><\/p>\n<p><span style=\"font-weight: 400\">Ta c\u00f3 th\u1ec3 l\u01b0u workbook khi mu\u1ed1n \u0111\u00f3ng n\u00f3, c\u00f2n n\u1ebfu mu\u1ed1n l\u01b0u \u1edf b\u1ea5t k\u1ef3 giai \u0111o\u1ea1n n\u00e0o ch\u1ec9 c\u1ea7n s\u1eed d\u1ee5ng ph\u01b0\u01a1ng ph\u00e1p nh\u01b0 sau:<\/span><\/p>\n<pre><strong>wk.Save<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">Ta c\u0169ng c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng ph\u01b0\u01a1ng ph\u00e1p SaveAs:<\/span><\/p>\n<pre><strong>wk.SaveAs \"C:Backupsaccounts.xlsx\"<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">Ph\u01b0\u01a1ng th\u1ee9c workbook SaveAs cho ph\u00e9p b\u1ea1n th\u00eam m\u1eadt kh\u1ea9u v\u1edbi 12 tham s\u1ed1, \u0111\u1eb7t t\u1ec7p \u1edf ch\u1ebf \u0111\u1ed9 ch\u1ec9 \u0111\u1ecdc\u2026<\/span><\/p>\n<p><span style=\"font-weight: 400\">Ngo\u00e0i ra c\u0169ng c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng VBA \u0111\u1ec3 l\u01b0u workbook d\u01b0\u1edbi d\u1ea1ng b\u1ea3n sao b\u1eb1ng SaveCopyAs.<\/span><\/p>\n<pre><strong>wk.SaveCopyAs \"C:DocsCopy.xlsm\"<\/strong><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Sao_chep_workbook\"><\/span><b>Sao ch\u00e9p workbook<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">N\u1ebfu workbook \u0111ang m\u1edf, ch\u00fang ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng hai ph\u01b0\u01a1ng ph\u00e1p tr\u00ean \u0111\u1ec3 t\u1ea1o b\u1ea3n sao t\u1ee9c l\u00e0 SaveAs v\u00e0 SaveCopyAs.<\/span><\/p>\n<p><span style=\"font-weight: 400\">N\u1ebfu b\u1ea1n mu\u1ed1n sao ch\u00e9p m\u1ed9t workbook m\u00e0 kh\u00f4ng c\u1ea7n m\u1edf th\u00ec c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng FileCopy nh\u01b0 v\u00ed d\u1ee5 sau:<\/span><\/p>\n<pre><strong>Public Sub CopyWorkbook()\r\n    FileCopy \"C:DocsDocs.xlsm\", \"C:DocsExample_Copy.xlsm\"\r\nEnd Sub<\/strong><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Su_dung_hop_thoai_File_de_mo_Workbook\"><\/span><b>S\u1eed d\u1ee5ng h\u1ed9p tho\u1ea1i File \u0111\u1ec3 m\u1edf Workbook<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">Ngo\u00e0i nh\u1eefng c\u00e1ch m\u1edf Workbook nh\u01b0 \u0111\u1ec1 c\u1eadp \u1edf t\u00ean th\u00ec c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng h\u1ed9p tho\u1ea1i File \u0111\u1ec3 m\u1edf Workbook. S\u1eed d\u1ee5ng c\u00e1ch n\u00e0y n\u1ebfu b\u1ea1n mu\u1ed1n ng\u01b0\u1eddi d\u00f9ng ch\u1ecdn workbook. C\u00f3 th\u1ec3 d\u1ec5 d\u00e0ng s\u1eed d\u1ee5ng Windows File Dialog nh\u01b0 d\u01b0\u1edbi \u0111\u00e2y:<\/span><\/p>\n<p><span style=\"font-weight: 400\">FileDialog c\u00f3 th\u1ec3 \u0111\u1ecbnh c\u1ea5u h\u00ecnh v\u00e0 s\u1eed d\u1ee5ng n\u00f3 \u0111\u1ec3:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Ch\u1ecdn m\u1ed9t t\u1eadp tin.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Ch\u1ecdn m\u1ed9t th\u01b0 m\u1ee5c.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">M\u1edf t\u1ec7p.<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">\u201cSave as\u201d l\u01b0u d\u01b0\u1edbi d\u1ea1ng t\u1ec7p.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400\">N\u1ebfu b\u1ea1n ch\u1ec9 mu\u1ed1n ng\u01b0\u1eddi d\u00f9ng ch\u1ecdn t\u1ec7p c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng ch\u1ee9c n\u0103ng GetOpenFileName.<\/span><\/p>\n<p><span style=\"font-weight: 400\">H\u00e0m sau s\u1ebd m\u1edf m\u1ed9t workbook l\u00e0m vi\u1ec7c b\u1eb1ng File Dialog, h\u00e0m s\u1ebd tr\u1ea3 v\u1ec1 t\u00ean t\u1ec7p \u0111\u1ea7y \u0111\u1ee7 n\u1ebfu t\u1ec7p \u0111\u01b0\u1ee3c ch\u1ecdn. N\u1ebfu ngwoif d\u00f9ng h\u1ee7y s\u1ebd hi\u1ec3n th\u1ecb m\u1ed9t th\u00f4ng b\u00e1o v\u00e0 tr\u1ea3 v\u1ec1 chu\u1ed7i tr\u1ed1ng.<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Function UserSelectWorkbook() As String\r\n\r\n    On Error Goto ErrorHandler\r\n\r\n    Dim sWorkbookName As String\r\n\r\n    Dim FD As FileDialog\r\n    Set FD = Application.FileDialog(msoFileDialogFilePicker)\r\n\r\n    ' Open the file dialog\r\n    With FD\r\n        ' Set Dialog Title\r\n        .Title = \"Please Select File\"\r\n\r\n        ' Add filter\r\n        .Filters.Add \"Excel Files\", \"*.xls;*.xlsx;*.xlsm\"\r\n\r\n        ' Allow selection of one file only\r\n        .AllowMultiSelect = False\r\n\r\n        ' Display dialog\r\n        .Show\r\n\r\n        If .SelectedItems.Count &gt; 0 Then\r\n            UserSelectWorkbook = .SelectedItems(1)\r\n        Else\r\n            MsgBox \"Selecting a file has been cancelled. \"\r\n            UserSelectWorkbook = \"\"\r\n        End If\r\n    End With\r\n\r\n    ' Clean up\r\n    Set FD = Nothing\r\nDone:\r\n    Exit Function\r\nErrorHandler:\r\n    MsgBox \"Error: \" + Err.Description\r\nEnd Function<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">Khi b\u1ea1n g\u1ecdi h\u00e0m n\u00e0y, b\u1ea1n ph\u1ea3i ki\u1ec3m tra xem ng\u01b0\u1eddi d\u00f9ng c\u00f3 h\u1ee7y h\u1ed9p tho\u1ea1i hay kh\u00f4ng. V\u00ed d\u1ee5 sau \u0111\u00e2y cho b\u1ea1n th\u1ea5y c\u00e1ch d\u1ec5 d\u00e0ng g\u1ecdi h\u00e0m UserSelectWorkbook v\u00e0 x\u1eed l\u00fd tr\u01b0\u1eddng h\u1ee3p ng\u01b0\u1eddi d\u00f9ng h\u1ee7y.<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Sub TestUserSelect()\r\n\r\n    Dim userBook As Workbook, sFilename As String\r\n\r\n    ' Call the UserSelectworkbook function\r\n    sFilename = UserSelectWorkbook()\r\n\r\n    ' If the filename returns is blank the user cancelled\r\n    If sFilename &lt;&gt; \"\" Then\r\n        ' Open workbook and do something with it\r\n        Set userBook = Workbooks.Open(sFilename)\r\n    End If\r\n\r\nEnd Sub<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">Ta c\u00f3 th\u1ec3 t\u00f9y ch\u1ec9nh h\u1ed9p tho\u1ea1i b\u1eb1ng c\u00e1ch thay \u0111\u1ed5i ti\u00eau \u0111\u1ec1, b\u1ed9 l\u1ecdc v\u00e0 AllowMultiSelect trong\u00a0h\u00e0m \u00a0<\/span><b>UserSelectWorkbook<\/b><span style=\"font-weight: 400\">\u00a0.<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Sub WriteToCellUsingThis()\r\n\r\n    ' Both lines do the same thing.\r\n    Debug.Print ThisWorkbook.FullName\r\n    Debug.Print Workbooks(\"MyVBA.xlsm\").FullName\r\n\r\nEnd Sub<\/strong><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Su_dung_Thisworkbook\"><\/span><b>S\u1eed d\u1ee5ng Thisworkbook<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">C\u00f3 m\u1ed9t c\u00e1ch d\u1ec5 d\u00e0ng h\u01a1n \u0111\u1ec3 truy c\u1eadp workbook hi\u1ec7n t\u1ea1i thay v\u00ec s\u1eed d\u1ee5ng\u00a0<\/span><b>Workbooks ()<\/b><span style=\"font-weight: 400\">.\u00a0\u0110\u00f3 l\u00e0 s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a\u00a0<\/span><b><i>ThisWorkbook<\/i><\/b><i><span style=\"font-weight: 400\">\u00a0.\u00a0<\/span><\/i><span style=\"font-weight: 400\">N\u00f3 \u0111\u1ec1 c\u1eadp \u0111\u1ebfn workbook hi\u1ec7n t\u1ea1i t\u1ee9c l\u00e0 workbook\u00a0 c\u00f3 ch\u1ee9a m\u00e3 VBA.<\/span><\/p>\n<p><span style=\"font-weight: 400\">N\u1ebfu m\u00e3 c\u1ee7a ch\u00fang t\u00f4i trong workbook g\u1ecdi MyVBA.xlsm th\u00ec\u00a0<\/span><b>ThisWorkbook<\/b><span style=\"font-weight: 400\">\u00a0v\u00e0\u00a0<\/span><b>Workbooks (\u201cMyVBA.xlsm\u201d)<\/b><span style=\"font-weight: 400\">\u00a0tham chi\u1ebfu \u0111\u1ebfn c\u00f9ng m\u1ed9t workbook.<\/span><\/p>\n<p><span style=\"font-weight: 400\">S\u1eed d\u1ee5ng\u00a0<\/span><b>ThisWorkbook<\/b><span style=\"font-weight: 400\">\u00a0h\u1eefu \u00edch h\u01a1n s\u1eed d\u1ee5ng\u00a0<\/span><b>Workbooks ()<\/b><span style=\"font-weight: 400\">\u00a0.\u00a0V\u1edbi\u00a0<\/span><b>ThisWorkbook<\/b><span style=\"font-weight: 400\"> kh\u00f4ng c\u1ea7n ph\u1ea3i lo l\u1eafng v\u1ec1 t\u00ean c\u1ee7a t\u1ec7p.\u00a0<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Thay \u0111\u1ed5i t\u00ean t\u1ec7p s\u1ebd kh\u00f4ng \u1ea3nh h\u01b0\u1edfng \u0111\u1ebfn m\u00e3<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Sao ch\u00e9p m\u00e3 sang m\u1ed9t workbook kh\u00e1c kh\u00f4ng y\u00eau c\u1ea7u thay \u0111\u1ed5i m\u00e3.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400\">Th\u1ef1c t\u1ebf l\u00e0 t\u00ean t\u1ec7p c\u1ee7a b\u1ea1n s\u1ebd lu\u00f4n thay \u0111\u1ed5i.\u00a0S\u1eed d\u1ee5ng\u00a0<\/span><b>ThisWorkbook\u00a0<\/b><span style=\"font-weight: 400\">\u00a0c\u00f3 ngh\u0129a l\u00e0 m\u00e3 c\u1ee7a b\u1ea1n s\u1ebd v\u1eabn ho\u1ea1t \u0111\u1ed9ng t\u1ed1t.<\/span><\/p>\n<p><span style=\"font-weight: 400\">V\u00ed d\u1ee5 sau \u0111\u00e2y cho th\u1ea5y hai d\u00f2ng m\u00e3.\u00a0M\u1ed9t s\u1eed d\u1ee5ng\u00a0<\/span><b>ThisWorkbook\u00a0<\/b><span style=\"font-weight: 400\">\u00a0v\u00e0 m\u1ed9t s\u1eed d\u1ee5ng\u00a0<\/span><b>Workbooks ()<\/b><span style=\"font-weight: 400\">.\u00a0Workbooks s\u1ebd kh\u00f4ng c\u00f2n ho\u1ea1t \u0111\u1ed9ng n\u1ebfu t\u00ean c\u1ee7a MyVBA.xlsm thay \u0111\u1ed5i.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Su_dung_ActiveWorkbook\"><\/span><b>S\u1eed d\u1ee5ng ActiveWorkbook<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><b>ActiveWorkbook<\/b><span style=\"font-weight: 400\">\u00a0\u0111\u1ec1 c\u1eadp \u0111\u1ebfn workbook hi\u1ec7n \u0111ang ho\u1ea1t \u0111\u1ed9ng. Ph\u01b0\u01a1ng ph\u00e1p n\u00e0y ch\u1ec9 h\u1eefu \u00edch l\u00fac \u0111\u00e2u.\u00a0V\u1ea5n \u0111\u1ec1 l\u00e0 b\u1ea5t k\u1ef3 workbook n\u00e0o c\u0169ng c\u00f3 th\u1ec3 ho\u1ea1t \u0111\u1ed9ng ch\u1ec9 b\u1eb1ng m\u1ed9t c\u00fa nh\u1ea5p chu\u1ed9t \u0111\u01a1n gi\u1ea3n.\u00a0V\u00ec th\u1ebf m\u00e0 c\u00f3 th\u1ec3 d\u1eabn \u0111\u1ebfn vi\u1ebft d\u1eef li\u1ec7u v\u00e0o sai workbook.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Ngo\u00e0i ra, s\u1eed d\u1ee5ng\u00a0<\/span><b>ActiveWorkbook<\/b><span style=\"font-weight: 400\">\u00a0\u00a0 l\u00e0m cho m\u00e3 kh\u00f3 \u0111\u1ecdc.\u00a0M\u00e3 c\u00f3 th\u1ec3 kh\u00f4ng ph\u00e2n bi\u1ec7t workbook n\u00e0o ph\u1ea3i l\u00e0 workbook active.<\/span><\/p>\n<p><span style=\"font-weight: 400\">V\u00ec th\u1ec3 tr\u00e1nh s\u1eed d\u1ee5ng\u00a0<\/span><b>ActiveWorkbook<\/b><span style=\"font-weight: 400\">\u00a0tr\u1eeb b\u1eaft bu\u1ed9c ph\u1ea3i l\u00e0m v\u1eady.\u00a0<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Cac_vi_du_ve_truy_cap_workbook\"><\/span><b>C\u00e1c v\u00ed d\u1ee5 v\u1ec1 truy c\u1eadp workbook<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">Ch\u00fang ta \u0111\u00e3 xem x\u00e9t t\u1ea5t c\u1ea3 c\u00e1c c\u00e1ch truy c\u1eadp workbook.\u00a0\u0110o\u1ea1n m\u00e3 sau \u0111\u00e2y cho th\u1ea5y c\u00e1c v\u00ed d\u1ee5 v\u1ec1 nh\u1eefng c\u00e1ch n\u00e0y:<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Sub WorkbooksUse()\r\n\r\n    ' This is a workbook that is already open and called MyVBA.xlsm\r\n    Debug.Print Workbooks(\"MyVBA.xlsm\").FullName\r\n\r\n    ' The workbook that contains this code\r\n    Debug.Print ThisWorkbook.FullName\r\n\r\n    ' The open workbook that was opened first\r\n    Debug.Print Workbooks(1).FullName\r\n\r\n    ' The open workbook that was opened last\r\n    Debug.Print Workbooks(Workbooks.Count).FullName\r\n\r\n    ' The workbook that is the currently active one\r\n    Debug.Print ActiveWorkbook.FullName\r\n\r\n    ' No workbook mentioned - the active one will be used\r\n    Debug.Print Worksheets(\"Sheet1\").Name\r\n\r\n    ' A closed workbook called Book1.xlsm in folder C:Docs\r\n    Workbooks.Open (\"C:DocsBook1.xlsm\")\r\n    Debug.Print Workbooks(\"Book1.xlsm\").FullName\r\n    Workbooks(\"Book1.xlsm\").Close\r\n\r\nEnd Sub<\/strong><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Khai_bao_bien_workbook\"><\/span><b>Khai b\u00e1o bi\u1ebfn workbook<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">M\u1ee5c \u0111\u00edch khai b\u00e1o bi\u1ebfn workbook \u0111\u1ec3 l\u00e0m cho m\u00e3 d\u1ec5 \u0111\u1ecdc v\u00e0 d\u1ec5 hi\u1ec3u h\u01a1n.<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Sub OpenWrkObjects()\r\n\r\n    Dim wrk As Workbook\r\n    Set wrk = Workbooks.Open(\"C:DocsBook1.xlsm\")\r\n\r\n    ' Print number of sheets in each book\r\n    Debug.Print wrk.Worksheets.Count\r\n    Debug.Print wrk.Name\r\n\r\n    wrk.Close\r\n\r\nEnd Sub<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">Ch\u00fang ta c\u00f3 th\u1ec3 \u0111\u1eb7t bi\u1ebfn workbook b\u1ea5t k\u1ef3 b\u1eb1ng c\u00e1c ph\u01b0\u01a1ng ph\u00e1p truy c\u1eadp m\u00e0 ch\u00fang ta \u0111\u00e3 t\u00ecm hi\u1ec3u.<\/span><\/p>\n<p><span style=\"font-weight: 400\">D\u01b0\u1edbi \u0111\u00e2y cho th\u1ea5y c\u00f9ng m\u1ed9t m\u00e3 nh\u01b0ng kh\u00f4ng c\u00f3 bi\u1ebfn workbook:<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Sub OpenWrkNoObjects()\r\n\r\n   Workbooks.Open (\"C:DocsBook1.xlsm\")\r\n\r\n   Debug.Print Workbooks(\"Book2.xlsm\").Worksheets.Count\r\n   Debug.Print Workbooks(\"Book2.xlsm\").Name\r\n\r\n    Workbooks(\"Book2.xlsm\").Close\r\n\r\nEnd Sub<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">Trong nh\u1eefng v\u00ed d\u1ee5 n\u00e0y, kh\u00f4ng c\u00f3 qu\u00e1 nhi\u1ec1u kh\u00e1c bi\u1ec7t.\u00a0Tuy nhi\u00ean, khi b\u1ea1n c\u00f3 nhi\u1ec1u m\u00e3, vi\u1ec7c s\u1eed d\u1ee5ng bi\u1ebfn s\u1ebd h\u1eefu \u00edch \u0111\u1eb7c bi\u1ec7t cho worksheet v\u00e0 v\u00f9ng d\u1eef li\u1ec7u khi t\u00ean d\u00e0i, v\u00ed d\u1ee5:\u00a0<\/span><b>thisWorkbook.Worksheets (\u201cSheet1\u201d). Range (\u201cA1\u201d)<\/b><span style=\"font-weight: 400\">\u00a0.<\/span><\/p>\n<p><span style=\"font-weight: 400\">B\u1ea1n c\u00f3 th\u1ec3 \u0111\u1eb7t t\u00ean bi\u1ebfn workbook gi\u1ed1ng nh\u01b0 wrkRead ho\u1eb7c wrkWrite \u0111\u1ec3 nhanh ch\u00f3ng th\u1ea5y \u0111\u01b0\u1ee3c ch\u1ee9c n\u0103ng c\u1ee7a n\u00f3.<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Tao_mot_workbook_moi\"><\/span><b>T\u1ea1o m\u1ed9t workbook m\u1edbi<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">\u0110\u1ec3 t\u1ea1o m\u1ed9t workbook m\u1edbi ta s\u1eed d\u1ee5ng h\u00e0m Add. H\u00e0m n\u00e0y t\u1ea1o ra m\u1ed9t workbook tr\u1ed1ng m\u1edbi, \u0111i\u1ec1u n\u00e0y c\u0169ng t\u01b0\u01a1ng t\u1ef1 nh\u01b0 ch\u1ecdn workbook m\u1edbi t\u1eeb menu t\u1ec7p c\u1ee7a Excel.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Khi t\u1ea1o m\u1ed9t workbook l\u00e0m vi\u1ec7c m\u1edbi ta th\u01b0\u1eddng s\u1ebd l\u01b0u n\u00f3. \u0110o\u1ea1n m\u00e3 d\u01b0\u1edbi \u0111\u00e2y s\u1ebd minh h\u1ecda c\u00e1ch th\u1ef1c hi\u1ec7n.<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Sub AddWordbook()\r\n\r\n    Dim wrk As Workbook\r\n    Set wrk = Workbooks.Add\r\n\r\n    ' Save as xlsx. This is the default.\r\n    wrk.SaveAs \"C:TempExample.xlsx\"\r\n\r\n    ' Save as a Macro enabled workbook\r\n    wrk.SaveAs \"C:TempExample.xlsm\", xlOpenXMLWorkbookMacroEnabled\r\n\r\nEnd Sub<\/strong><\/pre>\n<p><span style=\"font-weight: 400\">Khi b\u1ea1n t\u1ea1o m\u1ed9t workbook m\u1edbi, n\u00f3 th\u01b0\u1eddng ch\u1ee9a ba worksheet.\u00a0\u0110i\u1ec1u n\u00e0y \u0111\u01b0\u1ee3c x\u00e1c \u0111\u1ecbnh b\u1edfi thu\u1ed9c t\u00ednh\u00a0<\/span><b>Application.SheetsInNewWorkbook<\/b><span style=\"font-weight: 400\">.<\/span><\/p>\n<p><span style=\"font-weight: 400\">N\u1ebfu b\u1ea1n mu\u1ed1n c\u00f3 m\u1ed9t s\u1ed1 worksheet kh\u00e1c trong workbook m\u1edbi th\u00ec b\u1ea1n thay \u0111\u1ed5i thu\u1ed9c t\u00ednh n\u00e0y tr\u01b0\u1edbc khi t\u1ea1o workbook m\u1edbi.\u00a0V\u00ed d\u1ee5 sau \u0111\u00e2y cho b\u1ea1n th\u1ea5y c\u00e1ch t\u1ea1o m\u1ed9t workbook m\u1edbi v\u1edbi b\u1ea3y worksheet.\u00a0<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\nPublic Sub AddWordbookMultiSheets()\r\n\r\n    ' Store SheetsInNewWorkbook value so we can reset it later\r\n    Dim sheetCnt As Long\r\n    sheetCnt = Application.SheetsInNewWorkbook\r\n\r\n    ' Set sheets in a new workbook to be 7\r\n    Application.SheetsInNewWorkbook = 7\r\n\r\n    ' Workbook will be created with 7 sheets\r\n    Dim wrk As Workbook\r\n    Set wrk = Workbooks.Add\r\n\r\n    ' Display sheet count\r\n    Debug.Print \"number of sheets: \" &amp; CStr(wrk.Worksheets.Count)\r\n\r\n    ' Reset to original value\r\n    Application.SheetsInNewWorkbook = sheetCnt\r\n\r\nEnd Sub<\/strong><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Su_dung_WITH_voi_workbook\"><\/span><b>S\u1eed d\u1ee5ng WITH v\u1edbi workbook<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span style=\"font-weight: 400\">L\u1ec7nh WITH l\u00e0m cho vi\u1ec7c \u0111\u1ecdc v\u00e0 vi\u1ebft m\u00e3 VBA d\u1ec5 d\u00e0ng h\u01a1n. S\u1eed d\u1ee5ng With c\u00f3 \u00fd ngh\u0129a b\u1ea1n ch\u1ec9 c\u1ea7n \u0111\u1ec1 c\u1eadp \u0111\u1ed1i t\u01b0\u1ee3ng m\u1ed9t l\u1ea7n.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">V\u00ed d\u1ee5 sau c\u00f3 hai sub.\u00a0\u0110o\u1ea1n \u0111\u1ea7u ti\u00ean nh\u01b0 c\u00e1c m\u00e3 b\u00ecnh th\u01b0\u1eddng.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">\u0110o\u1ea1n th\u1ee9 th\u1ee9 hai s\u1eed d\u1ee5ng\u00a0t\u1eeb kh\u00f3a\u00a0<\/span><b>With<\/b><span style=\"font-weight: 400\">.\u00a0\u0111i\u1ec1u n\u00e0y gi\u00fap m\u00e3 r\u00f5 r\u00e0ng h\u01a1n.\u00a0C\u00e1c t\u1eeb kh\u00f3a\u00a0<\/span><b>End With<\/b><span style=\"font-weight: 400\">\u00a0\u0111\u00e1nh d\u1ea5u s\u1ef1 k\u1ebft th\u00fac c\u1ee7a m\u1ed9t m\u00e3 ph\u1ea7n b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng\u00a0<\/span><b>With<\/b><span style=\"font-weight: 400\">.<\/span><\/p>\n<pre><strong>' https:\/\/excelmacromastery.com\/\r\n' Not using the With keyword\r\nPublic Sub NoUsingWith()\r\n\r\n   Debug.Print Workbooks(\"Book2.xlsm\").Worksheets.Count\r\n   Debug.Print Workbooks(\"Book2.xlsm\").Name\r\n   Debug.Print Workbooks(\"Book2.xlsm\").Worksheets(1).Range(\"A1\")\r\n   Workbooks(\"Book2.xlsm\").Close\r\n\r\nEnd Sub\r\n\r\n' Using With makes the code easier to read\r\nPublic Sub UsingWith()\r\n\r\n    With Workbooks(\"Book2.xlsm\")\r\n        Debug.Print .Worksheets.Count\r\n        Debug.Print .Name\r\n        Debug.Print .Worksheets(1).Range(\"A1\")\r\n        .Close\r\n    End With\r\n\r\nEnd Sub.\r\n<\/strong><\/pre>\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-55.gif\"><br \/>\n<\/a>\n<\/div>\n","protected":false},"author":1,"featured_media":1837,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1836","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\/1836","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=1836"}],"version-history":[{"count":0,"href":"https:\/\/blog.ezworkapp.com\/index.php\/wp-json\/wp\/v2\/posts\/1836\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ezworkapp.com\/index.php\/wp-json\/wp\/v2\/media\/1837"}],"wp:attachment":[{"href":"https:\/\/blog.ezworkapp.com\/index.php\/wp-json\/wp\/v2\/media?parent=1836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ezworkapp.com\/index.php\/wp-json\/wp\/v2\/categories?post=1836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ezworkapp.com\/index.php\/wp-json\/wp\/v2\/tags?post=1836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}