N
Thử xem nàoheroic nói:Mong chỉ giáo giúp amator về VBA!
2. Khi rút trích dữ liệu từ một worksheet dùng auto filter thì lọc bỏ được các dòng trống, Vậy có cách nào xoá luôn được cái dòng trống đó bằng VBA không?
Mã:
Sub DeleteBlankRows()
Set myrange = Range("B4:I31")
Set blanks = myrange.SpecialCells(xlCellTypeBlanks)
For Each area In blanks.Areas
If area.Columns.Count = myrange.Columns.Count Then
area.EntireRow.Delete
End If
Next area
End Sub
'The code above is usually adequate, however it could be improved upon.
'Since it processes each row deletion independently, it causes recalcs
'and other little things to happen in the Excel application to slow it
'down a bit. It could be made to be even faster if it built a row
'deletion range and then processed a single delete. The modification
'below explores the idea. You can test it on your data to see how much
'faster it really is. I prefer to use the faster code below.
Sub DeleteBlankRowsEvenFaster()
Set myrange = Range("B4:I31")
Set blanks = myrange.SpecialCells(xlCellTypeBlanks)
For Each area In blanks.Areas
If area.Columns.Count = myrange.Columns.Count Then
n = n + 1
If n = 1 Then
Set delrange = area.EntireRow
Else
Set delrange = Union(delrange, area.EntireRow)
End If
End If
Next area
delrange.Delete
End Sub
'It's also worth noting that both of the examples above error out if
'no blanks are found using the specialcells method. I'll leave it up
'to you to add the appropriate errhandlers.
em test thấy cũng được lém. Seo không chơi BBCODE được nà