Copying and Pasting data into a template

Broccoli

New Member
Joined
Mar 22, 2009
Messages
6
Hi there

I was wondering if the below is possible using VBA?

I have a spreadsheet containing many sheets with different products.

Each Product sheet is comprised as follows:
No header row
Data contained from columns A to T
The rows vary in number

I also have a sheet named Template in the same workbook as the products.
What I'm trying to do is use VBA to copy each of the product tabs and paste them one by one into the Template sheet(paste into cell a3) and then save as a separate workbook based on the contents of cell F3 in the template.

The Template worksheet is comprised as follows:
Blank from columns A to T
Header rows in rows 1 and 2
Formula contained in columns U to Z(starting from row 3)
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Code:
Sub CombineRangefromEachSheet()
'========================================================================
' THIS MERGES ALL ROWS IN COLUMNS A To T FROM ALL WORKSHEETS
' IN WORKBOOK ONTO A "TEMPLATE" SHEET COLUMNS A TO T
'========================================================================
    Dim wsTemplate As Worksheet, SourceSheet As Worksheet
    Dim LRTemplate As Long, LRSource As Long
    Set wsTemplate = Sheets("Template")
    LRTemplate = 3
    For Each SourceSheet In Worksheets
        If SourceSheet.Name <> wsTemplate.Name Then
            LRSource = SourceSheet.Range("A" & Rows.Count).End(xlUp).Row
            SourceSheet.Range("A1:T" & LRSource).Copy wsTemplate.Range("A" & LRTemplate)
            LRTemplate = wsTemplate.Range("A" & Rows.Count).End(xlUp).Row + 1
 
        End If
    Next SourceSheet
End Sub

Hi

Give this code a go. Paste into the "This Workbook" sheet in VBE.

Please let me know how you get on

Mark:)
 
Upvote 0
Mark, Wouldn't your excellant code be better placed in a standard module versus being placed in the "ThisWorkbook" module. Aren't Event modules (Worksheets and ThisWirkbook reserved for EVENT-TYPE procedures? Again, your excellant code is a straight procedure, not involing an Event.

Jim
 
Upvote 0
Jim - Thanks for the hint

I am still working on a "Trial and Error" basis, without fully understanding Modules / Events etc, ie if it works................

Next job today though - look it up and understand the differences!

All the best

Mark
 
Upvote 0
Mark,
All the best to you -- I'm 6 years "trial and error" - and have all the scars to prove it.
I've got over 16 Excel Books and my wife that I sleep with. I'm Glad to help as I have been helped so much over the years by this Board.

Good Luck
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,755
Members
449,049
Latest member
excelknuckles

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