This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Private Sub ComboBox1_Change() 'Sự kiện thay đổi danh sách tại ComboBox2 | |
Dim rng As Range 'Tạo biến Vùng danh sách gốc | |
Dim r As Range 'Tạo biến để thực hiện vòng lặp giúp kiểm tra danh sách nạp vào ComboBox2 | |
Dim Dic As Object 'Tạo biến thư viện để gọi đối tượng dictionary | |
Dim ws As Worksheet ' Tạo biến Sheet chứa danh sách gốc | |
Set ws = ActiveSheet 'Sheet chứa danh sách gốc là sheet đang làm việc (khi combobox đặt trong Sheet danh sách) | |
'Nếu khác combobox đặt tại sheet khác thì cần tham chiếu tới sheet chứa danh sách gốc | |
Set rng = ws.Range("A2", ws.Range("A" & Rows.Count).End(xlUp)) 'Vùng danh sách gốc ở cột A, từ ô A2 tới dòng cuối có dữ liệu | |
Set Dic = CreateObject("scripting.dictionary") 'Thiết lập đối tượng dictionary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Private Sub Worksheet_Activate() 'Lấy danh sách không trùng vào ComboBox | |
Dim rng As Range 'Tạo biến Vùng danh sách gốc | |
Dim r As Range 'Tạo biến để thực hiện vòng lặp trong danh sách gốc | |
Dim Dic As Object 'Tạo biến để gọi đối tượng dictionary | |
Dim ws As Worksheet 'Tạo biến Sheet chứa danh sách gốc | |
Set ws = ActiveSheet 'Sheet chứa danh sách gốc là sheet đang làm việc | |
Set rng = ws.Range("A2", ws.Range("A" & Rows.Count).End(xlUp)) 'Vùng danh sách gốc nằm ở cột A, bắt đầu từ ô A2 | |
Set Dic = CreateObject("scripting.dictionary") 'Thiết lập đối tượng dictionary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Phan_mang_Dat_Cong_thuc() 'Đặt công thức cho các mảng bị phân tách | |
Dim lr As Long | |
Dim area As Range | |
Dim sh As Worksheet | |
Set sh = ActiveSheet | |
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row | |
'Cột thành tiền (cột E) nhân 10% thuế để ra kết quả tại cột F | |
For Each area In sh.Range("E2:E" & lr).SpecialCells(xlCellTypeFormulas).Areas | |
area.Offset(, 1).FormulaR1C1 = "=RC[-1]*10%" 'Cột F cách cột E 1 cột nên Offset(,1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Phan_Mang_Data() | |
Dim lr As Long 'Biến dòng cuối | |
Dim area As Range 'Biến mảng chi tiết | |
Dim sh As Worksheet 'Biến Sheet | |
Set sh = ActiveSheet 'Tại sheet đang sử dụng | |
'Tô màu xanh cho các mảng trong cột A | |
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row 'Xác định dòng cuối cột A | |
For Each area In sh.Range("A2:A" & lr).SpecialCells(xlCellTypeConstants).Areas | |
area.Interior.Color = vbGreen 'Tô màu xanh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Tim_so_gan_dung() | |
Dim rng As Range | |
Dim r As Range | |
Dim Mx As Single | |
Dim i As Long | |
Dim Target As Integer | |
Target = Range("E2").Value 'Số làm mẫu tại ô E2 | |
Set rng = Range([A3], Range("A" & Rows.Count).End(xlUp)) 'Phạm vi dãy số ở cột A từ A3 tới dòng cuối có chứa dữ liệu | |
rng.Offset(, 1).ClearContents 'Xóa kết quả tại cột B (cách rgn 1 cột). Đây là nơi sẽ thông báo kết quả của lệnh VBA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub test_RegExp() 'Sử dụng Regular Expression trong VBA | |
Dim RE As Object 'Khai báo biến Regular Expression để sử dụng | |
Dim strPattern As String 'Khai báo biến đại diện cho đoạn chuỗi text cần xử lý bởi RE | |
Dim matches As Object 'Kết quả sau khi thực hiện bởi RE | |
strPattern = "[ABCD]-hoc excel online" 'Thiết lập quy luật hiển thị dữ liệu | |
Set RE = CreateObject("VBScript.RegExp") 'Thiết lập Object cho Regular Expression | |
RE.Pattern = strPattern 'Xác định đoạn chuỗi sẽ xử lý bởi RE | |
RE.Global = True 'Lấy toàn bộ kết quả có được trong chuỗi | |
'Nếu RE.Global = False hoặc không sử dụng thì chỉ lấy được 1 kết quả đúng đầu tiên tìm được |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function SumByCol(range, column) 'Hàm tính tổng theo cột bất kỳ trong mảng | |
With Application.WorksheetFunction | |
SumByRow = .Sum(.Index(range, 0, column)) | |
End With | |
End Function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function SumByRow(range, row) 'Hàm tính tổng theo dòng bất kỳ trong mảng | |
With Application.WorksheetFunction | |
SumByRow = .Sum(.Index(range, row, 0)) | |
End With | |
End Function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function EVAL(range) 'Hàm định dạng lại dữ liệu | |
EVAL = Evaluate(range.Value) | |
End Function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Time_03() 'Xác định thời gian thực hiện câu lệnh | |
Dim t As Date | |
t=Now() 'Thời gian bắt đầu câu lệnh | |
'Nội dung câu lệnh đặt tại đây | |
'Xác định thời gian thực hiện câu lệnh | |
MsgBox Format(Now() - t, "hh:mm:ss") | |
End Sub |