Adaptable Copy/Paste to another Sheet

Hobi

New Member
Joined
Jan 18, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hey.

I've come across the common problem that is 'I know what I want to do but I don't know the proper term for searching'.

For this problem I have two sheets ("Source" and "Destination"), and a Command button to copy and paste.

The source data is F4:F198 on sheet "Source". I want it initially pasted to G2:G196 on sheet "Destination".

If G2:G196 is not empty, I want it to paste in H2:H196. If H2:H196 is not empty, then paste into I2:I196 and so on to end at Z2:Z196. Once the data has been successfully pasted once, the loop should end.

The goal is to create something where I can change the data in "Source" F4:F198 and press the button to paste to the next empty "Destination" range.

For bonus points: some basic explanation for what each line does would be appreciated.

Does this make sense? Thanks in advance for any help or advice.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi Hobi and Welcome to the Board! Seems like this will work providing you have data in row 2 of A to F columns (the LastCol part of the code requires it). The resize does the same as copy and paste for the range. HTH. Dave
Code:
Sub test()
Dim Rng As Range, LastCol As Integer
'F4:F198 on sheet "Source"
Set Rng = Sheets("sheet2").Range("F4:F198")
With Sheets("sheet1")
LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
.Cells(2, LastCol + 1).Resize(Rng.Rows.Count, _
            Rng.Columns.Count).Cells.Value = Rng.Cells.Value
End With
End Sub
 
Upvote 0
Solution
Hi Hobi and Welcome to the Board! Seems like this will work providing you have data in row 2 of A to F columns (the LastCol part of the code requires it). The resize does the same as copy and paste for the range. HTH. Dave
Code:
Sub test()
Dim Rng As Range, LastCol As Integer
'F4:F198 on sheet "Source"
Set Rng = Sheets("sheet2").Range("F4:F198")
With Sheets("sheet1")
LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
.Cells(2, LastCol + 1).Resize(Rng.Rows.Count, _
            Rng.Columns.Count).Cells.Value = Rng.Cells.Value
End With
End Sub
This works almost perfectly.

I had some buttons for functionality on the "destination" sheet and initially the code was dumping it to the right of that. Once I removed them, it worked as intended. I'll have to trial and error some new button locations to see where it won't interfere.

More importantly, I now know what definitely works and can do my own reading on things like LastCol and Resize.

Thank you for the help.
 
Upvote 0
You are welcome. Thanks for posting your outcome. Dave
edit: I see that I forgot to change the sheet names to what you had outlined. Source sheet is Sheet2 and Destination sheet is Sheet1. My apologies for any confusion.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,174
Members
452,615
Latest member
bogeys2birdies

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