Copy/Paste Values multiple outputs with different sizes into one single Excel sheet using VBA

daisylee

New Member
Joined
Oct 25, 2023
Messages
2
Platform
  1. Windows
Hi everyone,

I recently started learning VBA on my own. I would like to use VBA to automatically copy different outputs from one Excel file to another one. In the original data file, I have 10 outputs (let's say, A-J) with different sizes/ranges. I want to copy them into one single sheet in a destination file. In the destination file, each output will be pasted to the worksheet called "Final Data", according to the range that I assign.
'copy output A from original data
Workbooks("Original Data").Sheets("A").Activate
lastrow = Cells.Find(What:="*", SearchDirection:=xlPrevious).Row
lastcol = Cells.Find(What:="*", SearchDirection:=xlPrevious).Column

ActiveSheet.Range(Cells(2,1),Cells(lastrow,lastcol)).Copy

'paste output A to sheet "Final Data" in destination file
Workbooks("Destination Data").Sheets("Final Data").Activate

ActiveSheet.Range("B5").PasteSpecial xlPasteValues

I figured out the syntax for copy/paste 1 single output at a time. To improve efficiency, I want to use the loop to reference each of the 10 outputs, then paste them to their assigned range. Is there a way to dynamic change Sheets("A") to "B","C", etc., and also change Range("B5") to "B18" (for destination of output B), "B121" (for destination of output C), etc.? Thank you!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi @daisylee Welcome to the MrExcel forum. Please accept my warmest greetings and sincere hope that all is well.

Adjust the names of your sheets, your books, and the destination cells.
Try this:

VBA Code:
Sub Copy_Values()
  Dim lr As Long, lc As Long, i As Long
  Dim ary As Variant
  Dim wb1 As Workbook
  Dim sh As Worksheet
  
  Set wb1 = Workbooks("Original Data")          'Original book name
  'All the names of your sheets and their respective cell
  ary = Array("A", "B5", "B", "B18", "C", "B121", _
              "D", "B200", "E", "B220", "F", "B225", _
              "G", "B230", "H", "B235", "I", "B240", _
              "J", "B245")
              
  For i = 0 To UBound(ary) Step 2
    Set sh = wb1.Sheets(ary(i))
    lr = sh.Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
    lc = sh.Cells.Find("*", , xlValues, xlPart, xlByColumns, xlPrevious).Column
    sh.Range("A2", sh.Cells(lr, lc)).Copy
    Workbooks("Destination Data").Sheets("Final Data").Range(ary(i + 1)).PasteSpecial xlPasteValues
  Next
End Sub

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 1
Solution

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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