简体   繁体   中英

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

Okay, so I am relatively new to 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. This is just a snippet. The VictimResults and TempWorksheet variables are set higher up. 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). When I step over the breakpoint I get a dialog box which simply says "400".

Any help is much appreciated.

---edit--- I have created this complete VBA file that replicates the problem. 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.

If you are trying to copy and paste why not use .copy and .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.

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. Since you only need one cell, you can just use the .Cells property instead:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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