Copy and paste data from one row to another VBA

Chewyhairball

Board Regular
Joined
Nov 30, 2017
Messages
97
Hi Folks

I have a table of data and I would like to be able to copy arow of data and paste it into a different row using VBA and a button.
My table goes from columns A-G and infinite rows down.
Here is what I require but cant figure out..

  1. Click anywhere on a row I want to move to adifferent row
  2. Press a button at the top of the page that saysMove Row( I can do this bit J)
  3. The macro highlights columns A-G in the row thencopies.
  4. A message pops up telling you to ‘Select yourdestination Row’
  5. You select any cell in that row then click OKfor the above message.
  6. The data is then pasted into the selected rowand the old date in rows A-G are deleted.
It would be good if it doesn’t matter which column is chosenwhen selecting a row as long as it copies, pastes and deletes rows A-G only.
Also it needs to be copy and paste instead of moving anycells.
I have an old macro that does something a little bit similarbut I cant for the life of me adapt it.
Thanks for any help with this.

Rory

 

Some videos you may like

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Try this "SelectionChange Event" with ActiveX "CommandButton1" control.
The commandbutton1 will Change from "Yes" to "No on selection.
It needs to read "Yes" for the code to run.

With command Button reading "Yes" select any row "A to G" (any column)
This will copy that row.
Select another row (any column), that will then paste the copied row and delete the original line.
Commandbutton1 will then show, "No".
Code:
Private [COLOR="Navy"]Sub[/COLOR] CommandButton1_Click()
CommandButton1.Caption = IIf(CommandButton1.Caption = "Yes", "No", "Yes")
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Code:
Private [COLOR="Navy"]Sub[/COLOR] Worksheet_SelectionChange(ByVal Target [COLOR="Navy"]As[/COLOR] Range)
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range
Static Temp [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]If[/COLOR] CommandButton1.Caption = "Yes" [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]If[/COLOR] Not Intersect(Target, Range("A:G")) [COLOR="Navy"]Is[/COLOR] Nothing And Temp [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]Set[/COLOR] Temp = Range("A" & Target.Row & ":" & "G" & Target.Row)
        Temp.Copy
    [COLOR="Navy"]ElseIf[/COLOR] Not Temp [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
        Range("A" & Target.Row & ":" & "G" & Target.Row).Insert 
        Temp.Delete shift:=xlUp
        [COLOR="Navy"]Set[/COLOR] Temp = Nothing
        CommandButton1.Caption = "No"
    [COLOR="Navy"]Else[/COLOR]
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Last edited:

Chewyhairball

Board Regular
Joined
Nov 30, 2017
Messages
97
Thanks Mick

That works good altho when i select a row below the first selection to paste the data into it actually pastes it into the row above. It works fine if i choose a row above but if i choose a row below it always pastes into the row above.

thanks
 

Chewyhairball

Board Regular
Joined
Nov 30, 2017
Messages
97

ADVERTISEMENT

Hi Mick

I have made an adaption with the following code that is runfrom a normal button:
Sub movecells()
Dim Target As Range
Range(Cells(Selection.Row, 1), Cells(Selection.Row,7)).Select
Set Target =Application.InputBox(Prompt:= _
"Please select a destination", _
Title:="Destination row", Type:=8)
Selection.Copy
Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks_
:=False, Transpose:=False
End Sub
You basically click on the row you want to move then clickthe button. You select the row you wantto move to an click OK. It copies andpastes perfectly. The only thing I cantdo with it is clear the contents from the cell I copied.
I have tried adapting your code and taking bits from it butI am stumped.
Any help would be appreciated.
Thanks

Rory
 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Perhaps this:-
Code:
Private [COLOR="Navy"]Sub[/COLOR] CommandButton1_Click()
[COLOR="Navy"]Dim[/COLOR] Target [COLOR="Navy"]As[/COLOR] Range, Rng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(Cells(Selection.Row, 1), Cells(Selection.Row, 7))
[COLOR="Navy"]If[/COLOR] Not Rng [COLOR="Navy"]Is[/COLOR] Nothing [COLOR="Navy"]Then[/COLOR]
    [COLOR="Navy"]Set[/COLOR] Target = Application.InputBox(Prompt:= _
      "Please select a destination", _
          Title:="Destination row", Type:=8)
     Rng.Copy
     Cells(Target.Row, 1).Insert
    Rng.Delete shift:=xlUp
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 

Chewyhairball

Board Regular
Joined
Nov 30, 2017
Messages
97

ADVERTISEMENT

Thanks Mick
I like that better than the other version. Its pretty much perfect for what I need apartfrom when pasting to a row beneath the selection it still puts in in the rowabove
If I select row 16 it puts it in 15.
If we can figure that out I will be singing and dancing J

 

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Try changing this:-
Code:
Cells(Target.Row, 1).Insert

To this:-
Code:
 Cells(Target.Offset(1).Row, 1).Insert
 

Watch MrExcel Video

Forum statistics

Threads
1,122,514
Messages
5,596,611
Members
414,080
Latest member
penguin23

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
Top