簡體   English   中英

錯誤1004刪除范圍類的方法失敗

[英]Error 1004 Delete method of Range class failed

開始新的分離時,我想刪除特定工作表之間的舊數據。 (在綠色和紅色之間)。 不幸的是,我收到此錯誤消息,無法弄清楚我做錯了什么。

“錯誤1004 Range類的刪除方法失敗”

請幫忙 ! 謝謝。

'-----------------------------
Sub Test()
'-----------------------------
    Dim ws As Worksheet
    Dim lRow As Long, lCol As Long
    Dim Rng As Range
    Dim beginIdx As Integer, endIdx As Integer

    '-- Get the 'Green' and 'Red' indexses in the active workbook .
    beginIdx = ActiveWorkbook.Sheets("Green").Index + 1
    endIdx = ActiveWorkbook.Sheets("Red").Index - 1

    '-- Delete old data between 'Green' and 'Red' tabs
    For J = beginIdx To endIdx

        '-- Set this to the relevant worksheet
        Set ws = ActiveWorkbook.Sheets(J)

        With ws
              '-- Get the last row and last column
              lRow = .UsedRange.SpecialCells(xlCellTypeLastCell).row
              lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

              '-- Set the  sheet range to delete old data leaving the headings intact
              Set Rng = .Range(.Cells(2, 1), .Cells(lRow, lCol))

              Application.DisplayAlerts = False   ' Get rid of pop-up message

              With Rng
                    '-- Now delete the old data from the sheet
                    .EntireRow.Delete
              End With

              Application.DisplayAlerts = True    ' Back to normal
        End With

    Next J

End Sub

現在可以使用。 我只需要包括:
*如果檢查lRow值的語句> 2
*將范圍單元格值從2-> 3增加(設置Rng = .Range(.Cells(3,1)...)

'-----------------------------
Sub Test()
'-----------------------------
    Dim ws As Worksheet
    Dim lRow As Long, lCol As Long
    Dim Rng As Range
    Dim beginIdx As Integer, endIdx As Integer

    '-- Get the 'Green' and 'Red' indexses in the active workbook .
    beginIdx = ActiveWorkbook.Sheets("Green").Index + 1
    endIdx = ActiveWorkbook.Sheets("Red").Index - 1

    '-- Delete old data between 'Green' and 'Red' tabs
    For J = beginIdx To endIdx

        '-- Set this to the relevant worksheet
        Set ws = ActiveWorkbook.Sheets(J)

        With ws
              '-- Get the last row and last column
              lRow = .UsedRange.SpecialCells(xlCellTypeLastCell).row
              lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

              '-- Set the  sheet range to delete old data leaving the headings intact
              If lRow > 2 Then
                   Set Rng = .Range(.Cells(3, 1), .Cells(lRow, lCol))

                   Application.DisplayAlerts = False   ' Get rid of pop-up message

                  With Rng
                        '-- Now delete the old data from the sheet
                        .EntireRow.Delete
                  End With
             End If 

              Application.DisplayAlerts = True    ' Back to normal
        End With

    Next J

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM