Sub DeleteHiddenRows()
For j = 1 To 2542
If Rows(j).Hidden Then
Rows(j).Hidden = False
Rows(j).Select
Selection.EntireRow.Delete
End If
Next j
End Sub
Sub DelHiddenRows()
Dim r As Range, k As Range
With ActiveSheet
Set r = .Range("A1:A" & .Cells.SpecialCells(xlCellTypeLastCell).Row)
Set k = r.SpecialCells(xlCellTypeVisible)
r.EntireRow.Hidden = False
k.EntireRow.Hidden = True
r.SpecialCells(xlCellTypeVisible).EntireRow.Delete
r.EntireRow.Hidden = False
End With
End Sub
Sub DelHiddenRows()
Dim r As Range, k As Range
With ActiveSheet
Set r = .Range("A1:A" & .Cells.SpecialCells(xlCellTypeLastCell).Column)
Set k = r.SpecialCells(xlCellTypeVisible)
r.EntireColumn.Hidden = False
k.EntireColumn.Hidden = True
r.SpecialCells(xlCellTypeVisible).EntireColumn.Delete
r.EntireColumn.Hidden = False
End With
End Sub
Không hẳn là dtới dòng thứ 2542. Tôi thử chỉ vài dòng từ 1 đến 10 thì nó cũng không xóa dòng ẩn cuối cùng.digita đã viết:Code anh Tuấn không xóa hết hàng vì trong code cho biết "mũi Cà Mau" là ở hàng 2542 rồi nếu các hàng ẩn ờ dưới nữa thì vẫn không ảnh hưỡng (tức là không bị xóa).
Bác xem lại Excel của bác bị làm sao... Máy em chạy cái vèo, xóa tuốt, chỉ chừa những hàng không hide thôi.anhtuan1066 đã viết:Nó báo lỗi bạn Digita ơi.. (thông báo lỗi chỉ có duy nhất số 400.. ko biết là lỗi gì).. và nó chỉ Unhide chứ ko xóa gì cả
Chẳng biết nữa.. thử 3 máy tính rồi, đều báo lỗi và ko xóa dc thứ gì cả... Cũng thử trên 2 phiên bản Office: 2002 và 2003BNTT đã viết:Bác xem lại Excel của bác bị làm sao... Máy em chạy cái vèo, xóa tuốt, chỉ chừa những hàng không hide thôi.
anhtuan1066 đã viết:Nó báo lỗi bạn Digita ơi.. (thông báo lỗi chỉ có duy nhất số 400.. ko biết là lỗi gì).. và nó chỉ Unhide chứ ko xóa gì cả
Cái xóa hàng thì được còn cái xóa cột thì không bác Digita ơi (mặc dù đã sửa dùm bác chữ Delete rồi)digita đã viết:Còn xóa các cột ẩn thì là:
Trích:Sub DelHiddenRows()Mến
Dim r As Range, k As Range
With ActiveSheet
Set r = .Range("A1:A" & .Cells.SpecialCells(xlCellTypeLastCell).Column)
Set k = r.SpecialCells(xlCellTypeVisible)
r.EntireColumn.Hidden = False
k.EntireColumn.Hidden = True
r.SpecialCells(xlCellTypeVisible).EntireColumn.Delete
r.EntireColumn.Hidden = False
End With
End Sub
Sub DelHiddenColumns()
For i = Cells.Columns.Count To 1
If Cells(1, i).EntireColumn.Hidden = True Then Cells(1, i).EntireColumn.Delete
Next
End Sub
digita đã viết:OK. Đã kiểm lại 2 code. Cái code 1 xóa hàng thì chạy ngon lành. Còn code 2 đúng là có lổi. Vì chưa nghĩ ra cách dùng theo cách specialcell, mình viết lại cái code xóa các cột ẩn dùng vòng lặp. Tuy chạy hơi lâu 1 chút nhưng thà chậm tí xíu nhưng chắc ăn.
Mến
Sub DelHiddenColumns()
For i = Cells.Columns.Count To 1 Step -1
If Cells(1, i).EntireColumn.Hidden = True Then
Cells(1, i).EntireColumn.Hidden = False
Cells(1, i).EntireColumn.Delete
End If
Next
End Sub
Sub DelHiddenColumns()
For i = Cells.Columns.Count To 1 Step -1
If Cells(1, i).EntireColumn.Hidden Then Cells(1, i).EntireColumn.Delete
Next
End Sub
anhtuan1066 đã viết:OK... cách của minhlev thì chạy tốt... sữa lại chút đỉnh thì thành xóa Row...
Code cuối cùng của Digita cũng chạy tốt... và theo ý bạn Digita thì code của minhlev có thể bỏ bớt dòng:
Cells(1, i).EntireColumn.Hidden = False
digita đã viết:Còn xóa các cột ẩn thì là:
[COLOR=darkblue]Sub[/COLOR] DelHiddenCols()
[COLOR=darkblue]Dim[/COLOR] r [COLOR=darkblue]As[/COLOR] Range, k [COLOR=darkblue]As[/COLOR] Range
[COLOR=darkblue]With[/COLOR] ActiveSheet
[COLOR=darkblue]Set[/COLOR] r = .Range(Cells(1, 1), Cells(1, .Cells.SpecialCells(xlCellTypeLastCell).Column))
[COLOR=darkblue]Set[/COLOR] k = r.SpecialCells(xlCellTypeVisible)
r.EntireColumn.Hidden = [COLOR=darkblue]False[/COLOR]
k.EntireColumn.Hidden = [COLOR=darkblue]True[/COLOR]
r.SpecialCells(xlCellTypeVisible).EntireColumn.Delete
r.EntireColumn.Hidden = [COLOR=darkblue]False[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
Sub DelHiddenRows()
[color=darkblue]Sub[/color] DelHiddenCols()
[color=darkblue]Dim[/color] r [color=darkblue]As[/color] Range, k [color=darkblue]As[/color] Range
[color=darkblue]With[/color] ActiveSheet
[color=darkblue]Set[/color] r = .Range(Cells(1, 1), Cells(1, .Cells.SpecialCells(xlCellTypeLastCell).Column))
[color=darkblue]Set[/color] k = r.SpecialCells(xlCellTypeVisible)
[color=darkblue]If[/color] r.Cells.Count > k.Cells.Count [color=darkblue]Then[/color]
r.EntireColumn.Hidden = [color=darkblue]False[/color]
k.EntireColumn.Hidden = [color=darkblue]True[/color]
r.SpecialCells(xlCellTypeVisible).EntireColumn.Del ete
r.EntireColumn.Hidden = [color=darkblue]False[/color]
[color=darkblue]End[/color] [color=darkblue]If[/color]
[color=darkblue]End[/color] [color=darkblue]With[/color]
[color=darkblue]Set[/color] r = [color=darkblue]Nothing[/color]
[color=darkblue]Set[/color] k = [color=darkblue]Nothing[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
muigele đã viết:Tôi thấy mọi người hay nhắc đến VBA. Vậy VBA là gì? sử dụng như thế nào? Các bạn giải thích giúp tôi nhé
Thân!
Vấn đề không phải là dốt mà dốt mới là vấn đề!![]()
BNTT đã viết:Cái xóa hàng thì được còn cái xóa cột thì không bác Digita ơi (mặc dù đã sửa dùm bác chữ Delete rồi)
Sub DelHiddenRows()
Dim r As Range, k As Range
With ActiveSheet
Set r = .Range("A1:A" & .Cells.SpecialCells(xlCellTypeLastCell).Column)
[COLOR="Red"][B]Set k = r.SpecialCells(xlCellTypeVisible)[/B][/COLOR]
r.EntireColumn.Hidden = False
k.EntireColumn.Hidden = True
r.SpecialCells(xlCellTypeVisible).EntireColumn.Delete
r.EntireColumn.Hidden = False
End With
End Sub
Tunguyen đã viết:Em chạy thử nó báo lỗi này :
Run-time error '1004':
No cells were found.
rồi nó báo lỗi ở hàng này
Mã:Sub DelHiddenRows() Dim r As Range, k As Range With ActiveSheet Set r = .Range("A1:A" & .Cells.SpecialCells(xlCellTypeLastCell).Column) [COLOR="Red"][B]Set k = r.SpecialCells(xlCellTypeVisible)[/B][/COLOR] r.EntireColumn.Hidden = False k.EntireColumn.Hidden = True r.SpecialCells(xlCellTypeVisible).EntireColumn.Delete r.EntireColumn.Hidden = False End With End Sub
Sub DelHiddenRows()
Dim r As Range, k As Range
If Cells.Rows.Count - Cells.SpecialCells(xlCellTypeVisible).Count > 0 Then
With ActiveSheet
Set r = .Range("A1:A" & .Cells.SpecialCells(xlCellTypeLastCell).Row)
Set k = r.SpecialCells(xlCellTypeVisible)
r.EntireRow.Hidden = False
k.EntireRow.Hidden = True
r.SpecialCells(xlCellTypeVisible).EntireRow.Delete
r.EntireRow.Hidden = False
End With
Set r = Nothing
Set k = Nothing
Else
MsgBox "Khong co hang an."
End If
End Sub
anhtuan1066 đã viết:Digita xem lại nha! Nếu vừa có hàng ẩn, vừa có cột ẩn thì code này sẽ báo sai, nó sẽ báo rằng "Không có hàng ẩn"... Vô lý!
Sub DelHiddenRows()
Dim r As Range, k As Range
If Cells.Rows.Count - Cells.SpecialCells(xlCellTypeVisible).Rows.Count > 0 Then
With ActiveSheet
Set r = .Range("A1:A" & .Cells.SpecialCells(xlCellTypeLastCell).Row)
Set k = r.SpecialCells(xlCellTypeVisible)
r.EntireRow.Hidden = False
k.EntireRow.Hidden = True
r.SpecialCells(xlCellTypeVisible).EntireRow.Delete
r.EntireRow.Hidden = False
End With
Set r = Nothing
Set k = Nothing
Else
MsgBox "Khong co hang de xoa."
End If
End Sub
Sub DelHiddenCols()
Dim r As Range, k As Range
If Cells.Columns.Count - Cells.SpecialCells(xlCellTypeVisible).Columns.Count > 0 Then
With ActiveSheet
Set r = .Range(Cells(1, 1), Cells(1, .Cells.SpecialCells(xlCellTypeLastCell).Column))
Set k = r.SpecialCells(xlCellTypeVisible)
If r.Cells.Count > k.Cells.Count Then
r.EntireColumn.Hidden = False
k.EntireColumn.Hidden = True
r.SpecialCells(xlCellTypeVisible).EntireColumn.Delete
r.EntireColumn.Hidden = False
End If
End With
Set r = Nothing
Set k = Nothing
Else
MsgBox "Khong co cot an de xoa."
End If
End Sub