Hi Thầy ơi,
Em muốn sắp xếp tới dòng cuối. Câu lệnh em viết như bên dưới.
Tuy nhiên, Macro chỉ chạy được lệnh kẻ khung, còn lệnh Sắp Xếp không chạy được. Thầy sửa giúp em với ạ.
Em cảm ơn Thầy.
Sub SapXep_KeKhung()
Dim Dongcuoi As Long
Dongcuoi = Sheet1.Range("B" & Rows.Count).End(xlUp).Row
'Buoc 1: Sap xep
Range("A1:B" & Dongcuoi).Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("B2:B" & Dongcuoi) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:B5")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Buoc 2: Ke Khung
Range("A1:B" & Dongcuoi).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous••••ˇˇˇˇ
Chào bạn bạn thử với code sắp xếp nhé
Sub Sapxep()
Dim i As Long, ws As Worksheet
i = Tensheet.Range("C" & Tensheet.Rows.Count).End(xlUp).Row
ActiveWorkbook.Worksheets(Tensheet.Name).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(Tensheet.Name).Sort.SortFields.Add Key:=Range("C8:C" & i), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(Tensheet.Name).Sort
.SetRange Range("C8:R" & i)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Hi Thầy ơi,
Sheet của em chỉ có cột B không có cột C.
Em đã chỉnh lại như bên dưới và kết quả là macro không chạy gì. Thầy xem lại giúp em ạ.
Em cảm ơn Thầy ạ.
Sub SapXep_KeKhung()
Dim a As Long
a = ActiveCell.Row
Sheet1.Range("A" & a + 1).EntireRow.Insert
'Buoc 1: Sap xep
Dim i As Long, ws As Worksheet
i = Sheet1.Range("B" & Sheet1.Rows.Count).End(xlUp).Row
ActiveWorkbook.Worksheets(Sheet1.Name).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(Sheet1.Name).Sort.SortFields.Add Key:=Range("B2:B" & i), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(Sheet1.Name).Sort
.SetRange Range("B2:R" & i)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Buoc 2: Ke Khung
Range("A1:B" & Dongcuoi).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
'Buoc 3: Thong bao hoan thanh
MsgBox ("Thong bao hoan thanh")
Lỗi như hình bên dưới ạ. Mà Code Thầy viết em đọc cũng không hiểu gì luôn. Không biết học những chương sau có code này không ạ.
Chào bạn bạn đẩy file lên giúp gitiho nhé.
https://docs.google.com/spreadsheets/...
Em gửi Thầy nhé ạ.
Chào bạn bạn gửi file có code của ban gitiho mới biết đang bị lỗi ở đâu được
https://docs.google.com/spreadsheets/...
Em gửi lại file nhé Thầy.
Chào bạn bạn chọn lại file giúp gitiho nhé.
https://drive.google.com/file/d/1flGe...
Hi Thầy ơi, em gửi Thầy lại link nhé. Thầy viết lại và giải thích câu lệnh sắp xếp đến dòng cuối giúp em nhé ạ.