Copy and paste non-contiguous cells in same position

Jeff131313

New Member
Joined
Feb 9, 2011
Messages
32
I want to paste a group of non-contiguous cells, and paste the values elsewhere on the sheet while maintaining the same relative position. is this possible via macro somehow?

for example, in column A i want to copy A1,A2,A5,A9; and paste into D1,D2,D5,D9 in one step.

thanks in advance!
 
Jeff131313, I though shg's idea was worth expanding to make a more general solution (not restricted to a single row) with a mouse selection for the destination.
Since it worked I thought I'd post it.

Code:
Sub Jeff_2()
    Dim rArea As Range, rCopyDest As Range, rStart As Range
    Dim lCol As Long, lRow As Long
    
    Set rStart = Selection.Cells(1, 1)
    Set rCopyDest = Application.InputBox(prompt:="Select Output Cell", Type:=8)
    lCol = rCopyDest.Column - rStart.Column
    lRow = rCopyDest.Row - rStart.Row
    
    For Each rArea In Selection.Areas
        rArea.Copy rArea.Offset(lRow, lCol)
    Next rArea
End Sub
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Jeff131313, I though shg's idea was worth expanding to make a more general solution (not restricted to a single row) with a mouse selection for the destination.
Since it worked I thought I'd post it.

Code:
Sub Jeff_2()
    Dim rArea As Range, rCopyDest As Range, rStart As Range
    Dim lCol As Long, lRow As Long
    
    Set rStart = Selection.Cells(1, 1)
    Set rCopyDest = Application.InputBox(prompt:="Select Output Cell", Type:=8)
    lCol = rCopyDest.Column - rStart.Column
    lRow = rCopyDest.Row - rStart.Row
    
    For Each rArea In Selection.Areas
        rArea.Copy rArea.Offset(lRow, lCol)
    Next rArea
End Sub

Dear Jeff,
You are genius. I am not good at VB, but that code you have given, dear, it works!
I want to learn VB. Can you please guide me through?
I should learn at least for the sake of Excel functioning.
Please help me. Tell me where to start, what to look forward and how to proceed.
Thank you very much for the solution, though!
Please do write me at girishkdesh@gmail.com.
 
Upvote 0
OK ...

Code:
Sub Jeff()
    Dim rArea As Range
    Dim sCol As String

    sCol = InputBox("Enter the column letter(s) to paste to")
    
    For Each rArea In Selection.Areas
        rArea.Copy Intersect(rArea.EntireRow, Columns(sCol))
    Next rArea
End Sub

Thanks Jeff for your support.
I've a question: ould I paste only "value" without formatting by you VBS ? Is there any change to obtain it?
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,854
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top