Nguyn Phng Tho - Trung tm Dy ngh v Ph cp Tin hc Min ni ABC 525 - in Bin - TX. Yn Bi Bn thn mn! Qua cc bi do u, chng ta lm quen vi kiu lp trnh VBA (Visual Basic For Application) n gin. Lp trnh VBA c rt nhiu ci li, nu thnh tho ta s c nhng sn phm hn hn lp trnh VB (Visual Basic thun) v cc l do sau y: - Bn tn dng c ton b nhng g hc c t Visual Basic. - Bn tn dng c mi kh nng m chng trnh Microsoft Office mang li. Nu lp trnh trong Excel, bn tn dng c ton b cc th vin v nhng cng c tnh ton bt kh chin bi, ng u th gii. Ta khng phi vit nhng thut ton n gin, m ch vic "moi" t chng trnh Excel ra s dng. (Ta tm gi cc chng trnh ny l cc chng trnh bao). Cc chng trnh bao nh EXCEL, Winword, Power Point, Access, AutoCAD, OutLook... c rt nhiu th vin DLL xy dng sn, ang ch chng ta khm ph. Nu bn bit ting Anh th l nhng kho vng kin thc cho bn hc v... bt chc. C rt nhiu v d cc k phong ph v a dng ta khng th b qua. Ti dch hng nm tri m vn cha th ht c kho d liu s . - Cc chng trnh bao u h tr cc dng ni kt OLE, DDE l mt xch tuyt vi bn ni kt v chia s d liu, m bao cng sc lp trnh. Mt v d: y l s i tng Microsoft Office:Ta c th thit lp, thay i, thm ch ci t cc phn ny d nh tr bn tay. Theo s ny, i tng v tp hp (Collection) c m t mu vng. Cn ch c i tng th mu xanh. Ta thy mi i tng v tp hp u c cng dng ring trong Microsoft Ofice Object: 1. Command Bars: Lm vic vi thanh cng c v nt cng c. (Tt nhin thanh thc n cng ch l thanh cng c c bit, thanh ShortCut Menu - Hin th khi nhn phm phi - cng nm trong s ny). 2. Assistant: Lm vic vi nhn vt hot hnh, iu khin v bt n phc v mnh. (Xem bi lp trnh vi nhn vt hot hnh ca Office - Nguyn Phng Tho) 3. File Search: Lm vic vi cng c tm kim. 4. Document Properties: Lm vic vi cc tnh cht ca vn bn. 5. FileFile - Macintosh: Lm vic vi h iu hnh Macitosh. (Phn ny ti ch bit qua khng dm vit v khng hiu r). Ti mi phn trn u c rt nhiu phn chi tit km theo. Nhng phn ny rt t ti liu cp ti. Tuy nhin khng ch c cc i tng trn m rt nhiu i tng khc. Ti cha mun a ra bi vit ny, v nu a ra c th c bn s chong ngp v n cng cha mang li mc ch g c. Vic lp trnh trong VBA thng rt thun li do giao din chnh c chng trnh bao. Bn ch phi lp k hoch cho mt s cng vic chnh, sau mi n vn trau chut. Cc cng vic chnh gm: 1. Giao din v kiu cch nhp d liu. Bn mun ngi dng nhp nhng d liu g? Nhp b sung vo lc no? Nhp vo u, vo chnh chng trnh bao hay nhp vo cc hp InputBox, Form, Dialog, MsgBox?... 2. Giao din v kiu cch xut d liu. Xut nhng g, xut vo u, vo v tr trn chng trnh bao hay bn thng bo bng cc form, dialog hay report bi tp, kt qu in... 3. Tm mi lin h gia gi tr nhp vo vi gi tr xut ra. C bao nhiu cng thc. D liu cung cp cho cng thc nhp u (T chng trnh bao, hp Dialog hay yu cu nhp thm. Nu nhp thm th b sung vo cng vic 1). Sau khi xy dng xong k hoch, mi bt tay vo thit k. Thit k giao din d nht, nhng phi c u c quan st v m thut cho tht p. Mt Form p, cn i bao gi cng chim nhiu cm tnh hn. Qu trnh trau chut thng ch l thay i tnh cht (property) ca i tng m thi. Ngay c vn xut d liu dng n gin, bn cng c th xut bng phng php thay i tnh cht (property) ca i tng. V thay i property ca i tng ta ch vic p dng cng thc truyn thng: Tn_i_tng. Tn_tnh_cht = Gi_tr_mi Gi tr mi c th l gi tr c th hay mt biu thc ra mt gi tr khc. Vn quan trng l bn phi hiu v tr li c cu hi ca ming:"Nu ti lm vic A, my phi p ng c cng vic B. Mun my lm cng vic B, ti phi dng cng thc i vi i tng no, v cng thc cn thay i tnh cht g ca i tng ?" trnh lan man, ti xin ni v lp trnh Excel. Tuy nhin nhng bi vit khc, ti s ni v chng trnh khc (V d nh Word, Power Point, AutoCAD, Access, OutLook...). Cc thnh phn lp trnh VBA trong EXcel Trong Excel, cc thnh phn lp trnh c chia lm cc ch ln l: Bt u vi Visual Basic Excel vi cc tham chiu Visual Basic Ngn ng Visual Basic vi tham chiu Cc ch truy tm vi Visual Basic C s d liu truy cp i tng Tham chiu vi s dng Jet SQL Office vi cc tham chiu Visual Basic Form vi cc tham chiu Visual Basic Mu pht trin vi li khuyn Tip Tham chiu i tng vi Form. Ti mi ch ln li chia ra nhiu cc ch con. Mi ch con u c phn loi v ni kt cc mc vi nhau. Nu bn bit Ting Anh th y qu l nhng v d v bi hc thc s b ch. Ta cng c th hc c rt nhiu phn ny. Ni v cu trc chng trnh chy trong Excel V cu trc chng trnh, tt c cc chng trnh chng ta thit k trong Excel u c vit trong Module ca tp d n. tp d n bn phi t tn nh tp Excel thng thng. Ri vo ch lp trnh VBA bng nhiu cch, thng g Alt+F11 ri Ctrl+R hin Project Explore. Chn tp d n ca mnh v Insert Module. Ti module ny ta vit cc chng trnh dng Sub TEN_THU_TUC. y l cc th tc t do. Th tc t do ging nh bt c cc th tc thng thng m ta vn lp trnh bng Visual Basic. Cng tng t vi vic bn chn Form v vit cc th tc bin c ca Form. Ti tp Project ta cn c th vit mt dng th tc na l th tc bin c. Ta n hin ra nh th ny: Tn bin c Th tc p ng (Khi nh nh thm Private vo trc) A. Th tc cho Work Book 1. Work Book b kch hot Sub Workbook_Activate() Work Book thm AddIn Sub Workbook_AddinInstall() Work Book g b AddIn Sub Workbook_AddinUninstall() Work Book trc khi ng Sub Workbook_BeforeClose(Cancel As Boolean) Work Book trc khi in Sub Workbook_BeforePrint(Cancel As Boolean) Work Book trc khi ct Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Work Book mt Focus Sub Workbook_Deactivate() Work Book thm Sheet mi Sub Workbook_NewSheet(ByVal Sh As Object) Work Book m Sub Workbook_Open() Sheet ca Work Book c kch hot Sub Workbook_SheetActivate(ByVal Sh As Object) Sheet ca Work Book trc khi Dclick Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Excel.Range, Cancel As Boolean) Sheet ca Work Book trc khi Right click Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Excel.Range, Cancel As Boolean) Sheet ca Work Book tnh ton Sub Workbook_SheetCalculate(ByVal Sh As Object) Sheet ca Work Book thay i Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Excel.Range) Sheet ca Work Book mt Focus Sub Workbook_SheetDeactivate(ByVal Sh As Object) Sheet ca Work Book thay i vng chn Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range) Ca s Work Book c kch hot Sub Workbook_WindowActivate(ByVal Wn As Excel.Window) Ca s Work Book thi kch hot Sub Workbook_WindowDeactivate(ByVal Wn As Excel.Window) Ca s Work Book i c Sub Workbook_WindowResize(ByVal Wn As Excel.Window) B. Th tc cho tng Work Sheet ring (Chn Sheet cn vit v chn theo bin c) Sheet c kch hot Sub Worksheet_Activate() Sheet trc khi b Dclick Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean) Sheet trc khi b RightClick Sub Worksheet_BeforeRightClick(ByVal Target As Excel.Range, Cancel As Boolean) Sheet tnh ton Sub Worksheet_Calculate() Sheet thay i Sub Worksheet_Change(ByVal Target As Excel.Range) Sheet mt kch hot Sub Worksheet_Deactivate() Sheet thay i vng chn Sub Worksheet_SelectionChange(ByVal Target As Excel.Range) Tuy nhin ta khng phi nh chi tit nhng bin c my. Bn hy chn hp chn Object ri chn bin c th tc hp Procedure. Excel s t vit cho bn phn u v kt thc th tc. Bn ch vic nh vo trong lng th tc nh ca mnh l xong. Ngoi cc th tc v bin c Excel cng nh cc chng trnh Office khc u c cc Macro t ng thc hin. Cc Macro ny, bn ch cn dt ti mt module bt k. Bao gm cc Macro sau Tn macro Cng dng T ng thc hin khi Auto_Open() M tp Auto_Exec() Vo Excel Auto_Close() ng tp Auto_Exit() Thot Auto_New() To tp mi Khi c bin c thuc cc loi trn, th tc s t ng thc hin. Ni v nhp gi tr vo cc bin trong th tc: Cng ging nh lp trnh Visual Basic thun ta thng s dng cc phng php sau nhp gi tr vo cc bin trong th tc nhm phc v cho vic x l: 1. S dng hm nhp: (Hm c sn Visual Basic) a gi tr vo bin Bin_Cn_ly_gi_Tr = INPUTBOX ("Li nhc","Tiu li nhc","Ga tr t sn") 2. S dng hm hi li (Hm c sn Visual Basic) a gi tr tr li vo bin ri a bin vo cu trc x l Bin_Cn_ly_gi_Tr=MsgBox("Li nhc",Nt+Kiu+Ngm nh,"Tiu ") 3. Ly gi tr t mt trong mt sheet bt k ca Excel a vo bin Bin_Cn_ly_gi_Tr = Sheets("Tn sheet").Cells( hng, ct) 4. Ly gi tr cu mc t chng trnh khc bng k thut HOOK (Bi ring). 5. Ly gi tr t cc tnh cht ca Form m mnh thit k. Bin_Cn_ly_gi_Tr = Tn_Form.Tn_i_Tng.Thuc_tnh_cn_ly 6. Ly gi tr thng qua cc hp thoi c sn ca Excel Tn_Bin_DiaLog = Application.Dialogs(Tn_hng).Show Bin_Cn_ly_gi_Tr = Tn_Bin_DiaLog.Tn_i_Tng.Thuc_tnh_cn_ly Tu vo gi tr ca hng trong bng sau m hp thoi no s c trng ln. Bn thy c ti 200 hp thoi ca Excel l cha k n c nhng hp thoi cn c n vi cch th hin (n xu lun!!!). Tn hp thoi hay tab ca hp thoi Hng Tham s km theo xc lp 1. Kch hot xlDialogActivate window_text, pane_num Font ca xlDialogActiveCellFont font, font_style, size, strikethrough, superscript, subscript, outline, shadow, underline, color, normal, background, start_char, char_count Thm t ng th Format xlDialogAddChartAutoformat name_text, desc_text iu khin AddIn xlDialogAddinManager operation_num, addinname_text, copy_logical Cn xlDialogAlignment horiz_align, wrap, vert_align, orientation, add_indent p dng Tn xlDialogApplyNames name_array, ignore, use_rowcol, omit_col, omit_row, order_num, append_last p dng kiu xlDialogApplyStyle style_text Dch chuyn xlDialogAppMove x_num, y_num Thay c xlDialogAppSize x_num, y_num Xp lp xlDialogArrangeAll arrange_num, active_doc, sync_horiz, sync_vert Gn vo i tng xlDialogAssignToObject macro_ref Gn vo cng c xlDialogAssignToTool bar_id, position, macro_ref Gn Text xlDialogAttachText attach_to_num, series_num, point_num Gn Thanh cng c xlDialogAttachToolbars G tt xlDialogAutoCorrect correct_initial_caps, capitalize_days Xc nh trc xlDialogAxes x_primary, y_primary, x_secondary, y_secondary xlDialogAxes x_primary, y_primary, z_primary ng bao xlDialogBorder outline, left, right, top, bottom, shade, outline_color, left_color, right_color, top_color, bottom_color Tnh ton xlDialogCalculation type_num, iter, max_num, max_change, update, precision, date_1904, calc_save, save_values, alt_exp, alt_form Bo v xlDialogCellProtection locked, hidden i lin kt xlDialogChangeLink old_text, new_text, type_of_link Thm d liu th xlDialogChartAddData ref, rowcol, titles, categories, replace, series Trend th xlDialogChartTrend type, ord_per, forecast, backcast, intercept, equation, r_squared, name Hng dn nhanh th xlDialogChartWizard long, ref, gallery_num, type_num, plot_by, categories, ser_titles, legend, title, x_title, y_title, z_title, number_cats, number_titles Thuc tnh checkbox xlDialogCheckboxProperties value, link, accel_text, accel2_text, 3d_shading Xo xlDialogClear type_num Bng mu xlDialogColorPalette file_text D rng ct xlDialogColumnWidth width_num, reference, standard, type_num, standard_num Combination xlDialogCombination type_num Chng d liu xlDialogConsolidate source_refs, function_num, top_row, left_col, create_links Copy th xlDialogCopyChart size_num Copy ho xlDialogCopyPicture appearance_num, size_num, type_num To tn mi xlDialogCreateNames top, left, bottom, right To Publisher xlDialogCreatePublisher file_text, appearance, size, formats Thanh cng c theo mun xlDialogCustomizeToolbar category Xo d liu xlDialogDataDelete Nhn d liu xlDialogDataLabel show_option, auto_text, show_key Chui d liu xlDialogDataSeries rowcol, type_num, date_num, step_value, stop_value, trend t tn xlDialogDefineName name_text, refers_to, macro_type, shortcut_text, hidden, category, local t kiu xlDialogDefineStyle style_text, number, font, alignment, border, pattern, protection xlDialogDefineStyle style_text, attribute_num, additional_def_args,... Xo nh dng xlDialogDeleteFormat format_text Xo tn xlDialogDeleteName name_text Demote xlDialogDemote row_col Hin n xlDialogDisplay formulas, gridlines, headings, zeros, color_num, reserved, outline, page_breaks, object_num xlDialogDisplay cell, formula, value, format, protection, names, precedents, dependents, note Tnh cht hp xlDialogEditboxProperties validation_num, multiline_logical, vscroll_logical, password_logical Son mu xlDialogEditColor color_num, red_value, green_value, blue_value Xo, sa xlDialogEditDelete shift_num La son xlDialogEditionOptions edition_type, edition_name, reference, option, appearance, size, formats t chui xlDialogEditSeries series_num, name_ref, x_ref, y_ref, z_ref, plot_order Bo li bar trc X xlDialogErrorbarX include, type, amount, minus Bo li bar trc Y xlDialogErrorbarY include, type, amount, minus Khai trin xlDialogExtract unique Xo tp xlDialogFileDelete file_text T in nhm xlDialogFillGroup type_num T in nhm lm vic xlDialogFillWorkgroup type_num Lc ci tin xlDialogFilterAdvanced operation, list_ref, criteria_ref, copy_ref, unique Tm tp xlDialogFindFile Chn Font xlDialogFont name_text, size_num Tnh cht font xlDialogFontProperties font, font_style, size, strikethrough, superscript, subscript, outline, shadow, underline, color, normal, background, start_char, char_count T nh dng xlDialogFormatAuto format_num, number, font, alignment, border, pattern, width nh dng th xlDialogFormatChart layer_num, view, overlap, angle, gap_width, gap_depth, chart_depth, doughnut_size, axis_num, drop, hilo, up_down, series_line, labels, vary Kiu th nh dng xlDialogFormatCharttype apply_to, group_num, dimension, type_num nh dng font xlDialogFormatFont color, backgd, apply, name_text, size_num, bold, italic, underline, strike, outline, shadow, object_id, start_num, char_num xlDialogFormatFont name_text, size_num, bold, italic, underline, strike, color, outline, shadow xlDialogFormatFont name_text, size_num, bold, italic, underline, strike, color, outline, shadow, object_id_text, start_num, char_num nh dng ch thch xlDialogFormatLegend position_num nh dng chnh xlDialogFormatMain type_num, view, overlap, gap_width, vary, drop, hilo, angle, gap_depth, chart_depth, up_down, series_line, labels, doughnut_size nh dng di chuyn xlDialogFormatMove x_offset, y_offset, reference xlDialogFormatMove x_pos, y_pos xlDialogFormatMove explosion_num nh dng s xlDialogFormatNumber format_text nh dng Overlay xlDialogFormatOverlay type_num, view, overlap, gap_width, vary, drop, hilo, angle, series_dist, series_num, up_down, series_line, labels, doughnut_size nh dng c xlDialogFormatSize width, height xlDialogFormatSize x_off, y_off, reference nh dng vn bn xlDialogFormatText x_align, y_align, orient_num, auto_text, auto_size, show_key, show_value, add_indent Tm cng thc xlDialogFormulaFind text, in_num, at_num, by_num, dir_num, match_case, match_byte Nhy n cng thc xlDialogFormulaGoto reference, corner Thay th cng thc xlDialogFormulaReplace find_text, replace_text, look_at, look_by, active_cell, match_case, match_byte Hng dn nhanh hm xlDialogFunctionWizard Th vin 3D vng xlDialogGallery3dArea type_num Th vin 3D thanh xlDialogGallery3dBar type_num Th vin 3D ct xlDialogGallery3dColumn type_num Th vin 3D xlDialogGallery3dLine type_num ng Th vin 3D bnh xlDialogGallery3dPie type_num Th vin 3D b mt xlDialogGallery3dSurface type_num Th vin vng xlDialogGalleryArea type_num, delete_overlay Th vin thanh xlDialogGalleryBar type_num, delete_overlay Th vin ct xlDialogGalleryColumn type_num, delete_overlay Th vin theo mun xlDialogGalleryCustom name_text Th vin Doughnut xlDialogGalleryDoughnut type_num, delete_overlay Th vin ng xlDialogGalleryLine type_num, delete_overlay Th vin bnh xlDialogGalleryPie type_num, delete_overlay Th vin ra a xlDialogGalleryRadar type_num, delete_overlay Th vin Scatter xlDialogGalleryScatter type_num, delete_overlay GoalSeek xlDialogGoalSeek target_cell, target_value, variable_cell ng li xlDialogGridlines x_major, x_minor, y_major, y_minor, z_major, z_minor, 2D_effect Chn xlDialogInsert shift_num Chn Objiect xlDialogInsertObject object_class, file_name, link_logical, display_icon_logical, icon_file, icon_number, icon_label Chn ho xlDialogInsertPicture file_name, filter_number Chn tiu xlDialogInsertTitle chart, y_primary, x_primary, y_secondary, x_secondary Tnh cht nhn xlDialogLabelProperties accel_text, accel2_text, 3d_shading Tnh cht list box xlDialogListboxProperties range, link, drop_size, multi_select, 3d_shading La chn Macro xlDialogMacroOptions macro_name, description, menu_on, menu_text, shortcut_on, shortcut_key, function_category, status_bar_text, help_id, help_file Son th xlDialogMailEditMailer to_recipients, cc_recipients, bcc_recipients, subject, enclosures, which_address Vo th xlDialogMailLogon name_text, password_text, download_logical Th k xlDialogMailNextLetter th chnh xlDialogMainChart type_num, stack, 100, vary, overlap, drop, hilo, overlap%, cluster, angle Kiu th chnh xlDialogMainChartType type_num Son sa Menu xlDialogMenuEditor Di chuyn xlDialogMove x_pos, y_pos, window_text To mi xlDialogNew type_num, xy_series, add_logical Ghi nh xlDialogNote add_text, cell_ref, start_char, num_chars Tnh cht i tng xlDialogObjectProperties placement_type, print_object Bo v i tng xlDialogObjectProtection locked, lock_text M xlDialogOpen file_text, update_links, read_only, format, prot_pwd, write_res_pwd, ignore_rorec, file_origin, custom_delimit, add_logical, editable, file_access, notify_logical, converter M lin kt xlDialogOpenLinks document_text1, document_text2,..., read_only, type_of_link M th xlDialogOpenMail subject, comments M Text xlDialogOpenText file_name, file_origin, start_row, file_type, text_qualifier, consecutive_delim, tab, semicolon, comma, space, other, other_char, field_info La chn tnh ton xlDialogOptionsCalculation type_num, iter, max_num, max_change, update, precision, date_1904, calc_save, save_values La chn th xlDialogOptionsChart display_blanks, plot_visible, size_with_window La chn son xlDialogOptionsEdit incell_edit, drag_drop, alert, entermove, fixed, decimals, copy_objects, update_links, move_direction, autocomplete, animations La chn chung xlDialogOptionsGeneral R1C1_mode, dde_on, sum_info, tips, recent_files, old_menus, user_info, font_name, font_size, default_location, alternate_location, sheet_num, enable_under La chn List xlDialogOptionsListsAdd string_array xlDialogOptionsListsAdd import_ref, by_row La chn Transition xlDialogOptionsTransition menu_key, menu_key_action, nav_keys, trans_eval, trans_entry La chn xem xlDialogOptionsView formula, status, notes, show_info, object_num, page_breaks, formulas, gridlines, color_num, headers, outline, zeros, hor_scroll, vert_scroll, sheet_tabs Outline xlDialogOutline auto_styles, row_dir, col_dir, create_apply Overlay xlDialogOverlay type_num, stack, 100, vary, overlap, drop, hilo, overlap%, cluster, angle, series_num, auto Kiu th xlDialogOverlayChartType type_num Thit lp trang xlDialogPageSetup head, foot, left, right, top, bot, hdng, grid, h_cntr, v_cntr, orient, paper_size, scale, pg_num, pg_order, bw_cells, quality, head_margin, foot_margin, notes, draft xlDialogPageSetup head, foot, left, right, top, bot, size, h_cntr, v_cntr, orient, paper_size, scale, pg_num, bw_chart, quality, head_margin, foot_margin, draft xlDialogPageSetup head, foot, left, right, top, bot, orient, paper_size, scale, quality, head_margin, foot_margin, pg_num Parse xlDialogParse parse_text, destination_ref Dn c bit xlDialogPasteSpecial paste_num, operation_num, skip_blanks, transpose xlDialogPasteSpecial rowcol, titles, categories, replace, series xlDialogPasteSpecial paste_num xlDialogPasteSpecial format_text, pastelink_logical, display_icon_logical, icon_file, icon_number, icon_label Nn xlDialogPatterns apattern, afore, aback, newui xlDialogPatterns lauto, lstyle, lcolor, lwt, hwidth, hlength, htype xlDialogPatterns bauto, bstyle, bcolor, bwt, shadow, aauto, apattern, afore, aback, rounded, newui xlDialogPatterns bauto, bstyle, bcolor, bwt, shadow, aauto, apattern, afore, aback, invert, apply, newfill xlDialogPatterns lauto, lstyle, lcolor, lwt, tmajor, tminor, tlabel xlDialogPatterns lauto, lstyle, lcolor, lwt, apply, smooth xlDialogPatterns lauto, lstyle, lcolor, lwt, mauto, mstyle, mfore, mback, apply, smooth xlDialogPatterns type, picture_units, apply Nhm Pivot xlDialogPivotFieldGroup start, end, by, periods Tnh cht trng Pivot xlDialogPivotFieldProperties name, pivot_field_name, new_name, orientation, function, formats Tch nhm Pivot xlDialogPivotFieldUngroup Hin trang Pivot xlDialogPivotShowPages name, page_field Hng dn nhanh bng Pivot xlDialogPivotTableWizard type, source, destination, name, row_grand, col_grand, save_data, apply_auto_format, auto_page, reserved Placement xlDialogPlacement placement_type In xlDialogPrint range_num, from, to, copies, draft, preview, print_what, color, feed, quality, y_resolution, selection, printer_text, print_to_file, collate Thit lp my in xlDialogPrinterSetup printer_text Xem trc khi xlDialogPrintPreview in Promote xlDialogPromote rowcol Tnh cht xlDialogProperties title, subject, author, keywords, comments Bo v xlDialogProtectDocument contents, windows, password, objects, scenarios Tnh cht nt xlDialogPushbuttonProperties default_logical, cancel_logical, dismiss_logical, help_logical, accel_text, accel_text2 Thay th Font xlDialogReplaceFont font_num, name_text, size_num, bold, italic, underline, strike, color, outline, shadow RoutingSlip xlDialogRoutingSlip recipients, subject, message, route_num, return_logical, status_logical cao hng xlDialogRowHeight height_num, reference, standard_height, type_num Chy... xlDialogRun reference, step Ct tn khc xlDialogSaveAs document_text, type_num, prot_pwd, backup, write_res_pwd, read_only_rec Copy ni vo xlDialogSaveCopyAs document_text Ct to mi xlDialogSaveNewObject CtWorkbook xlDialogSaveWorkbook document_text, type_num, prot_pwd, backup, write_res_pwd, read_only_rec Ct Workspace xlDialogSaveWorkspace name_text Co gin xlDialogScale cross, cat_labels, cat_marks, between, max, reverse xlDialogScale min_num, max_num, major, minor, cross, logarithmic, reverse, max xlDialogScale cat_labels, cat_marks, reverse, between xlDialogScale series_labels, series_marks, reverse xlDialogScale min_num, max_num, major, minor, cross, logarithmic, reverse, min Thm Scenario xlDialogScenarioAdd scen_name, value_array, changing_ref, scen_comment, locked, hidden Cc Scenario xlDialogScenarioCells changing_ref SaScenario xlDialogScenarioEdit scen_name, new_scenname, value_array, changing_ref, scen_comment, locked, hidden Trn Scenario xlDialogScenarioMerge source_file Tng kt Scenario xlDialogScenarioSummary result_ref, report_type Tnh cht thanh cun xlDialogScrollbarProperties value, min, max, inc, page, link, 3d_shading La chn c bit xlDialogSelectSpecial type_num, value_type, levels Gi th xlDialogSendMail recipients, subject, return_receipt Trc xlDialogSeriesAxes axis_num Th t xlDialogSeriesOrder chart_num, old_series_num, new_series_num Vi trc X xlDialogSeriesX x_ref Vi trc Y xlDialogSeriesY name_ref, y_ref t gi tr Ctrl xlDialogSetControlValue value t tiu in xlDialogSetPrintTitles titles_for_cols_ref, titles_for_rows_ref Cp nht trng thi xlDialogSetUpdateStatus link_text, status, type_of_link Xem chi tit xlDialogShowDetail rowcol, rowcol_num, expand, show_field Hin ToolBar xlDialogShowToolbar bar_id, visible, dock, x_pos, y_pos, width, protect, tool_tips, large_buttons, color_buttons C xlDialogSize width, height, window_text Sp xp xlDialogSort orientation, key1, order1, key2, order2, key3, order3, header, custom, case xlDialogSort orientation, key1, order1, type, custom Xp c bit xlDialogSortSpecial sort_by, method, key1, order1, key2, order2, key3, order3, header, order, case Chia xlDialogSplit col_split, row_split Font ngm nh xlDialogStandardFont name_text, size_num, bold, italic, underline, strike, color, outline, shadow rng ngm nh xlDialogStandardWidth standard_num Kiu xlDialogStyle bold, italic SubscribeTo xlDialogSubscribeTo file_text, format_num To tng kt con xlDialogSubtotalCreate at_change_in, function_num, total, replace, pagebreaks, summary_below SummaryInfo xlDialogSummaryInfo title, subject, author, keywords, comments Bng xlDialogTable row_ref, column_ref Th t Tab xlDialogTabOrder Con vt vn bn thnh ct xlDialogTextToColumns destination_ref, data_type, text_delim, consecutive_delim, tab, semicolon, comma, space, other, other_char, field_info Cho hin xlDialogUnhide window_text UpdateLink xlDialogUpdateLink link_text, type_of_link Chn File xlDialogVbaInsertFile filename_text To Add_In xlDialogVbaMakeAddin filename_text nh ngha th tc xlDialogVbaProcedureDefinition Xem 3D xlDialogView3d elevation, perspective, rotation, axes, height%, autoscale Di chuyn ca s xlDialogWindowMove x_pos, y_pos, window_text nh c ca s xlDialogWindowSize width, height, window_text Thm Work book xlDialogWorkbookAdd name_array, dest_book, position_num Cop Work book xlDialogWorkbookCopy name_array, dest_book, position_num Chn Work book xlDialogWorkbookInsert type_num Di chuyn Work book xlDialogWorkbookMove name_array, dest_book, position_num t tn Work book xlDialogWorkbookName oldname_text, newname_text To mi Work xlDialogWorkbookNew book La chn Work book xlDialogWorkbookOptions sheet_name, bound_logical, new_name Bo v Work book xlDialogWorkbookProtect structure, windows, password Chia bng xlDialogWorkbookTabSplit ratio_num Cho hin Work book xlDialogWorkbookUnhide sheet_text Workgroup xlDialogWorkgroup name_array Workspace xlDialogWorkspace fixed, decimals, r1c1, scroll, status, formula, menu_key, remote, entermove, underlines, tools, notes, nav_keys, menu_key_action, drag_drop, show_info Thu phng xlDialogZoom magnification Ni v cch xut d liu ra: Bn c th s dng cc cch sau xut d liu ra. 1. S dng hp thoi c sn MsgBox. 2. S dng Form m mnh thit k thng bo gi tr xut thng qua cc tnh cht i tng thng l cc nhn Label, hp Text. Mt s ngi thng "tit kim" bin ( khng phi l cch lm vic hin i), nn ngay biu thc vo v phi ca lnh gn. Xin bn ng vit v phi l biu thc, m nn to mt bin ghi kt qu, sau a vo tnh cht ca i tng theo cng thc: Tn_Form.Tn_label.Caption = Bin_lu_gi_tr_cn_xut_dng_chui Tn_Form.Tn_TextBox.Text = Bin_lu_gi_tr_cn_xut_dng_chui 3. S dng hp thoi c sn Dialog ca Excel xut. 4. t trc tip gi tr vo ca Excel theo mt trong 2 cch. o Dng cng thc thng: Sheets("Tn Sheet").Cells(hng,ct).Value = Bin_lu_gi_tr_cn_xut o Dng cng thc : Sheets("Tn Sheet").Select V d: Sheets("CSDL").Select Range("Tn ").Select V d: Range("C3").Select ActiveCell.FormulaR1C1 = Bin_lu_gi_tr_cn_xut 1. Xut ra my in bng lnh in hoc hp thoi Dialog v in. (Bi vit ring) 2. Xut vo mt tp TEXT. (Bi vit ring) 3. Xut vo mt chng trnh khc (Bi vit ring) o C th khi ng chng trnh khc bng lnh Shell. Ri dng cc th tc SendKey truyn b sung cc lnh vo. o C th s dng hm API nh WindowsEx. Ri dng th tc SendMessage() hay PosMessage() vo. Bn thn mn! Nh vy chng ta va lm quen 2 phn c bn ca vic lp trnh trong Excel v nhp v xut d liu. Hi vng nhng bi vit sau, chng ta s i su hn v nhng vn nu, ng thi chng ta p dng nhng g c ca Visual Basic. V phn Visual Basic bn c th c rt nhiu ti liu tham kho. Ti xin php c vit v nhng vn m cc ti liu cha cp ti hoc cp nhng rt m nht trong cc bi vit k tip. Xin chn thnh cm n s quan tm ca bn.