Những thủ thuật trong OFFICE (8)

Chúng ta hãy lập trình bằng VBA (1).

Nguyễn Phương Thảo - Trung tâm Dạy nghề và Phổ cập Tin học Miền núi ABC

525 - Điện Biên - TX. Yên Bái

Bạn thân mến! Qua các bài dạo đầu, chúng ta đã làm quen với kiểu lập trình VBA (Visual Basic For Application) đơn giản. Lập trình VBA có rất nhiều cái lợi, nếu thành thạo ta sẽ có những sản phẩm hơn hẳn lập trình VB (Visual Basic thuần) vì các lý do sau đây:

- Bạn tận dụng được toàn bộ những gì đã học được từ Visual Basic.

- Bạn tận dụng được mọi khả năng mà chương trình Microsoft Office mang lại. Nếu lập trình trong Excel, bạn tận dụng được toàn bộ các thư viện và những công cụ tính toán bất khả chiến bại, đứng đầu thế giới. Ta không phải viết những thuật toán đơn giản, mà chỉ việc "moi" từ chương trình Excel ra sử dụng. (Ta tạm gọi các chương trình này là các chương trình bao). Các chương trình bao như EXCEL, Winword, Power Point, Access, AutoCAD, OutLook... có rất nhiều thư viện DLL đã xây dựng sẵn, đang chờ chúng ta khám phá. Nếu bạn biết tiếng Anh thì đó là những kho vàng kiến thức cho bạn học và... bắt chước. Có rất nhiều ví dụ cực kỳ phong phú và đa dạng ta không thể bỏ qua. Tôi đã dịch hàng năm trời mà vẫn chưa thể hết được kho dữ liệu đồ sộ đó.

- Các chương trình bao đều hỗ trợ các dạng nối kết OLE, DDE đó là mắt xích tuyệt vời để bạn nối kết và chia sẻ dữ liệu, mà đỡ bao công sức lập trình.

Một ví dụ: Đây là sơ đồ đối tượng Microsoft Office:Ta có thể thiết lập, thay đổi, thậm chí cải tổ các phần này dễ như trở bàn tay. Theo sơ đồ này, đối tượng và tập hợp (Collection) được mô tả màu vàng. Còn chỉ có đối tượng thì màu xanh.

Ta thấy mỗi đối tượng và tập hợp đều có công dụng riêng trong Microsoft Ofice Object:

1. Command Bars: Làm việc với thanh công cụ và nút công cụ. (Tất nhiên thanh thực đơn cũng chỉ là thanh công cụ đặc biệt, thanh ShortCut Menu - Hiển thị khi nhấn phím phải - cũng nằm trong số này).

2. Assistant: Làm việc với nhân vật hoạt hình, điều khiển và bắt nó phục vụ mình. (Xem bài lập trình với nhân vật hoạt hình của Office - Nguyễn Phương Thảo)

3. File Search: Làm việc với công cụ tìm kiếm.

4. Document Properties: Làm việc với các tính chất của văn bản.

5. FileFile - Macintosh: Làm việc với hệ điều hành Macitosh. (Phần này tôi chỉ biết qua không dám viết vì không hiểu rõ).

Tại mỗi phần trên đều có rất nhiều phần chi tiết kèm theo. Những phần này rất ít tài liệu đề cập tới.

Tuy nhiên không chỉ có các đối tượng trên mà rất nhiều đối tượng khác. Tôi chưa muốn đưa ra ở bài viết này, vì nếu đưa ra có thể có bạn sẽ choáng ngợp và nó cũng chưa mang lại mục đích gì cả.

Việc lập trình trong VBA thường rất thuận lợi do giao diện chính đã có ở chương trình bao. Bạn chỉ phải lập kế hoạch cho một số công việc chính, sau đó mới đến vấn đề trau chuốt. Các công việc chính gồm:

  1. Giao diện và kiểu cách nhập dữ liệu. Bạn muốn người dùng nhập những dữ liệu gì? Nhập bổ sung vào lúc nào? Nhập vào đâu, vào chính chương trình bao hay nhập vào các hộp InputBox, Form, Dialog, MsgBox?...

  2. Giao diện và kiểu cách xuất dữ liệu. Xuất những gì, xuất vào đâu, vào vị trí trên chương trình bao hay bạn thông báo bằng các form, dialog hay report bởi tệp, kết quả in...

  3. Tìm mối liên hệ giữa giá trị nhập vào với giá trị xuất ra. Có bao nhiêu công thức. Dữ liệu cung cấp cho công thức nhập ở đâu (Từ chương trình bao, hộp Dialog hay yêu cầu nhập thêm. Nếu nhập thêm thì bổ sung vào công việc 1).

Sau khi xây dựng xong kế hoạch, mới bắt tay vào thiết kế. Thiết kế giao diện dễ nhất, nhưng phải có đầu óc quan sát và mỹ thuật cho thật đẹp. Một Form đẹp, cân đối bao giờ cũng chiếm nhiều cảm tình hơn.

Quá trình trau chuốt thường chỉ là thay đổi tính chất (property) của đối tượng mà thôi. Ngay cả vấn đề xuất dữ liệu dạng đơn giản, bạn cũng có thể xuất bằng phương pháp thay đổi tính chất (property) của đối tượng.

Về thay đổi property của đối tượng ta chỉ việc áp dụng công thức truyền thống:

Tên_đối_tương. Tên_tính_chât = Giá_trị_mới

Giá trị mới có thể là giá trị cụ thể hay một biểu thức để ra một giá trị khác. Vấn đề quan trọng là bạn phải hiểu và trả lời được câu hỏi cửa miệng:"Nếu tôi làm việc A, máy phải đáp ứng được công việc B. Muốn máy làm công việc B, tôi phải dùng công thức đối với đối tượng nào, và công thức cần thay đổi tính chất gì của đối tượng đó?"

Để tránh lan man, tôi xin nói về lập trình Excel. Tuy nhiên những bài viết khác, tôi sẽ nói về chương trình khác (Ví dụ như Word, Power Point, AutoCAD, Access, OutLook...).

Các thành phần lập trình VBA trong EXcel

Trong Excel, các thành phần để lập trình được chia làm các chủ đề lớn là:

Nói về cấu trúc chương trình chạy trong Excel

Về cấu trúc chương trình, tất cả các chương trình chúng ta thiết kế trong Excel đều được viết trong Module của tệp dự án. Ở tệp dự án bạn phải đặt tên như tệp Excel thông thường. Rồi vào chế độ lập trình VBA bằng nhiều cách, thường gõ Alt+F11 rồi Ctrl+R để hiện Project Explore.

Chọn tệp dự án của mình và Insert Module. Tại module này ta viết các chương trình ở dạng Sub TEN_THU_TUC. Đây là các thủ tục tự do. Thủ tục tự do giống như bất cứ các thủ tục thông thường mà ta vẫn lập trình bằng Visual Basic. Cũng tương tự với việc bạn chèn Form và viết các thủ tục biến cố của Form.

Tại tệp Project ta còn có thể viết một dạng thủ tục nữa đó là thủ tục biến cố. Ta để ý nó hiện ra như thế này:

Tên biến cố

Thủ tục đáp ứng (Khi đánh nhớ thêm Private vào trước)

A. Thủ tục cho Work Book

  1. Work Book bị kích hoạt

Sub Workbook_Activate()

  • Work Book thêm AddIn

  • Sub Workbook_AddinInstall()

  • Work Book gỡ bỏ AddIn

  • Sub Workbook_AddinUninstall()

  • Work Book trước khi đóng

  • Sub Workbook_BeforeClose(Cancel As Boolean)

  • Work Book trước khi in

  • Sub Workbook_BeforePrint(Cancel As Boolean)

  • Work Book trước khi cất

  • Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

  • Work Book mất Focus

  • Sub Workbook_Deactivate()

  • Work Book thêm Sheet mới

  • Sub Workbook_NewSheet(ByVal Sh As Object)

  • Work Book mở

  • Sub Workbook_Open()

  • Sheet của Work Book được kích hoạt

  • Sub Workbook_SheetActivate(ByVal Sh As Object)

  • Sheet của Work Book trước khi Dclick

  • Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Excel.Range, Cancel As Boolean)

  • Sheet của Work Book trước khi Right click

  • Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Excel.Range, Cancel As Boolean)

  • Sheet của Work Book tính toán

  • Sub Workbook_SheetCalculate(ByVal Sh As Object)

  • Sheet của Work Book thay đổi

  • Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Excel.Range)

  • Sheet của Work Book mất Focus

  • Sub Workbook_SheetDeactivate(ByVal Sh As Object)

  • Sheet của Work Book thay đổi vùng chọn

  • Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)

  • Cửa sổ Work Book được kích hoạt

  • Sub Workbook_WindowActivate(ByVal Wn As Excel.Window)

  • Cửa sổ Work Book thôi kích hoạt

  • Sub Workbook_WindowDeactivate(ByVal Wn As Excel.Window)

  • Cửa sổ Work Book đổi cỡ

  • Sub Workbook_WindowResize(ByVal Wn As Excel.Window)

    B. Thủ tục cho từng Work Sheet riêng (Chọn Sheet cần viết và chọn theo biến cố)

  • Sheet được kích hoạt

  • Sub Worksheet_Activate()

  • Sheet trước khi bị Dclick

  • Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)

  • Sheet trước khi bị RightClick

  • Sub Worksheet_BeforeRightClick(ByVal Target As Excel.Range, Cancel As Boolean)

  • Sheet tính toán

  • Sub Worksheet_Calculate()

  • Sheet thay đổi

  • Sub Worksheet_Change(ByVal Target As Excel.Range)

  • Sheet mất kích hoạt

  • Sub Worksheet_Deactivate()

  • Sheet thay đổi vùng chọn

  • Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

    Tuy nhiên ta không phải đánh chi tiết những biến cố mày. Bạn hãy chọn ở hộp chọn Object rồi chọn biến cố thủ tục ở hộp Procedure. Excel sẽ tự viết cho bạn phần đầu và kết thúc thủ tục. Bạn chỉ việc đánh vào trong lòng thủ tục ý định của mình là xong.

    Ngoài các thủ tục về biến cố Excel cũng như các chương trình Office khác đều có các Macro tự động thực hiện. Các Macro này, bạn chỉ cần dặt tại một module bất kỳ. Bao gồm các Macro sau

    Tên macro

    Công dụng Tự động thực hiện khi

    Auto_Open()

    Mở tệp

    Auto_Exec()

    Vào Excel

    Auto_Close()

    Đóng tệp

    Auto_Exit()

    Thoát

    Auto_New()

    Tạo tệp mới

    Khi có biến cố thuộc các loại trên, thủ tục sẽ tự động thực hiện.

    Nói về nhập giá trị vào các biến trong thủ tục:

    Cũng giống như lập trình Visual Basic thuần ta thường sử dụng các phương pháp sau để nhập giá trị vào các biến trong thủ tục nhằm phục vụ cho việc xử lý:

    1. Sử dụng hàm nhập: (Hàm có sẵn ở Visual Basic) đưa giá trị vào biến

      Biến_Cần_lấy_giá_Trị = INPUTBOX ("Lời nhắc","Tiêu đề lời nhắc","Gía trị đặt sẵn")

    2. Sử dụng hàm hỏi lại (Hàm có sẵn ở Visual Basic) đưa giá trị trả lời vào biến rồi đưa biến vào cấu trúc xử lý

      Biến_Cần_lấy_giá_Trị=MsgBox("Lời nhắc",Nút+Kiểu+Ngầm định,"Tiêu đề")

    3. Lấy giá trị từ một ô trong một sheet bất kỳ của Excel đưa vào biến

      Biến_Cần_lấy_giá_Trị = Sheets("Tên sheet").Cells( hàng, cột)

    4. Lấy giá trị câu móc từ chương trình khác bằng kỹ thuật HOOK (Bài riêng).

    5. Lấy giá trị từ các tính chất của Form mà mình thiết kế.

      Biến_Cần_lấy_giá_Trị = Tên_Form.Tên_Đối_Tượng.Thuộc_tính_cần_lấy

    6. Lấy giá trị thông qua các hộp thoại có sẵn của Excel

    Tên_Biến_DiaLog = Application.Dialogs(Tên_hằng).Show

    Biến_Cần_lấy_giá_Trị = Tên_Biến_DiaLog.Tên_Đôi_Tượng.Thuộc_tính_cần_lấy

    Tuỳ vào giá trị của hằng trong bảng sau mà hộp thoại nào sẽ được trưng lên. Bạn thấy có tới 200 hộp thoại của Excel đó là chưa kể đến có những hộp thoại còn có đến vài cách thể hiện (Đến xỉu luôn!!!).

    Tên hộp thoại hay tab của hộp thoại

    Hằng

    Tham số kèm theo để xác lập

    1. Kích hoạt

    xlDialogActivate

    window_text, pane_num

  • Font của ô

  • xlDialogActiveCellFont

    font, font_style, size, strikethrough, superscript, subscript, outline, shadow, underline, color, normal, background, start_char, char_count

  • Thêm tự động đồ thị Format

  • xlDialogAddChartAutoformat

    name_text, desc_text

  • Điều khiển AddIn

  • xlDialogAddinManager

    operation_num, addinname_text, copy_logical

  • Căn

  • xlDialogAlignment

    horiz_align, wrap, vert_align, orientation, add_indent

  • Áp dụng Tên

  • xlDialogApplyNames

    name_array, ignore, use_rowcol, omit_col, omit_row, order_num, append_last

  • Áp dụng kiểu

  • xlDialogApplyStyle

    style_text

  • Dịch chuyển

  • xlDialogAppMove

    x_num, y_num

  • Thay cỡ

  • xlDialogAppSize

    x_num, y_num

  • Xếp lớp

  • xlDialogArrangeAll

    arrange_num, active_doc, sync_horiz, sync_vert

  • Gán vào đối tượng

  • xlDialogAssignToObject

    macro_ref

  • Gán vào công cụ

  • xlDialogAssignToTool

    bar_id, position, macro_ref

  • Gắn Text

  • xlDialogAttachText

    attach_to_num, series_num, point_num

  • Gắn Thanh công cụ

  • xlDialogAttachToolbars

     

  • Gõ tắt

  • xlDialogAutoCorrect

    correct_initial_caps, capitalize_days

  • Xác định trục

  • 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

  • Tính toán

  • xlDialogCalculation

    type_num, iter, max_num, max_change, update, precision, date_1904, calc_save, save_values, alt_exp, alt_form

  • Bảo vệ

  • xlDialogCellProtection

    locked, hidden

  • Đổi liên kết

  • xlDialogChangeLink

    old_text, new_text, type_of_link

  • Thêm dữ liệu đồ thị

  • xlDialogChartAddData

    ref, rowcol, titles, categories, replace, series

  • Trend đồ thị

  • xlDialogChartTrend

    type, ord_per, forecast, backcast, intercept, equation, r_squared, name

  • Hướng dẫn 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

  • Thuộc tính checkbox

  • xlDialogCheckboxProperties

    value, link, accel_text, accel2_text, 3d_shading

  • Xoá

  • xlDialogClear

    type_num

  • Bảng màu

  • xlDialogColorPalette

    file_text

  • Dộ rộng cột

  • xlDialogColumnWidth

    width_num, reference, standard, type_num, standard_num

  • Combination

  • xlDialogCombination

    type_num

  • Chồng dữ liệu

  • 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

  • Tạo tên mới

  • xlDialogCreateNames

    top, left, bottom, right

  • Tạo Publisher

  • xlDialogCreatePublisher

    file_text, appearance, size, formats

  • Thanh công cụ theo ý muốn

  • xlDialogCustomizeToolbar

    category

  • Xoá dữ liệu

  • xlDialogDataDelete

     

  • Nhãn dữ liệu

  • xlDialogDataLabel

    show_option, auto_text, show_key

  • Chuỗi dữ liệu

  • xlDialogDataSeries

    rowcol, type_num, date_num, step_value, stop_value, trend

  • Đặt tên

  • xlDialogDefineName

    name_text, refers_to, macro_type, shortcut_text, hidden, category, local

  • Đặt kiểu

  • xlDialogDefineStyle

    style_text, number, font, alignment, border, pattern, protection

    xlDialogDefineStyle

    style_text, attribute_num, additional_def_args,...

  • Xoá định dạng

  • xlDialogDeleteFormat

    format_text

  • Xoá tên

  • xlDialogDeleteName

    name_text

  • Demote

  • xlDialogDemote

    row_col

  • Hiện ẩn

  • xlDialogDisplay

    formulas, gridlines, headings, zeros, color_num, reserved, outline, page_breaks, object_num

    xlDialogDisplay

    cell, formula, value, format, protection, names, precedents, dependents, note

  • Tính chất hộp

  • xlDialogEditboxProperties

    validation_num, multiline_logical, vscroll_logical, password_logical

  • Soạn màu

  • xlDialogEditColor

    color_num, red_value, green_value, blue_value

  • Xoá, sửa

  • xlDialogEditDelete

    shift_num

  • Lựa soạn

  • xlDialogEditionOptions

    edition_type, edition_name, reference, option, appearance, size, formats

  • Đặt chuỗi

  • xlDialogEditSeries

    series_num, name_ref, x_ref, y_ref, z_ref, plot_order

  • Báo lỗi bar trục X

  • xlDialogErrorbarX

    include, type, amount, minus

  • Báo lỗi bar trục Y

  • xlDialogErrorbarY

    include, type, amount, minus

  • Khai triển

  • xlDialogExtract

    unique

  • Xoá tệp

  • xlDialogFileDelete

    file_text

  • Tự điền nhóm

  • xlDialogFillGroup

    type_num

  • Tự điền nhóm làm việc

  • xlDialogFillWorkgroup

    type_num

  • Lọc cải tiến

  • xlDialogFilterAdvanced

    operation, list_ref, criteria_ref, copy_ref, unique

  • Tìm tệp

  • xlDialogFindFile

     

  • Chọn Font

  • xlDialogFont

    name_text, size_num

  • Tính chất font

  • xlDialogFontProperties

    font, font_style, size, strikethrough, superscript, subscript, outline, shadow, underline, color, normal, background, start_char, char_count

  • Tự định dạng

  • xlDialogFormatAuto

    format_num, number, font, alignment, border, pattern, width

  • Định dạng đồ 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

  • Kiểu đồ thị định dạng

  • xlDialogFormatCharttype

    apply_to, group_num, dimension, type_num

  • Định dạng 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 dạng chú thích

  • xlDialogFormatLegend

    position_num

  • Định dạng chính

  • xlDialogFormatMain

    type_num, view, overlap, gap_width, vary, drop, hilo, angle, gap_depth, chart_depth, up_down, series_line, labels, doughnut_size

  • Định dạng di chuyển

  • xlDialogFormatMove

    x_offset, y_offset, reference

    xlDialogFormatMove

    x_pos, y_pos

    xlDialogFormatMove

    explosion_num

  • Định dạng số

  • xlDialogFormatNumber

    format_text

  • Định dạng Overlay

  • xlDialogFormatOverlay

    type_num, view, overlap, gap_width, vary, drop, hilo, angle, series_dist, series_num, up_down, series_line, labels, doughnut_size

  • Định dạng cỡ

  • xlDialogFormatSize

    width, height

    xlDialogFormatSize

    x_off, y_off, reference

  • Định dạng văn bản

  • xlDialogFormatText

    x_align, y_align, orient_num, auto_text, auto_size, show_key, show_value, add_indent

  • Tìm công thức

  • xlDialogFormulaFind

    text, in_num, at_num, by_num, dir_num, match_case, match_byte

  • Nhảy đến công thức

  • xlDialogFormulaGoto

    reference, corner

  • Thay thế công thức

  • xlDialogFormulaReplace

    find_text, replace_text, look_at, look_by, active_cell, match_case, match_byte

  • Hướng dẫn nhanh hàm

  • xlDialogFunctionWizard

     

  • Thư viện 3D vùng

  • xlDialogGallery3dArea

    type_num

  • Thư viện 3D thanh

  • xlDialogGallery3dBar

    type_num

  • Thư viện 3D cột

  • xlDialogGallery3dColumn

    type_num

  • Thư viện 3D đường

  • xlDialogGallery3dLine

    type_num

  • Thư viện 3D bánh

  • xlDialogGallery3dPie

    type_num

  • Thư viện 3D bề mặt

  • xlDialogGallery3dSurface

    type_num

  • Thư viện vùng

  • xlDialogGalleryArea

    type_num, delete_overlay

  • Thư viện thanh

  • xlDialogGalleryBar

    type_num, delete_overlay

  • Thư viện cột

  • xlDialogGalleryColumn

    type_num, delete_overlay

  • Thư viện theo ý muốn

  • xlDialogGalleryCustom

    name_text

  • Thư viện Doughnut

  • xlDialogGalleryDoughnut

    type_num, delete_overlay

  • Thư viện đường

  • xlDialogGalleryLine

    type_num, delete_overlay

  • Thư viện bánh

  • xlDialogGalleryPie

    type_num, delete_overlay

  • Thư viện ra đa

  • xlDialogGalleryRadar

    type_num, delete_overlay

  • Thư viện Scatter

  • xlDialogGalleryScatter

    type_num, delete_overlay

  • GoalSeek

  • xlDialogGoalSeek

    target_cell, target_value, variable_cell

  • Đường lưới

  • xlDialogGridlines

    x_major, x_minor, y_major, y_minor, z_major, z_minor, 2D_effect

  • Chèn

  • xlDialogInsert

    shift_num

  • Chèn Objiect

  • xlDialogInsertObject

    object_class, file_name, link_logical, display_icon_logical, icon_file, icon_number, icon_label

  • Chèn đồ hoạ

  • xlDialogInsertPicture

    file_name, filter_number

  • Chèn tiêu đề

  • xlDialogInsertTitle

    chart, y_primary, x_primary, y_secondary, x_secondary

  • Tính chất nhãn

  • xlDialogLabelProperties

    accel_text, accel2_text, 3d_shading

  • Tính chất list box

  • xlDialogListboxProperties

    range, link, drop_size, multi_select, 3d_shading

  • Lựa chọn Macro

  • xlDialogMacroOptions

    macro_name, description, menu_on, menu_text, shortcut_on, shortcut_key, function_category, status_bar_text, help_id, help_file

  • Soạn thư

  • xlDialogMailEditMailer

    to_recipients, cc_recipients, bcc_recipients, subject, enclosures, which_address

  • Vào thư

  • xlDialogMailLogon

    name_text, password_text, download_logical

  • Thư kế

  • xlDialogMailNextLetter

     

  • Đồ thị chính

  • xlDialogMainChart

    type_num, stack, 100, vary, overlap, drop, hilo, overlap%, cluster, angle

  • Kiểu đồ thị chính

  • xlDialogMainChartType

    type_num

  • Soạn sửa Menu

  • xlDialogMenuEditor

     

  • Di chuyển

  • xlDialogMove

    x_pos, y_pos, window_text

  • Tạo mới

  • xlDialogNew

    type_num, xy_series, add_logical

  • Ghi nhớ

  • xlDialogNote

    add_text, cell_ref, start_char, num_chars

  • Tính chất đối tượng

  • xlDialogObjectProperties

    placement_type, print_object

  • Bảo vệ đối tượng

  • 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ở liên kết

  • 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

  • Lựa chọn tính toán

  • xlDialogOptionsCalculation

    type_num, iter, max_num, max_change, update, precision, date_1904, calc_save, save_values

  • Lựa chọn đồ thị

  • xlDialogOptionsChart

    display_blanks, plot_visible, size_with_window

  • Lựa chọn soạn

  • xlDialogOptionsEdit

    incell_edit, drag_drop, alert, entermove, fixed, decimals, copy_objects, update_links, move_direction, autocomplete, animations

  • Lựa chọn 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

  • Lựa chọn List

  • xlDialogOptionsListsAdd

    string_array

    xlDialogOptionsListsAdd

    import_ref, by_row

  • Lựa chọn Transition

  • xlDialogOptionsTransition

    menu_key, menu_key_action, nav_keys, trans_eval, trans_entry

  • Lựa chọn 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

  • Kiểu đồ thị

  • xlDialogOverlayChartType

    type_num

  • Thiết lập 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

  • Dán đặc biệt

  • 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

  • Nền

  • 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

  • Nhóm Pivot

  • xlDialogPivotFieldGroup

    start, end, by, periods

  • Tính chất trường Pivot

  • xlDialogPivotFieldProperties

    name, pivot_field_name, new_name, orientation, function, formats

  • Tách nhóm Pivot

  • xlDialogPivotFieldUngroup

     

  • Hiện trang Pivot

  • xlDialogPivotShowPages

    name, page_field

  • Hướng dẫn nhanh bảng 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

  • Thiết lập máy in

  • xlDialogPrinterSetup

    printer_text

  • Xem trước khi in

  • xlDialogPrintPreview

     

  • Promote

  • xlDialogPromote

    rowcol

  • Tính chất

  • xlDialogProperties

    title, subject, author, keywords, comments

  • Bảo vệ

  • xlDialogProtectDocument

    contents, windows, password, objects, scenarios

  • Tính chất nút

  • 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 hàng

  • xlDialogRowHeight

    height_num, reference, standard_height, type_num

  • Chạy...

  • xlDialogRun

    reference, step

  • Cất tên khác

  • xlDialogSaveAs

    document_text, type_num, prot_pwd, backup, write_res_pwd, read_only_rec

  • Copy nối vào

  • xlDialogSaveCopyAs

    document_text

  • Cất tạo mới

  • xlDialogSaveNewObject

     

  • CấtWorkbook

  • xlDialogSaveWorkbook

    document_text, type_num, prot_pwd, backup, write_res_pwd, read_only_rec

  • Cất Workspace

  • xlDialogSaveWorkspace

    name_text

  • Co giãn

  • 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

  • Thêm Scenario

  • xlDialogScenarioAdd

    scen_name, value_array, changing_ref, scen_comment, locked, hidden

  • Các ô Scenario

  • xlDialogScenarioCells

    changing_ref

  • SửaScenario

  • xlDialogScenarioEdit

    scen_name, new_scenname, value_array, changing_ref, scen_comment, locked, hidden

  • Trộn Scenario

  • xlDialogScenarioMerge

    source_file

  • Tổng kết Scenario

  • xlDialogScenarioSummary

    result_ref, report_type

  • Tính chất thanh cuộn

  • xlDialogScrollbarProperties

    value, min, max, inc, page, link, 3d_shading

  • Lựa chọn đặc biệt

  • xlDialogSelectSpecial

    type_num, value_type, levels

  • Gởi thư

  • xlDialogSendMail

    recipients, subject, return_receipt

  • Trục

  • xlDialogSeriesAxes

    axis_num

  • Thứ tự

  • xlDialogSeriesOrder

    chart_num, old_series_num, new_series_num

  • Với trục X

  • xlDialogSeriesX

    x_ref

  • Với trục Y

  • xlDialogSeriesY

    name_ref, y_ref

  • Đặt giá trị Ctrl

  • xlDialogSetControlValue

    value

  • Đặt tiêu đề in

  • xlDialogSetPrintTitles

    titles_for_cols_ref, titles_for_rows_ref

  • Cập nhật trạng thái

  • xlDialogSetUpdateStatus

    link_text, status, type_of_link

  • Xem chi tiết

  • xlDialogShowDetail

    rowcol, rowcol_num, expand, show_field

  • Hiện ToolBar

  • xlDialogShowToolbar

    bar_id, visible, dock, x_pos, y_pos, width, protect, tool_tips, large_buttons, color_buttons

  • Cỡ

  • xlDialogSize

    width, height, window_text

  • Sắp xếp

  • xlDialogSort

    orientation, key1, order1, key2, order2, key3, order3, header, custom, case

    xlDialogSort

    orientation, key1, order1, type, custom

  • Xếp đặc biệt

  • xlDialogSortSpecial

    sort_by, method, key1, order1, key2, order2, key3, order3, header, order, case

  • Chia

  • xlDialogSplit

    col_split, row_split

  • Font ngầm định

  • xlDialogStandardFont

    name_text, size_num, bold, italic, underline, strike, color, outline, shadow

  • Độ rộng ngầm định

  • xlDialogStandardWidth

    standard_num

  • Kiểu

  • xlDialogStyle

    bold, italic

  • SubscribeTo

  • xlDialogSubscribeTo

    file_text, format_num

  • Tạo tổng kết con

  • xlDialogSubtotalCreate

    at_change_in, function_num, total, replace, pagebreaks, summary_below

  • SummaryInfo

  • xlDialogSummaryInfo

    title, subject, author, keywords, comments

  • Bảng

  • xlDialogTable

    row_ref, column_ref

  • Thứ tự Tab

  • xlDialogTabOrder

     

  • Con vớt văn bản thành cột

  • xlDialogTextToColumns

    destination_ref, data_type, text_delim, consecutive_delim, tab, semicolon, comma, space, other, other_char, field_info

  • Cho hiện

  • xlDialogUnhide

    window_text

  • UpdateLink

  • xlDialogUpdateLink

    link_text, type_of_link

  • Chèn File

  • xlDialogVbaInsertFile

    filename_text

  • Tạo Add_In

  • xlDialogVbaMakeAddin

    filename_text

  • Định nghĩa thủ tục

  • xlDialogVbaProcedureDefinition

     

  • Xem 3D

  • xlDialogView3d

    elevation, perspective, rotation, axes, height%, autoscale

  • Di chuyển cửa sổ

  • xlDialogWindowMove

    x_pos, y_pos, window_text

  • Định cỡ cửa sổ

  • xlDialogWindowSize

    width, height, window_text

  • Thêm Work book

  • xlDialogWorkbookAdd

    name_array, dest_book, position_num

  • Cop Work book

  • xlDialogWorkbookCopy

    name_array, dest_book, position_num

  • Chèn Work book

  • xlDialogWorkbookInsert

    type_num

  • Di chuyển Work book

  • xlDialogWorkbookMove

    name_array, dest_book, position_num

  • Đặt tên Work book

  • xlDialogWorkbookName

    oldname_text, newname_text

  • Tạo mới Work book

  • xlDialogWorkbookNew

     

  • Lựa chọn Work book

  • xlDialogWorkbookOptions

    sheet_name, bound_logical, new_name

  • Bảo vệ Work book

  • xlDialogWorkbookProtect

    structure, windows, password

  • Chia bảng

  • xlDialogWorkbookTabSplit

    ratio_num

  • Cho hiện 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 phóng

  • xlDialogZoom

    magnification

    Nói về cách xuất dữ liệu ra:

    Bạn có thể sử dụng các cách sau để xuất dữ liệu ra.

    1. Sử dụng hộp thoại có sẵn MsgBox.

    2. Sử dụng Form mà mình thiết kế để thông báo giá trị xuất thông qua các tính chất đối tượng thường là các nhãn Label, hộp Text. Một số người thường "tiết kiệm" biến (Đó không phải là cách làm việc hiện đại), nên để ngay biểu thức vào vế phải của lệnh gán. Xin bạn đừng viết vế phải là biểu thức, mà nên tạo một biến để ghi kết quả, sau đó đưa vào tính chất của đối tượng theo công thức:

      Tên_Form.Tên_label.Caption = Biến_lưu_giá_trị_cân_xuất_dạng_chuôi

      Tên_Form.Tên_TextBox.Text = Biến_lưu_giá_trị_cân_xuất_dạng_chuôi

    3. Sử dụng hộp thoại có sẵn Dialog của Excel để xuất.

    4. Đặt trực tiếp giá trị vào ô của Excel theo một trong 2 cách.

    Sheets("Tên Sheet").Cells(hàng,cột).Value = Biến_lưu_giá_trị_cân_xuất

    Sheets("Tên Sheet").Select Ví dụ: Sheets("CSDL").Select

    Range("Tên ô").Select Ví dụ: Range("C3").Select

    ActiveCell.FormulaR1C1 = Biến_lưu_giá_trị_cân_xuất

    1. Xuất ra máy in bằng lệnh in hoặc hộp thoại Dialog về in. (Bài viết riêng)

    2. Xuất vào một tệp TEXT. (Bài viết riêng)

    3. Xuất vào một chương trình khác (Bài viết riêng)

    Bạn thân mến! Như vậy chúng ta vừa đã làm quen 2 phần cơ bản của việc lập trình trong Excel về nhập và xuất dữ liệu. Hi vọng những bài viết sau, chúng ta sẽ đi sâu hơn về những vấn đề đã nêu, đồng thời chúng ta áp dụng những gì đã có của Visual Basic. Về phần Visual Basic bạn có thể đọc rất nhiều tài liệu tham khảo. Tôi xin phép được viết về những vấn đề mà các tài liệu chưa đề cập tới hoặc đề cập nhưng rất mờ nhạt trong các bài viết kế tiếp. Xin chân thành cảm ơn sự quan tâm của bạn.


    PcLeHoan 1996 - 2002
    Mirror : http://www.pclehoan.com
    Mirror : http://www.lehoanpc.net

    Mirror : http://www.ktlehoan.com