Showing posts with label Phần 2. Show all posts
Showing posts with label Phần 2. Show all posts

Sunday, April 14, 2019

EXCEL VBA OBJECTS PHẦN 2 – ĐỐI TƯỢNG WORKBOOK

Bài trước chúng ta đã tìm hiểu về đối tượng (Objects) trong VBA, Workbook là một đối tượng quan trọng trong Excel VBA. Workbook là đối tượng nên nó có thuộc tính (Propertys)  và phương thức(Methods)
Khi bạn tạo một file excel hoặc mở một file excel có sẵn thì nó là một workbook (object)
Khi có nhiều file excel đang được mở thì tập hợp các file đó là workbooks (object)
1. Thuộc tính (Propertys) Workbook
Xác định một workbook bằng cách:
Workbook = workbooks(i)
Nếu bạn muốn lấy chính cái workbook đang thao tác thì
              Workbook = thisWorkbook
– Mở Objects browser lên và tìm đến workbook
ObjectAndprperty-768x411



Propertys, methods of object

Từ cửa sổ trên chúng ta sẽ thấy được các thuộc tính của workbook và phương thức của nó
Ví dụ:
– Thuộc tính name
Private Sub CommandButton1_Click()
MsgBox Workbooks(1).Name
Or
MsgBox ThisWorkbook.Name
End Sub
– Thuộc tính path
Private Sub CommandButton1_Click ()
MsgBox ThisWorkbook.Path
Or
MsgBox Workbooks (“workbook_object1.xls”).Path
End Sub
– Thuộc tính fullname
Private Sub CommandButton1_Click ()
MsgBox ThisWorkbook.FullName
Or
MsgBox Workbooks(“workbook_object1.xls”).Fullname
End Sub
2. Phương thức(methods) của workbook
Các phương pháp của workbook ta hay sử dụng như Save, SaveAs, Open, Close và còn nhiều phương pháp khác
Ví dụ:
Private Sub CommandButton1_Click()
            ‘Mở file
Workbooks.Open (“File Name”)
‘Lưu file
fName = Application.GetSaveAsFilename
ThisWorkbook.SaveAs Filename:=fName
‘Đóng file
Workbooks (i).Close
End Sub

Over 50 Ebooks Excel and VBA free Download

1. Statistics and Probability for Engineering Applications With Microsoft Excel by W.J. DeCoursey - PDF Free Download Download Siz...