我一直让我的代码所需要的对象。为什么?

问题描述 投票:0回答:1

我很抱歉,如果这是一个非常基本的问题,但我一直在尽力解决这个问题的年龄,不能弄明白。

为什么我的代码保持给人一种必需对象错误?

Sub MatchUp()

    For Each PastCell In Range("A1:A240")
        For Each FutureCell In Range("P1:P240")
            If FutureCell.Value = PastCell.Value Then
                Range(FutureCell.Offset(0, 1), FutureCell.Offset(0, 9)).Cut _
                        Range(PastCell.Offset(0, 15), PastCell.Offset(0.23))
            End If
        Next
    Next

    Application.CutCopyMode = False

End Sub
excel vba
1个回答
0
投票

你有一对夫妇O型的,再加上它的最好避免复制/粘贴。见下文。有这样做的更简洁的方式,但这种(未经测试)的代码应该让你到终点......

Sub MatchUp()
Dim pastCell As Range, FuturCell As Range


    For Each pastCell In Range("A1:A240").Cells
        For Each FutureCell In Range("P1:P240").Cells
            If FutureCell.Value = pastCell.Value Then
                Range(pastCell.Offset(0, 15), pastCell.Offset(0, 23)).Value = Range(FutureCell.Offset(0, 1), FutureCell.Offset(0, 9)).Value
                Range(FuturCell.Offset(0, 1), FuturCell.Offset(0, 9)).ClearContents

            End If
        Next FuturCell
    Next pastCell
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.