使用“Do Until”遇到错误“运行时错误91”

时间:2013-03-26 08:24:38

标签: excel

我对这整个网站和网站都是新手,最近遇到了我无法解决的代码错误。我的目标是让我的宏能够将包含短语“Subcontractor:”的任何单元从A列切换到列L'单元格,这是偏移量(1,11)。 我的代码允许它这样做,但即使我得到了我想要的结果,但是提供了错误91。

代码:

Sub SiteAccess()
Dim mySheet As Worksheet, myOtherSheet As Worksheet, myBook As Workbook 'Define your workbooks and worksheets as variables
Set myBook = Excel.ActiveWorkbook
Set mySheet = myBook.Sheets("SiteAccessReports")

Dim cc As Excel.Range, ee As Long

Do
For Each cc In Range("A1:A30000") 'Or whatever yours is
If cc.Value Like "*SUBCONTRACTOR : *" Then
cc.Cut Destination:=cc.Offset(1, 11)
For ee = mySheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count - 1 To ActiveSheet.UsedRange.Row Step -1
If Cells(ee, 1) = 0 _
Then Cells(ee, 1).EntireRow.Delete

     Next ee
Exit For
End If
Next
Loop Until cc.Value = "***END OF REPORT*** " 'Error Line

End Sub

感谢我能得到的任何帮助。

1 个答案:

答案 0 :(得分:0)

这可以满足您的需求,但您需要优化初始范围选择,以确保您没有获取任何空值。

Sub SiteAccess()
Dim mySheet As Worksheet, myOtherSheet As Worksheet, myBook As Workbook 'Define your     workbooks and worksheets as variables
Set myBook = Excel.ActiveWorkbook
Set mySheet = myBook.Sheets("SiteAccessReports")

Dim cc As Range

For Each cc In Range("A5:A14") 'Or whatever yours is

    If cc.Value Like "*SUBCONTRACTOR : *" Then

        cc.Cut Destination:=cc.Offset(1, 11)
        Cells(cc.Row - 1, 1).EntireRow.Delete

    End If

    If cc.Value Like "***END OF REPORT***" Then Exit Sub

Next


End Sub