VBA maco button for copying specific data

ccharles4432

New Member
Joined
Apr 5, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,
I am very new to excel and VBA. I am trying to:
-Have a button for each row in a sheet called "sheet one".
-On click for one of these buttons, it copies 10 specific cells from the row (say J, H, I, M and L for a given row),
-Putting them in a new entry in a different sheet "sheet two" (same worksheet).

If anyone could cover how I can copy specific cells for a given row and paste them into a different sheet, that would be the best.
Thanks!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Welcome to the Board!

Instead of having a button for each row, wouldn't it be better to just have one button, and have that button prompt the user which row number they would like to copy over?

Also, regarding just copying over specific columns, which columns should they be pasted to?
Are they going to the same columns (i.e. "J" going to "J", "H" going to "H", etc) or are they going in order (i.e."J" going to "A", "H" going to "B", etc)?
And is the new row of data to be placed underneath the current last row of data on Sheet2?
 
Upvote 0
Welcome to the Board!

Instead of having a button for each row, wouldn't it be better to just have one button, and have that button prompt the user which row number they would like to copy over?

Also, regarding just copying over specific columns, which columns should they be pasted to?
Are they going to the same columns (i.e. "J" going to "J", "H" going to "H", etc) or are they going in order (i.e."J" going to "A", "H" going to "B", etc)?
And is the new row of data to be placed underneath the current last row of data on Sheet2?
That could also work! In regards to copying the columns, the locations are arbitrary and don't need to be in the same ones as sheet 1. Also sorry, yes the new row of data is to be placed underneath the last row of data. Thanks heaps!
 
Upvote 0
OK. Try this. You can adjust it to suit your needs (I documented it, so it should hopefully be obvious where you would make the changes).
VBA Code:
Sub MyCopy()

    Dim r As Integer
    Dim nr As Integer
    
'   Prompt user on which row to copy from
    On Error GoTo err_chk
    r = InputBox("Enter row number from Sheet1 to copy from")
    
'   Verify entry
    If r < 1 Or r > Rows.Count Then GoTo err_chk
    On Error GoTo 0
    
'   Use column A on Sheet2 to find last row (*CHANGE IF NEEDED)
    nr = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row + 1
    
'   Copy values from column J on Sheet1 to column A on Sheet2
    Sheets("Sheet2").Cells(nr, "A") = Sheets("Sheet1").Cells(r, "J")
'   Copy values from column H on Sheet1 to column B on Sheet2
    Sheets("Sheet2").Cells(nr, "B") = Sheets("Sheet1").Cells(r, "H")
'   Copy values from column I on Sheet1 to column C on Sheet2
    Sheets("Sheet2").Cells(nr, "C") = Sheets("Sheet1").Cells(r, "I")
'   Copy values from column M on Sheet1 to column D on Sheet2
    Sheets("Sheet2").Cells(nr, "D") = Sheets("Sheet1").Cells(r, "M")
    
    MsgBox "Copy complete!"
    
    Exit Sub
    
err_chk:
    MsgBox "You have not entered a valid row number!", vbOKOnly, "ENTRY ERROR!"
    Exit Sub
    
End Sub
 
Upvote 0
Solution
OK. Try this. You can adjust it to suit your needs (I documented it, so it should hopefully be obvious where you would make the changes).
VBA Code:
Sub MyCopy()

    Dim r As Integer
    Dim nr As Integer
   
'   Prompt user on which row to copy from
    On Error GoTo err_chk
    r = InputBox("Enter row number from Sheet1 to copy from")
   
'   Verify entry
    If r < 1 Or r > Rows.Count Then GoTo err_chk
    On Error GoTo 0
   
'   Use column A on Sheet2 to find last row (*CHANGE IF NEEDED)
    nr = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row + 1
   
'   Copy values from column J on Sheet1 to column A on Sheet2
    Sheets("Sheet2").Cells(nr, "A") = Sheets("Sheet1").Cells(r, "J")
'   Copy values from column H on Sheet1 to column B on Sheet2
    Sheets("Sheet2").Cells(nr, "B") = Sheets("Sheet1").Cells(r, "H")
'   Copy values from column I on Sheet1 to column C on Sheet2
    Sheets("Sheet2").Cells(nr, "C") = Sheets("Sheet1").Cells(r, "I")
'   Copy values from column M on Sheet1 to column D on Sheet2
    Sheets("Sheet2").Cells(nr, "D") = Sheets("Sheet1").Cells(r, "M")
   
    MsgBox "Copy complete!"
   
    Exit Sub
   
err_chk:
    MsgBox "You have not entered a valid row number!", vbOKOnly, "ENTRY ERROR!"
    Exit Sub
   
End Sub
this helps tremendously. thank you very much
 
Upvote 0
You are very welcome!
:)
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,957
Members
452,539
Latest member
delvey

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