Ошибка 1004. Не удалось удалить метод класса Range.

Когда я начинаю новую сецессию, я хочу удалить старые данные между определенными листами. (Между зеленым и красным). К сожалению, я получаю это сообщение об ошибке и не могу понять, что я делаю неправильно.

«Ошибка 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

person Hycinth    schedule 11.02.2014    source источник
comment
какие значения в ваших lRow и lCol при отладке?   -  person Dmitry Pavliv    schedule 11.02.2014
comment
lRow = 2 и lCol = 38   -  person Hycinth    schedule 11.02.2014
comment
у меня это работает... можете ли вы показать свою книгу (например, с помощью dropbox.com)?   -  person Dmitry Pavliv    schedule 11.02.2014
comment
Это работает и для меня сейчас. Мне просто нужно сделать оператор If в моем коде, чтобы проверить значение lRow. Спасибо тебе ..   -  person Hycinth    schedule 11.02.2014


Ответы (1)


Это работает сейчас. Мне просто нужно включить:
* Оператор If для проверки значения lRow > 2
* Увеличьте значение ячейки Range с 2 -> 3 ( Set 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
person Hycinth    schedule 11.02.2014