我有一个包含数据的很多工作表的Excel工作簿,但我想只保留15行或更多行的工作簿。这是我的代码,但是一旦它以较少的数量命中它就会删除所有内容:
Sub LastRowWithData_xlUp_1()
Dim lastRow As Long
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
For Each Ws In ActiveWorkbook.Worksheets
Application.DisplayAlerts = False
If lastRow < 15 Then Ws.Delete
Next
Application.DisplayAlerts = True
End Sub
答案 0 :(得分:0)
为了答案而复制@ simoco的评论:
移动
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
内循环并将其更改为
lastRow = Ws.Cells(Ws.Rows.Count, "A").End(xlUp).Row