Copy and Paste Macro

dgrowe

Board Regular
Joined
Jun 10, 2004
Messages
75
I'd like to copy and paste a range of data using a macro button from one worksheet to another. The data will be variable so I'll need to identify how many rows exist to establish what to copy. I know this is probably fairly easy but I'm a novice with VB code. Thanks.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
If you record a macro performing what you want, then post it back here, someone can set it up dynamically for you.

That way we'll have the sheet names and an idea of the range that's being copied. Just note which column will most consistenly have data in it, because that's how you'll gauge the range size.
 
Upvote 0
This particular data set had 73 lines.


Sub Macro1()
'
' Macro1 Macro
'

'
Sheets("BUD FG").Select
Range("G4:G76").Select
Selection.Copy
Sheets("Sheet4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
 
Upvote 0
Here's one way to do it:

<font face=Calibri><SPAN style="color:#00007F">Sub</SPAN> Macro1()<br>    <SPAN style="color:#00007F">Dim</SPAN> lr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> ws <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <br>    <SPAN style="color:#00007F">Set</SPAN> ws = Sheets("BUD FG")<br>    lr = ws.Cells(Rows.Count, "G").End(xlUp).Row<br>    <br>    ws.Range("G4:G" & lr).Copy<br>    Sheets("Sheet4").Range("A1").PasteSpecial Paste:=xlPasteValues<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

You didn't say what the destination range was, so I just put it in A1. Just adjust it as needed.

HTH,
 
Upvote 0
Is there a way to add a 2nd column to the macro? I'd like to add column D in addition to G.
 
Upvote 0
I'd just add that as a separate condition.

ws.Range("D4:D" & lr).Copy
Paste here

Noting that will use the last cell in G as the ending range for D, so if you need to find the last row in D you'll need to repeat the procedure.

Can you use D:G, or do the columns need to be separate? What should the destinations be?
 
Upvote 0
Ok, I'll give it a try. The columns need to be separate because I don't want to paste the data in between D and G. The destination will be Sheet4 again in cell A1.
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,153
Members
452,891
Latest member
JUSTOUTOFMYREACH

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