Excel VBA运行时错误:方法";“颜色”;“对象的定义”;“内部”;失败

Excel VBA运行时错误:方法";“颜色”;“对象的定义”;“内部”;失败,excel,vba,Excel,Vba,我正在使用在上一个问题中得到帮助的代码:() 我使用以下代码替换列中的项目: 次替换_一次() Application.ScreenUpdating=False LastRow = Range("A" & Rows.Count).End(xlUp).Row Range("A1:A" & LastRow).Interior.ColorIndex = xlNone For Each Cel In Range("B1:B" & LastRow) For

我正在使用在上一个问题中得到帮助的代码:()

我使用以下代码替换列中的项目: 次替换_一次() Application.ScreenUpdating=False

LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A1:A" & LastRow).Interior.ColorIndex = xlNone
    For Each Cel In Range("B1:B" & LastRow)
        For Each C In Range("A1:A" & LastRow)
            If C.Value = Cel.Value And C.Interior.Color <> RGB(200, 200, 200) Then
            C.Interior.Color = RGB(200, 200, 200)
            C.Value = Cel.Offset(0, 1).Value
        End If
    Next
Next

我对你的代码做了一些优化

  • 声明变量/对象
  • 减少了循环时间。早些时候,您的代码循环了
    201924100
    次(14210列A行X 14210列B行)。您不必这样做,因为
    B236
    forwards是空的。现在循环只运行了3339350次。(14210列A列X 235列B列)
  • 整个代码在
    1分53秒内完成。请参阅文章末尾的即时窗口中的输出
  • 试试这个。这对我有用。在Excel 2013中进行了测试

    Sub Replace()
        Dim ws As Worksheet
        Dim A_LRow As Long, B_LRow As Long
        Dim i As Long, j As Long
    
        Application.ScreenUpdating = False
    
        Debug.Print "process started at " & Now
    
        Set ws = ThisWorkbook.Sheets("Sheet1")
    
        With ws
            '~~> Get Col A Last Row
            A_LRow = .Range("A" & .Rows.Count).End(xlUp).Row
            '~~> Get Col B Last Row
            B_LRow = .Range("B" & .Rows.Count).End(xlUp).Row
    
            .Range("A1:A" & A_LRow).Interior.ColorIndex = xlNone
    
            For i = 2 To B_LRow
                For j = 2 To A_LRow
                    If .Range("A" & j).Value = .Range("B" & i).Value And _
                    .Range("A" & j).Interior.Color <> RGB(200, 200, 200) Then
                        .Range("A" & j).Interior.Color = RGB(200, 200, 200)
                        .Range("A" & j).Value = .Range("B" & i).Offset(0, 1).Value
                        DoEvents
                    End If
                Next j
            Next i
        End With
    
        Application.ScreenUpdating = True
    
        Debug.Print "process ended at " & Now
    End Sub
    

    我使用10作为LastRow的编号运行了您的代码,它运行时没有问题。加入一些错误陷阱以获得完整的错误它运行良好,最后一行的数字为10,但当数字接近3800时,错误开始出现。我将其运行到4000,结果正常。可能是工作簿有问题。将数据复制到记事本,然后再复制回excel。重新运行宏。如果失败,请尝试使用GUI手动设置单元格的颜色。你犯错误了吗?示例:On error goto captureError captureError:如果Err.Number>0,则msgbox Err.DescriptionI正在尝试捕获错误。通常excel只是崩溃了,这有点棘手。我会尝试将数据复制到记事本并粘贴回去。这太棒了!你能解释一下这个剧本里发生了什么吗?(或者至少,为什么它比减少生产线更优化?)但我已经做到了;)请看我在文章开头提到的3点。
    Sub Replace()
        Dim ws As Worksheet
        Dim A_LRow As Long, B_LRow As Long
        Dim i As Long, j As Long
    
        Application.ScreenUpdating = False
    
        Debug.Print "process started at " & Now
    
        Set ws = ThisWorkbook.Sheets("Sheet1")
    
        With ws
            '~~> Get Col A Last Row
            A_LRow = .Range("A" & .Rows.Count).End(xlUp).Row
            '~~> Get Col B Last Row
            B_LRow = .Range("B" & .Rows.Count).End(xlUp).Row
    
            .Range("A1:A" & A_LRow).Interior.ColorIndex = xlNone
    
            For i = 2 To B_LRow
                For j = 2 To A_LRow
                    If .Range("A" & j).Value = .Range("B" & i).Value And _
                    .Range("A" & j).Interior.Color <> RGB(200, 200, 200) Then
                        .Range("A" & j).Interior.Color = RGB(200, 200, 200)
                        .Range("A" & j).Value = .Range("B" & i).Offset(0, 1).Value
                        DoEvents
                    End If
                Next j
            Next i
        End With
    
        Application.ScreenUpdating = True
    
        Debug.Print "process ended at " & Now
    End Sub
    
    process started at 10/18/2013 6:29:55 AM
    process ended at 10/18/2013 6:31:48 AM