0

我对整个事情和网站有点陌生,最近遇到了我无法解决的代码错误。我的目标是让我的宏能够将包含短语“分包商:”的任何单元格从 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

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

4

1 回答 1

0

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

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
于 2013-03-26T09:48:46.893 回答