Lập trình VBA trong excel cho người mới bắt đầu phan tự hướng 2015 Lập trình VBA trong excel cho người mới bắt đầu phan tự hướng 2015 Lập trình VBA trong excel cho người mới bắt đầu phan tự hướng 2015 Lập trình VBA trong excel cho người mới bắt đầu phan tự hướng 2015 Lập trình VBA trong excel cho người mới bắt đầu phan tự hướng 2015
fr$0r?0 tffru t0fi$H 0EEs }TMKru ru0ut HUUIdvt rvnHr Ax y^ cOH voH) Nys rynx yHN ffi ]t nv0 rpn t$ilt t0ntlff tlHc HUlildvt tlil0nff nI ilultd 'sut IecxA dgqd IeIS 3uoq4 queqJ irl uqnb u"g UgIp I€G ugrl iuo ru?r quQql ugqc ury :llurue lqr cip oeql lgl @ Suqqc ugp l4S 3uq1rn'r' urx qcYS ugnc 9^ f d93 to6 'quiu urlr cgr,t 3u9c 19't doq nt gqt 9c ueq 9p uoJl nglqu 19J u3 3uor1 yg^ qulrl Prxg dPt4d ry$ i4 Vr qu uYnb ung 9p ,+s I9l lq )v '(g/,w) suotlrsctlc[dy to! trsug IDnsrA rq8 qcgc 'nqu lgru rqn8u oqc legl u93 p ?s qogs ugnc SunP IQN'0102'1002 urgqr Suns'gq'lpqu dPc 9c q^ qrPS ,(eu ugq ugl uECl I91 roB 3uor1 yg^ quIrl dgl Sugnp uQ3 o(^ r.gnq I,9III rou e^ ouc 3uon1 19qJ 93 nQq Icl uJ oq lrlp pp urT tgt'vgA 4su u93u uiqcgt 1.9,r doq nqd leIQ clp q't bunp r9'r' ogt'r' IOIA ler 913 rgp ogP 3u9P u?P VgA Suori Suoqd ugrt rqn8u 3u.1qu 3uor1 'GgA) uottouyddY Tacrg lbsotLtll11 n{ crsag U)nsr1 43u ug8u I.9^ qu1n dQ1 Supu 9q{ oli pV uqc€lc I9u ?q t-YI1 qu1'u 3u6nqc-.q- u9q4 ',(gu r.or8 gq, ,q- 9s qull Sugq Y'lVI 'q-uip tg(nb u f1 ugnb pqu oqo lelql ugr c93 4t nx '4t upnb cnqc 91c91ovql Y tgur RI rqr uer rupu qulr SuAqu gc rgqd e,r, SunP 4s sonP uoc 9u 'doq ?ugnrr nglqu ulgur ugqd 'tgqc fqo ugqd quqqi t uglq gqd urgir ugqd 9q q1 SueP ec iP)xg lpsotcrl/g ryn7u 3w-tt15J - top uoq u'tgu't u7t1d 3ur3p {n8 :tn3 upllL nvc l9N lQl 8xce12003 5.1.2 Excel2007 vh 2010 5.2 Antohn ldm viQc v6'i tfp tin Excel 5.3 Sil dyng cdc chuong trinh diQt virus " ' "" 4'7 8.s.7 c '." ' " .48 ' ' ' '.' 50 8.5.8.: 8.5.9 T 53 8.5 10 ' 5.1.1 8.5 t l uu, NHugc DlEv cun cHuc NArue ru GHI MACRO 6.1 Uu dii5m cira chirc ndng tg ghi macro (Record Macro) 5.2 Nhuo.'c cti,5m cira chrlc ning tr,r ghi ' 8.5.12.l 56 macro .51 8.5.13 I 8.6 Tra p6r rUqNG LAP TRiNH vBA cuNG vor PHUONG THUC, THUOC TJNH CUA VBA 7.2 Phuong thilc (Method) 7.3 ThuQc tinh (Property) vOr s6 o0r ruoNc co " 59 ' "-.- ' " .62 65 -.' ".""' 67 Application ""' 68 8.1.1 ThuOc tinh Calculation ' ""' 69 8.1.2 ThuQc tinh ScreenUpdating '.""" 69 8.1.3 ThuQc tinh DisplayAlerts "" 70 '.' 8.2.Ddituo.ng Window ""-"""'72 8.3 D6i tuQng Workbook """""""""'73 8.4 DOi tuo.ng Worksheet .'.""""' 73 8.4.1 Ei6u khiiSn Worksheet """'75 8.4.2.Xdc dinh c6c th6ng tin Worksheet " ' """"75 8.a.3 NhAp dfr'liQu vho Worksheet """"""""""'76 8.5 Ddi tuo.''rg Range (kh6i 6) vd Cells (6) ' "'."' S.1 DOi tuo.ng Selection ' 8.5.3 DOi tuong Cells 8.5.4 ThuQc tinh Offset: 8.5.5 ThuOc tinh Resize 8.5.2 DOi tuo-ng - ' Ddi ^8.5.6 ^a' tuo.ng ,o Rows Rnrvq vh vi Columns Colnmns 9.1 Khai b6o I 9.2 Cic roiin """"""""""'7'7 """""""""79 """"""""' 80 """"""" 8l """"""" 82 ,.,, , 84 r, 9.3 Biiiu thri'c 9.4 Gin ri gi6, 9.4.1 ce BAN TRoNG EXCEL 8.5.1 Phuong thf'c Select vh phuong ph6p xdc dfnh ming vi KH} CHUNG 7.1 D6i tuo.ng l4p trinh cfu ' 9.4.2 Ge 9.4.3 G6 9.4"4.Hu 9.5 Khai b6o v 9.5.1 Md 9.s.2.MA 9.6 Sri dung T1 9.7.Xdc clinh ki 9.8 Vdng ctoi cr 10.1 Tro girip tr 10.2 Sn'dpng hr 10.3 Sir dung vi 10.4 Sfi'dung In THIi 11.1 S'i'dung 01 GHI MACRO VOI A I I T T\T.T 11.2 Sfi'dung VBAProject tZ6 pHuoNG pHAp TU xAy DUNG CHUONG rnirun lZ.l.Xdy dlmg macro m6.i 12g lL.2.Ngitddng md t€nh qud ddi 130 12.3 Cl,c t5i xay xdy dung macro l3l su DUNG VA XAy DUNG sArrt TRONG VBA 13.1 Sn'dung c6c hdm sin c6 rrong Exce1 14.1.: 14.1.1 14.2 Phuon 14.3 15.1 Di6u t5.1.2 13.1.1 Goi ci4c him WorksheetFunction rir VBA 135 13.1.2 Chbn hdm Excel v)o 15 .13j THOAT TRONG VBA box 14.1.1 Cr{c lo4i th6ng rliQp rrong butrons 14.L2 Th6ng s6 c6c nrit di6u khi€n la.1 HQp thdng b6o Message 10 l 15.1.4 13.2 Sf'dgng c6c hdm nop kl 15.1.1 135 V8A 13j 13.2.1 Him to6n hgc 13g 13.2.2 Him xi4c nhdn d[.Iipu 139 13.2.3 Hdrn chuy6n ctOi giu,a cric ki6u dfi.li6u 140 13.2.4 Hdm v6 x0,lf chudi 142 13.2.5.IIdm rlinh dang dfr.li€u .145 13.2.6 Him tgo s6 ng6u nhi6n t46 13.2.7 Hdm v6 ngiy gio 147 13.2.8 Hdm v€ cl6 hoa i51 13.2.9 Him chuong trinh , 152 13.2.10 Cric hdm khiic 152 13.3.Xdy dsng him VBA .155 13.3.1 phdn bi6t hdm tu lap v6i thrj tuc : 155 13.3.2 Ci{ch xdy dung him tu 1Ap 155 13.3.3 MO t6 nQi dung hdm 158 13.3.4 T4o vd sil dgng him tu.lAp rrong Add_Ins .t59 13.3.5 Goi hdm tu l6p tir thri tuc 162 Him Ir 15.1.5.l 15.2 Didu ls.2.1 I ls.2.2.1 15.2.3 E ls.2.4 E r5.2.s D Ls.2.6 P) 15.2.7 D 1s.2.8 Di ts.2.9.LC 15.2.10 'v 15.2.11 D 16.1 C6c kiilu td 16.2.L'i thi hdnt 16.2.1 X6,c 163 .164 164 16.2.2 C6ct 16.2.3 G6i 16.2.4 Nhfrl 16.2.5 X6,c r26 I.JONG TRINH t28 130 131 [ Msgbox 165 14.1.4 XAy dung tham s6 cho MsgBox 166 14.2 Phuong thri'c InputBox 166 14.3 Him InputBox 169 14.1.3 Cdc bi6u tuong thdng di6p cAu rRUC olAu rHrdnr , -']b vbA I 35 -,BA .135 r31 13'7 138 139 r42 laJ - -" | /1< ".'146 r47 I52 t55 155 159 t62 /BA It 17.6 Nhfrn; r7.6 r'7.6.' 18.1 Vi du 18 l 18.l i 18.1.: 18 l.z 18.1.i l8.2.Yi dv 18.2.1 18.2.2 t8.2.3 18.2.4 18.3 Thay d 18.3.1 18.3.2 18.4 M6t s6 18.4 l 18.4.2 18.4.3 18.4.4 18.4.5 18.4.6 t8.4.7 l8.4.8 18.4.9 18.4.10 18.4.11 Spin Button BAng2O-6: CAc gi6 tri Control cria di€u khi6n pornbo Box G Current value Gi6 tri hiqn tai cira di6u kni6n' Minimum value Gi6 tri nh6 nhAt cira diiiu khien (0)' Maximum value cir{ tril6n nhAt cira diAu khi€n (30,000)' Incremental change Bu6c gi6 tri thay d6i bAm vho mfii tOn l6n' xu6ng' duo-c chqn, 2, 3,r ?il:ht-9 rhi di€u khi6n tl6 tritrong chinh Id Dia chi 6li6n kOt hiQn io di6u khi6n tl6 , gi6 Cell link HIL: I a Dim ]+i A lhi v! lh6l) HEU Ydn !14*t NAng Thi lhio viThi chuyen ll E s,* l0 1l -t2", o tiaoult gidi tirh: i!' ' o uen 6t.nu ai^, t+'l d ti* tilt.hu ognieng, tS ' -"ii., oUgnf6tPshgcsinh: d nentdt ngonogu' 20: tl : 1'>: crla di€u khidn Spin Button Hinh 20-15: Crla s6 thu6c tfnh Control - Diiu khiAn Scroll Bar: Di€u khiOn ndY cho Ph6P chqn c tinh to6n 2'ts Bing20-7: - v6'i Button Cdc gi6 tri Control cta di€u khi6n Spin Eon , , A ung vol so l - v6'i Current value Gi6 tri hiqn tai cira diOu khi6n' Minimum value Giri tri nh6 nh6t cira di€u khiCn (0)' Maximum value Gi6 tri l6n nhAt cira tti6u khi6n (30,000)' Incremental change xu6ng' Bu6'c gi6 tri thay d6i bAm viro mfii tOn l6n' Page change giil'a hinh Bu6'c gi6 tri thay d6i bAm vio khoAng mfri tOn vi trang thdi cira di0u khi6n' K6t t vho gid tri (Hinh 20-1' r CJ 'de'i#5; lr6D dc chinh lh Dia chi liOn k6t v6i bing tinh, gi6 tri tlong gi6 tri hiQn tai cira di6u khiOn Cell link sl il: " Thu.( 10, -l*;l: ll t2 ;,6 tier tEt 13 o li6! kit 1.1 o liin k€t l-( oli6ok;t 16 o liEnkdt ? E I j :dt l, 10 FcrftrattLng ll 11 15 o lidnkdtgi6itinh: licn kat cho dfun: d litu kit chil nghi€ng: li€n kct Ds hqc ainh: 16 O liCn k6t ng6n 71 IJ i4 + ng;: ti:f$';':ri'*t + ti ll Gidi tinh ii li i > rr nat rrir >,,: iztlDii Ngon trgD 10 ?l 2? i3 di€u khidn scroll Bar H)nh 20-17: Cta s6 thu6c tinh Control cia 20.2.5 Li6n k6t va diAu khidn bang tinh tu c6c Control: -t Validation (DV) 2',76 I V, tri duo-c d6i tuo-n t€n l6n, xu6ng uons chinh ld iroll Bar ol: c li6n kiit trgc Mng ta c6 th6 khai h ntru hdm nhu If, bating (CF), Data duo ' :,, 'i ComboBc H ^i : I 'i+ co j,s'," '!s *, iij tiJ ll'' '! co ti.t.: o, i'a rc' Hinh 20,3.2 Thu ,A uen Ket gl chgn il6i t s6 Propert khai br{o v thu6c tfnh chung Bir BAng2O-8: M6 td c:ic ali€u khidn ri€ng cia ActiveX Controls HQp nhflp vdn bAn, cho ph6p sila ddi vd th6m vdn bin m6'i TextBox ld di€u khiiSn r6t ph6 bit5n trone UserForm vi c6 th6 su' duns Ati nien thi hoac vOu cAu d[' liQu tu' nguoi sri: d"-ung Chring tu O iftC nhfp vho chu5i, s6, tham chi6u hoac cdng thilc Text Box Iiq r-:i,t€ri: l ii#;r;H;d"il- chring Gdn inh cho d6i tuo.ng Image Dii;u khi6n ndy chdp nhfln loqi dnh nhu *.bmp, *.cur, *.ico, *.jgp, *.wmf Image ,.! g '1.- -ombo Box Hom€ nhin chung '-B.A, Jidu khi6n kh6ng :l visilsl Mo(ro5 Easic )'t i hsef Record Maffo lE u:e Relatile Referen(es -tl lJscro Sec(fltt aaLlz i , Revew " l";" Yprooetu€s r-r A{ '' ''-' :C vrerr Code ; lnsed Desion; - [1od-e ' , A Fln orilog g sour(e a.jr\t Ei I : tai thuc don cl6 , Fdmlulns onti =EMBED("Forms ComboBox, 1","") xuAt hiQn danh ia ,:t,, pao€ letoL( ri ;nuyen, tnay oor ,.d'*ii,r cu! !op'/ Comb.Ror Qb ed 8ro!pinq Controls n6n t6i biit cira ActiveX inh chung nhu'cld Or( er Fornlat Cortrol Hinh 20-21: Di€u khidn dan6 o che aO Design Mode v) cta sd Properties 20.3.2 ThuOc ign Mode c6 tiic ng (Flinh 20-21) :ic xdy dung th0 I ctrti d0 Design !_: ;., X ,^ ilet ve mret lap tfnh Properties: ThuQc tfnh Properties ttieu khi6n su lam vi6c cira cdc cl6i tu'-ong tao su' li6n ktit gifr'a ctiOu khi6n vh worksheet Khi da xdy dqng xong m6t it6i tuo.ng, chgn dtii tuo.ng d6 vd bdm phAi chuQt (Flinh 20-21) Chon menu Properties, cila s6 Properties hiQn.ra v6i rAt nhiAu thu6c tinh khdc nhau, mQt s6_thuQc tfnh ld khai b6o vd mQt s6 thi chi