pasting non-contiguous range of cells to new row, same cell locati

  • Thread starter Not excelling at macros
  • Start date
N

Not excelling at macros

I need a paste macro that will paste a non-contiguous range of cells (ie
"A1:B1,G1")
to the current row in the same column locations (ie "A5:B5,G5"), assuming
current row is 5th row.

Does anyone have code for this need that you can share?
 
R

Ron de Bruin

Try this

Sub test()
Range("A1:B1").Copy Cells(ActiveCell.Row, "A")
Range("G1").Copy Cells(ActiveCell.Row, "G")
End Sub
 
N

Not excelling at macros

This works great for known source/target rows, but I need it to be dynamic.
So, I click the current row and use a copy macro to select those specific
cells. Then, I need to click on the target row and use a paste macro to
paste to those specific cells.

Your suggestion works great, I just need one more tweak. Any suggestions?

Thanks Ron!
 
R

Ron de Bruin

Try this tester

Sub test()
Dim arr() As String
Dim N As Integer
Dim cell As Range
Dim destrow As Range
Dim selectionrow
Dim I As Integer

If Selection.Rows.Count > 1 Then Exit Sub
selectionrow = Selection.Row
N = 0
For Each cell In Selection
N = N + 1
ReDim Preserve arr(1 To N)
arr(N) = cell.Column
Next cell
On Error Resume Next
Set destrow = Application.InputBox("select a cell in the Destination row", Type:=8)
If destrow Is Nothing Then
On Error GoTo 0
Exit Sub
Else
For I = 1 To N
Cells(destrow.Cells(1).Row, Val(arr(I))) = Cells(selectionrow, Val(arr(I)))
Next I
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top