搜索不同工作簿时,对象必需的运行时错误VBA

时间:2016-04-05 10:21:44

标签: excel vba excel-vba

我对VBA相当新,因此我的很多代码都是我在互联网上研究并整理的代码。我想要实现的目标背景: -

我有两本具有相同布局的作品。一本工作簿是我的原始工作簿,其中包含VBA代码,另一本是一种覆盖文档。我在Overlay中有一个包含代码的列,如果找到该代码,则需要搜索原始工作簿的相同列,然后将整个行从overlay复制到原始行中并删除原始行中的行,如果找不到的话原来只是为了复制行。

我遇到运行时错误的代码行是: -

Set rngFound = Workbooks("OverLay").Worksheets("Overlay").Range("G:G").Find(What:=r.Value, LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

以下是我正在使用的代码的摘录。

Dim sht1 As Worksheet   'Current active worksheet (original version)
Dim sht2 As Worksheet   'Worksheet in OverLay
Dim rngFound As Range

Set sht2 = Workbooks("Overlay").Worksheets("Overlay")

With Workbooks("Original").Worksheets("Formatted")
    lastRow = .Range("G" & .Rows.Count).End(xlUp).Row
End With

With sht2
    For Each Row In .Range("G:G")
        Set rngFound = Workbooks("OverLay").Worksheets("Overlay").Range("G:G").Find(What:=r.Value, LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
        If Not rngFound Is Nothing Then
            rngFound.Copy
            Workbooks("Original").Worksheets("Formatted").Range(rngFound).PasteSpecial
        End If
    Next
End With

1 个答案:

答案 0 :(得分:0)

我首先会向你展示出错:

Dim sht1 As Worksheet '// <~~ This never gets used?
Dim sht2 As Worksheet   'Worksheet in OverLay
Dim rngFound As Range

Set sht2 = Workbooks("Overlay").Worksheets("Overlay")

With Workbooks("Original").Worksheets("Formatted")
    lastRow = .Range("G" & .Rows.Count).End(xlUp).Row
End With

With sht2
    For Each Row In .Range("G:G")
'// 'Row' in the above line will be treated as a variant as it hasn't been declared.
'// As such, it will most likely default to a Range object, which means you are
'// actually looping through each cell in that column. The lesson here is "be explicit"
'// and make sure the code is looking at exactly what you want it to look at.
        Set rngFound = Workbooks("OverLay").Worksheets("Overlay").Range("G:G").Find(What:=r.Value, LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
'// You've already set this sheet to 'sht2' so just use that instead. Also, as
'// we know - 'r' has not been set and so causes an error.
        If Not rngFound Is Nothing Then
            rngFound.Copy
            Workbooks("Original").Worksheets("Formatted").Range(rngFound).PasteSpecial
'// 'rngFound' is already a range object, no need to wrap it in a Range() method.
        End If
    Next
End With

这可以重写为:

Dim originalWS     As Worksheet '// give your variables meaningful names!
Dim overlayWS      As Worksheet
Dim rngSearchParam As Range
Dim rngFound       As Range

Set originalWS = Workbooks("Original").Sheets("Formatted")
Set overlayWS = Workbooks("Overlay").Sheets("Overlay")

With overlayWS
    For Each rngSearchParam In Intersect(.Range("G:G"), .UsedRange)

        Set rngFound = .Range("G:G").Find(rngSearchParam.Value, LookIn:=xlValues, _
            SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)

        If Not rngFound Is Nothing Then
            originalWS.Range(rngFound.Address).Value = rngFound.Value
        End If
    Next
End With

虽然看起来像是在搜索列,但是对于同一列中的单元格定义的值 - 所以不确定“最终目标”是什么。希望它澄清了你一直在遇到的问题