简体   繁体   English

将单元格范围从一个工作簿复制到另一个工作簿会产生400错误

[英]Copying range of cells from one workbook to another yields 400 error

Okay, so I am relatively new to Excel VBA. 好的,所以我对Excel VBA相对较新。 I am trying to do something which seems quite simple to me and there are many, many examples of how to do it which I have read exhaustively but I cannot seem to get past this so...here goes. 我正在尝试做一些对我来说似乎很简单的事情,并且有很多关于如何做到这一点的例子,我已经详尽地阅读了这些例子,但是我似乎无法克服这个问题。

I am trying to paste a range of cells from one worksheet to another in Excel Microsoft Office Professional Plus 2010. I think I have reduced the problem to the absolute simplest form possible to illustrate the problem. 我试图将一个工作表中的一系列单元格粘贴到Excel Microsoft Office Professional Plus 2010中。我认为我已将问题简化为可能的绝对简单形式来说明问题。 This is just a snippet. 这只是一个片段。 The VictimResults and TempWorksheet variables are set higher up. VictimResults和TempWorksheet变量设置得更高。 I didn't include the code because I thought it might confuse the articulation of the problem. 我没有包含代码,因为我认为它可能会使问题的表达变得混乱。

Dim SourceWorksheet As Worksheet
Dim TargetWorksheet As Worksheet
Dim SourceRange As Range
Dim TargetRange As Range    

Set SourceWorksheet = VictimResults
Set TargetWorksheet = TempWorksheet
Set SourceRange = Cells(1, 1)
Set TargetRange = Cells(1, 1)    

TargetWorksheet.Range(TargetRange) = SourceWorksheet.Range(SourceRange) I have placed the variables SourceWorksheet, TargetWorksheet, SourceRange, and TargetRange in a watch and set a breakpoint at the last line and they are all valid objects (not null). TargetWorksheet.Range(TargetRange)= SourceWorksheet.Range(SourceRange)我将变量SourceWorksheet,TargetWorksheet,SourceRange和TargetRange放置在手表中,并在最后一行设置断点,它们都是有效对象(不为null)。 When I step over the breakpoint I get a dialog box which simply says "400". 当我越过断点时,会出现一个对话框,上面简单地显示“ 400”。

Any help is much appreciated. 任何帮助深表感谢。

---edit--- I have created this complete VBA file that replicates the problem. ---编辑---我创建了一个完整的VBA文件,该文件复制了该问题。 Thought that might help someone answer. 以为可以帮助某人回答。

Option Explicit

Sub Main()
    GetFirstWorksheetContainsName("Sheet1").Range(Cells(1, 1)).Value = GetFirstWorksheetContainsName("Sheet2").Range(Cells(1, 1)).Value
End Sub

Function GetFirstWorksheetContainsName(worksheetNameContains) As Worksheet
    Dim m As Long
    Dim result As Worksheet

    m = 1

    Do
        If InStr(1, Sheets(m).Name, worksheetNameContains) Then
            Set result = Sheets(m)
            Exit Do
        End If

        m = m + 1
    Loop Until m > ThisWorkbook.Worksheets.Count

    Set GetFirstWorksheetContainsName = result
End Function

Here is something else I tried which yields something a little more verbose. 这是我尝试过的其他方法,可以使工作变得更冗长。

Option Explicit

Sub Main()
    Sheets("Sheet1").Select
    Range(Cells(1, 1)).Select
    Selection.Copy
    Sheets("Sheet2").Select
    Range(Cells(1, 1)).Select
    ActiveSheet.Paste
End Sub

It gives me a "Method 'Range' of object '_Global' failed" error when executing the first Range(Cells(1, 1)).Select line. 当执行第一个Range(Cells(1,1))。Select行时,它给我一个“对象'_Global'的方法'Range'失败”错误。

If you are trying to copy and paste why not use .copy and .pastespecial . 如果您要复制和粘贴,为什么不使用.copy.pastespecial They may slow down your code a little bit but as long as your aren't copying and pasting thousands of things it should be ok. 它们可能会稍微降低您的代码的速度,但是只要您不复制和粘贴成千上万的东西就可以了。

I'm not sure where the 400 is coming from, but the exception that is thrown is the same is in your verbose example (1004 - "Method 'Range' of object '_Worksheet' failed", and is thrown for the same reason. 我不确定400的来源,但是抛出的异常与您的详细示例相同(1004-“对象'_Worksheet'的方法'范围'失败”,并且由于相同的原因而抛出。

The problem is how you're addressing the Range. 问题是您如何处理范围。 Cells(1, 1) is implicitly set to the active worksheet , not whatever range you are passing it to as a parameter. Cells(1, 1)隐式设置为活动工作表 ,而不是将其作为参数传递给的范围。 Since you only need one cell, you can just use the .Cells property instead: 由于只需要一个单元格,因此可以只使用.Cells属性:

Sub Main()

    GetFirstWorksheetContainsName("Sheet1").Cells(1, 1).Value = _
        GetFirstWorksheetContainsName("Sheet2").Cells(1, 1).Value

End Sub

If you need to copy more than one cell, you'll have to either grab a reference to a worksheets instead of inlining the calls to GetFirstWorksheetContainsName if you use dynamic ranges: 如果您需要复制多个单元格,那么在使用动态范围时,您必须获取对工作表的引用,而不是内联对GetFirstWorksheetContainsName的调用:

Sub Main()

    Dim source As Worksheet
    Dim data As Range

    Set source = GetFirstWorksheetContainsName("Sheet2")
    Set data = source.Range("A1:B2")

    GetFirstWorksheetContainsName("Sheet1").Range(data.Address).Value = data.Value

End Sub

Or hard code it: 或对其进行硬编码:

Sub Main()

    GetFirstWorksheetContainsName("Sheet1").Range("A1:B2").Value = _
        GetFirstWorksheetContainsName("Sheet2").Range("A1:B2").Value

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM