Results 1 to 4 of 4

Copying range from a single cell reference

This is a discussion on Copying range from a single cell reference within the Excel Questions forums, part of the Question Forums category; I have the following code: Sub GetUserRange() Dim UserRangeOutput As Range Dim UserRangeInput As Range Dim Output As Range Prompt ...

  1. #1
    Board Regular
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    318

    Default Copying range from a single cell reference

    I have the following code:

    Sub GetUserRange()
    Dim UserRangeOutput As Range
    Dim UserRangeInput As Range
    Dim Output As Range

    Prompt = "Select the cells for the input."
    Title = "Select a cell"

    ' Display the Input Box
    On Error Resume Next
    Set UserRangeInput = Application.InputBox( _
    Prompt:=Prompt, _
    Title:=Title, _
    Default:=ActiveCell.Address, _
    Type:=8) 'Range selection
    On Error GoTo 0

    ' Was the Input Box cancelled?
    If UserRangeInput Is Nothing Then
    MsgBox "Cancelled"
    End If

    Prompt = "Select a cell for the output."
    Title = "Select a cell"

    ' Display the Input Box
    On Error Resume Next
    Set UserRangeOutput = Application.InputBox( _
    Prompt:=Prompt, _
    Title:=Title, _
    Default:=ActiveCell.Address, _
    Type:=8) 'Range selection
    On Error GoTo 0

    ' Was the Input Box cancelled?
    If UserRangeOutput Is Nothing Then
    MsgBox "Cancelled"
    Else
    UserRangeOutput.Range("A1") = UserRangeInput
    End If
    End Sub

    The code works fine if only one cell is selected to copy from, and either a single or multiple cell is selected as the destination.

    However, the code does not work at all if multiple cells are selected as the source - ie nothing is copied into the destination cells.

    How can I change it so that a range (ie multiple cells) is copied, and then pasted into the destination cells?

    Any help greatly appreciated.

    Cheers

    pvr928

  2. #2
    DRJ
    DRJ is offline
    MrExcel MVP DRJ's Avatar
    Join Date
    Feb 2002
    Location
    California
    Posts
    3,856

    Default

    What code are you using to copy and paste?

  3. #3
    Board Regular
    Join Date
    Oct 2002
    Location
    Sydney, Australia
    Posts
    318

    Default Re: Copying range from a single cell reference

    Jacob

    I have just solved my own problem!

    The third last line:

    UserRangeOutput.Range("A1") = UserRangeInput

    Should be:

    UserRangeInput.Copy UserRangeOutput

    Is that what you would have done?

    Cheers

    pvr928

  4. #4
    DRJ
    DRJ is offline
    MrExcel MVP DRJ's Avatar
    Join Date
    Feb 2002
    Location
    California
    Posts
    3,856

    Default

    Yeah, but I like to stick destination in the code since that makes it easier for me to remember what I was doing months later.
    Code:
    UserRangeInput.Copy Destination:=UserRangeOutput
    If you get stuck again post back.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com