简体   繁体   中英

Move a large set of selected data from one workbook to another

I'm trying to write some code to transfer large amounts of user selected data from one open workbook to another. The number of rows/columns varies and the starting point in the destination workbook also varies. I get a debug Run-time error '13': Type mismatch on the last line of code. There's probably a more elegant way to do some things like size the destination range, but everything else seems to be working as expected.

Public SourceWorkbook As Workbook
Public SourceWorksheet As Worksheet
Public DestWorkbook As Workbook
Public DestWorksheet As Worksheet
Public selectedRange As Range
Public selectedRows As Long
Public selectedColumns As Long
Sub SelectRange()
    Set SourceWorkbook = ActiveWorkbook
    Set SourceWorksheet = ActiveSheet
    'Display the Input Box to make selection
    On Error Resume Next
    Set selectedRange = Application.InputBox( _
        Prompt:="Select the range of cells you want to copy.", _
        title:="Select Range", _
        Default:=ActiveCell.Address, _
        Type:=8)
'   Was the Input Box canceled?
    If selectedRange Is Nothing Then
        MsgBox "Cancelled"
        Exit Sub
    End If
    selectedRange.Select
    'Set row and column number variables
    selectedRows = Selection.Rows.Count
    selectedColumns = Selection.Columns.Count
    'vbModeless Form that allows user to select another open workbook and start cell. The OK button simply calls the MoveData sub. 
    SwitchWorkbooksForm.Show vbModeless
End Sub

Sub MoveData()
    Dim DestRange As Range
    Set DestWorkbook = ActiveWorkbook
    Set DestWorksheet = ActiveSheet
'Start the desination range at the user select cell and Set it to the same size as the originally selected range
    StartRow = ActiveCell.Row
    finalRow = StartRow + selectedRows - 1
    StartColumn = ActiveCell.Column
    finalColumn = StartColumn + selectedColumns - 1
    Range(Cells(StartRow, StartColumn), Cells(finalRow, finalColumn)).Select
    Set DestRange = Selection

    'Debug error on the next line
    Workbooks(DestWorkbook).Worksheets(DestWorksheet).Range(DestRange).Value = _
    Workbooks(SourceWorkbook).Worksheets(SourceWorksheet).Range(selectedRange).Value
End Sub

You are using the Workbooks(...) and Worksheets(...) collections by passing in workbook and worksheet objects. You should be passing in names or using the workbook and worksheet references you've assigned directly.

Workbooks(DestWorkbook.name).Worksheets(DestWorksheet.name).Range(DestRange.address).Value = _
   Workbooks(SourceWorkbook.name).Worksheets(SourceWorksheet.name).Range(selectedRange.address).Value

... or,

DestRange = selectedRange.Value

You do not have to keep redefining the parents of a range object you have Set .

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