VBA删除每个工作表中的前7行

时间:2015-12-07 17:39:22

标签: excel vba rows

我正在尝试删除工作簿中包含的每个工作表中的前7行。我有以下代码,但是,我认为循环存在问题,我无法正确执行代码;我将不胜感激任何帮助!

Sub WorksheetLoop()

     Dim WS_Count As Integer
     Dim I As Integer

     ' Set WS_Count equal to the number of worksheets in the active
     ' workbook.
     WS_Count = ActiveWorkbook.Worksheets.Count

     ' Begin the loop.
     For I = 1 To WS_Count

    ActiveWorkbook.Worksheet(I).Rows(8).EntireRow.Delete
     Next I

  End Sub

1 个答案:

答案 0 :(得分:1)

建立这一行:

ActiveWorkbook.Worksheet(I).Rows(8).EntireRow.Delete

到此:

ActiveWorkbook.Worksheets(I).Range("1:7").EntireRow.Delete

另外,要遍历所有工作表,您可以改为:

Sub WorksheetLoop()

    Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        ws.Range("1:7").EntireRow.delete
    Next ws

End Sub