VBA同时复制两个范围"接下来没有For"错误

时间:2014-09-12 17:57:05

标签: vba excel-vba excel

我在下面有这个代码,我将一些范围从工作表复制到另一个。 **中的部分是我认为错误的地方。我想在那里复制两个范围而且我得到了 "接下来没有For"错误。如果我只复制一个范围,它工作正常。知道我做错了什么吗?

Set tempWB = Application.Workbooks.Open(vrtSelectedItem) 'This OPENS the reference workbook

                   With mainWB.Sheets("Sheet1").Range("B16:B30")

                    tempWB.Sheets("Sheet5").Range("B16:B30").Resize(.Rows.Count, _
                                               .Columns.Count).Value = .Value

                   Set tempWB = Application.Workbooks.Open(vrtSelectedItem) 'This OPENS the reference workbook



                   **With mainWB.Sheets("Sheet1").Range("B16:B30")
                    tempWB.Sheets("Sheet5").Range("B16:B30").Resize(.Rows.Count, _
                                               .Columns.Count).Value = .Value

                    With mainWB.Sheets("Sheet1").Range("D16:D30")
                    tempWB.Sheets("Sheet5").Range("D16:D30").Resize(.Rows.Count, _
                                               .Columns.Count).Value = .Value**

                    End With

                    tempWB.Save 'save and close the reference workbook
                    tempWB.Close

           Next vrtSelectedItem

        Else 'The user pressed Cancel.
        End If
    End With

    Set fd = Nothing 'Set the object variable to Nothing.
End Sub


                    End With

                    tempWB.Save 'save and close the reference workbook
                    tempWB.Close

           Next vrtSelectedItem

        Else 'The user pressed Cancel.
        End If
    End With

    Set fd = Nothing 'Set the object variable to Nothing.
End Sub

3 个答案:

答案 0 :(得分:1)

我相信你的问题可能是你的With语句嵌套在其他With语句中而没有正确关闭每个With语句。

虽然可以接受嵌套的With语句,但是外部With块的成员被屏蔽,因此如果要引用外部{{1}的对象的成员,则需要提供完全限定的对象引用。 1}}阻止。

在不使用With语句的情况下尝试,或者只是正确关闭With块。

答案 1 :(得分:0)

您的代码不完整,因此可能不正确。但是以这段代码为例。您有多个With语句没有等量的End With

With mainWB.Sheets("Sheet1").Range("B16:B30")

tempWB.Sheets("Sheet5").Range("B16:B30").Resize(.Rows.Count, .Columns.Count).Value = .Value

Set tempWB = Application.Workbooks.Open(vrtSelectedItem) 'This OPENS the reference workbook

**With mainWB.Sheets("Sheet1").Range("B16:B30")
tempWB.Sheets("Sheet5").Range("B16:B30").Resize(.Rows.Count, _
                           .Columns.Count).Value = .Value

With mainWB.Sheets("Sheet1").Range("D16:D30")
tempWB.Sheets("Sheet5").Range("D16:D30").Resize(.Rows.Count, _
                           .Columns.Count).Value = .Value**

End With

你需要做这样的事情。

With mainWB.Sheets("Sheet1").Range("B16:B30")
    tempWB.Sheets("Sheet5").Range("B16:B30").Resize(.Rows.Count, _
                               .Columns.Count).Value = .Value
End With

Set tempWB = Application.Workbooks.Open(vrtSelectedItem) 'This OPENS the reference workbook

With mainWB.Sheets("Sheet1").Range("B16:B30")
    tempWB.Sheets("Sheet5").Range("B16:B30").Resize(.Rows.Count, _
                           .Columns.Count).Value = .Value
    tempWB.Sheets("Sheet5").Range("D16:D30").Resize(.Rows.Count, _
                           .Columns.Count).Value = .Value**
End With

使用和with结束第一个end with,因为您有2个With声明相同,我们可以将它们合并为一个并正确终止。为了便于阅读,在块中缩进代码也是很好的。

答案 2 :(得分:0)

如果你将“复制值”分解为一个单独的Sub:

就是这样的

    '...
    Dim tempWB As Workbook

    Set tempWB = Application.Workbooks.Open(vrtSelectedItem)

    CopyValues mainWB.Sheets("Sheet1").Range("B16:B30"), _
               tempWB.Sheets("Sheet5").Range("B16")

    CopyValues mainWB.Sheets("Sheet1").Range("D16:D30"), _
               tempWB.Sheets("Sheet5").Range("D16")

    tempWB.Save
    tempWB.Close
    '...

    'copy values only from one range to another
    Sub CopyValues(rngSrc As Range, rngDest As Range)
        With rngSrc
            rngDest.Resize(.Rows.Count, .Columns.Count).Value = .Value
        End With
    End Sub