VBA查找命令中的可变错误

时间:2016-01-14 11:41:02

标签: excel excel-vba vba

我的代码自发地停止了工作。我尝试执行find命令时收到对象变量错误。那是怎么回事?奇怪的是,它昨天工作正常并且突然......

    Sub Combiner()
Macro1 Macro

With Application                        ' Scrubs settings that slow process
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
    .DisplayAlerts = False
End With

   Dim RangeCells As Range
   Dim CellVal As Range

' Do Loop for renaming Container Numbers

Worksheets("Input").Activate
Range("A1").Select

If Not IsEmpty(ActiveCell.Value) Then

    ' Copy All
        Application.CutCopyMode = False
        Range("A1").Select
        ActiveCell.SpecialCells(xlLastCell).Select
        Selection.End(xlUp).Select
        Range(Selection, Cells(1)).Select
        Selection.Copy

     Worksheets("Backup").Activate
     Range("A1").Activate
     ActiveSheet.Paste

    Worksheets("Input").Activate

    ' Recall UK
    Range("W2").Select
    ActiveCell.FormulaR1C1 = "Recall UK"
    Range("W2").Select
    Selection.Copy
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Paste

    ' Kill Archive Location
    Columns("AA:AA").Select
    Application.CutCopyMode = False
    Selection.ClearContents

       Set RangeCells = Range("Z2:Z201")

       For Each CellVal In RangeCells


          Application.CutCopyMode = False
          CellVal.Select

          Sheets("Source").Activate
          Worksheets("Source").Cells.Find(CellVal, LookAt:=xlWhole).Activate <-- Gets Stuck Here

          Application.CutCopyMode = False
          ActiveCell.Offset(0, 1).Select
          ActiveCell.Copy
          Sheets("Input").Activate
          ActiveSheet.Paste
          Application.CutCopyMode = False

        Next CellVal

    ' Kill title row
        Rows("1:1").Select
        Selection.Delete Shift:=xlUp

    ' Copy All
        ActiveCell.SpecialCells(xlLastCell).Select
        Selection.End(xlUp).Select
        Range(Selection, Cells(1)).Select
        Selection.Copy

End If

With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
    .DisplayAlerts = True
End With

'
End Sub

1 个答案:

答案 0 :(得分:0)

尝试更改线路(卡在哪里):

CellVal.Select
Sheets("Source").Activate
Worksheets("Source").Cells.Find(CellVal, LookAt:=xlWhole).Activate <-- Gets Stuck Here

要:

CellVal.Select
'Setting the CellValue to a variable    
ActualCellValue = CellVal.Value
Sheets("Source").Activate
Worksheets("Source").Cells.Find(CellVal.Value, LookAt:=xlWhole).Activate

我认为对引用不满意。

此外,您真正想要习惯远离SelectSelection。而是设置对RangeWorkSheet使用集的直接引用。您已经将它与 RangeCells CellVal 一起使用,因此您也可以使用Sheets进行此操作。

用于设置对工作表的直接引用并使用直接引用来插入&#34; Recall UK&#34;从W2到W列末尾的单元格,见下文:

Sub SomeSuggestions()

Dim SourceSheet As Worksheet
Dim InputSheet As Worksheet
Dim BackupSheet As Worksheet

Set SourceSheet = ThisWorkbook.Sheets("Source")
Set InputSheet = ThisWorkbook.Sheets("Input")
Set BackupSheet = ThisWorkbook.Sheets("Backup")

'And to Copy Recall UK form W2 to the end of the Column
InputSheet.Range("W2", Columns("W").End(xlDown)).FormulaR1C1 = "Recall UK"

End Sub