Skip to content

Instantly share code, notes, and snippets.

Private Sub SpinButton1_Change()
SpinButton1.Min = 1
SpinButton1.Max = Sheet1.Range("F" & Rows.Count).End(xlUp).Value
End Sub
Sub Spinner_getData()
Dim ws As Worksheet 'Thiết lập biến ws là Sheet đang mở
Set ws = ActiveSheet
Dim rngData As Range 'Thiết lập vùng bảng dữ liệu bắt đầu từ ô D3
Set rngData = Range(ws.Range("D3").End(xlDown), ws.Range("D3").End(xlToRight))
Dim Data As Variant 'Xét nội dung trong bảng Dữ liệu
Data = rngData.Value
Dim iCell As Integer 'Vị trí kết quả Spinner
iCell = ws.Range("M2").Value
Sub XuatPDF()
'Tìm dòng cuối bảng kê
Dim maxR As Integer
maxR = Sheet1.Range("F" & Rows.Count).End(xlUp).Value 'Luu ý cột cần xác định ở đây là cột F
'Xác định đường dẫn tới thư mục lưu kết quả
Set xSht = ActiveSheet
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
If xFileDlg.Show = True Then
Sub Sort_NoHeader() 'Excel VBA for a sort (ascending).
Range("A2", Range("G" & Rows.Count).End(xlUp)).Sort [D2], xlAscending
End Sub
Sub Sort_Header() 'Sort in Ascending with Header
Range("A1", Range("G" & Rows.Count).End(xlUp)).Sort [D2], xlAscending, Header:=xlYes
End Sub
Option Explicit
Sub Sort_Multi_Sheet() 'Excel VBA to Sort All Sheets in Workbook in Ascending Order
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Range("A2", ws.Range("G" & Rows.Count).End(xlUp)).Sort [D2], 1
Next ws
End Sub
Sub Loc_BCCT()
'Biện luận trường hợp không phát sinh
If Range("N1").Value = 0 Then
MsgBox "Khong co du lieu phat sinh"
Range("H6:K26").ClearContents
Exit Sub
Else
Application.ScreenUpdating = False
'Xóa dữ liệu cũ trong báo cáo
Range("H6:K26").ClearContents
Sub Xoa_Dong_Trong() 'Xóa dòng trống ở cột A
On Error Resume Next 'Bẫy lỗi không có dòng trống
Range("A3", Range("A65536").End(xlUp)).SpecialCells(4).EntireRow.Delete
On Error GoTo 0 'Nếu xảy ra lỗi thì tới 0
End Sub
Sub Loop_Test01() 'Ví dụ về vòng lặp lấy giá trị cho vùng từ A1 đến A10
Dim i As Integer
For i=1 To 10
Range("A" & i).Value=i
Next i
End Sub
Option Explicit
Sub Loop_Test02() 'Vòng lặp kép 2 vòng
Dim i As Integer
Dim j As Integer
For i = 1 To 2
For j = 2 To 6
If Cells(j, i) = "Book" Then Cells(j, 3) = "Yes"
Next j