Combine several Worksheet automatically with a macro?

RCMetrics

Board Regular
Joined
Oct 28, 2005
Messages
95
Hi,

say I have a workbook with 10-15 worksheets

All worksheets have the same headers, they just represent different locations.

What would be the code or macro to copy each one into one worksheet rather than copying and pasting everytime.

This is how the workbook is received and it's on a weekly basis... so getting anoying.

So it would go through each sheet, take line 2 to (what ever the amout there is in the worksheet) and get that into another one, then repeat the process for each worksheet till it has done all of them?

Thanks
 
Thank you. I seem to be getting somewhere now.. I just need to combine your code with the original post code.. If you could suggest how that would be great no problem if not.. Thsk you again..
 
Upvote 0

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
Sub CopyFromWorksheets()
Dim wrk As Workbook 'Workbook object - Always good to work with object variables
Dim sht As Worksheet 'Object for handling worksheets in loop
Dim trg As Worksheet 'Master Worksheet
Dim rng As Range 'Range object
Dim colCount As Integer 'Column count in tables in the worksheets
Dim n
n = 1

Set wrk = ActiveWorkbook 'Working in active workbook

'We don't want screen updating
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
On Error Resume Next
Sheets("Master").Delete

Application.DisplayAlerts = True
'Add new worksheet as the last worksheet
Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count))
'Rename the new worksheet
trg.Name = "Master"
'Get column headers from the first worksheet
'Column count first
Set sht = wrk.Worksheets(1)
colCount = sht.Cells(1, 255).End(xlToLeft).Column + 3
'Now retrieve headers, no copy&paste needed
With trg.Cells(1, 2).Resize(1, colCount)
.Value = sht.Cells(1, 1).Resize(1, colCount).Value
.Font.Bold = True
End With
trg.Cells(1, 1) = "Contractor"
trg.Cells(1, 1).Font.Bold = True
trg.Cells(1, 7) = "Contractor List"
trg.Cells(1, 7).Font.Bold = True

'We can start loop
For Each sht In wrk.Worksheets
'If worksheet in loop is the last one, stop execution (it is Master worksheet)
If sht.Index = wrk.Worksheets.Count Then
Exit For
End If
n = n + 1
'Data range in worksheet - starts from third row as first rows are the header rows in all worksheets
Set rng = sht.Range(sht.Cells(3, 1), sht.Cells(65536, 1).End(xlUp).Resize(, colCount))

'Put data into the Master worksheet
trg.Cells(65536, 2).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
trg.Cells(65536, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, 1).Value = sht.Name
trg.Cells(n, 7).Value = sht.Name
Next sht
'Fit the columns in Master worksheet
trg.Columns.AutoFit

Range("G2").Select
ActiveWindow.FreezePanes = True

'Screen updating should be activated
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True



End Sub
 
Upvote 0
Im really struggling changing this code so it does what I require. CAn any assist?

I need to change it so it ONLY takes data form the second sheet to the last. Also ONLY takes data form column B to Column I and row 30 till the last row with data in.

I have tried for hours to change it but have no reall experience in VBA.

Regards

Steve

How are the sheets named. You state 'second sheet'. Is this the second sheet in the order of tabs (which can be changed) or is this the second sheet such as sheet1, sheet2?

Perry
 
Upvote 0
does this do the job?
Code:
Sub Sample()
Dim wsR As worksheet, i As Integer, flg As Boolean
On Error Resume Next
With ThisWorkbook
     .Sheets("Combined").Delete
On Error GoTo 0
     Set wsR = .Sheets.Add(After:=.Sheets(.Sheets.Count))
     For i = 2 To .Sheets.Count - 1
          If Not flg Then
               wsR.Rows(1).Value = .Sheets(i).Rows(1).Value
                   With wsR
                         .Columns(1).Insert
                         With .Cells(1,1)
                                .Value = "Contractor"
                                .FontBold = Tue
                         End With
                         With .Cells(1,7)
                                 .Value = "Contractor List"
                                 .Font.Bold = True
                         End With
                   End With
               flg = True
          End If
          With .Sheets(i).Range("a1").CurrentRegion
                 With .Resize(.Rows.Count - 1).Offset(1)
                      wsR.Range("a" & Rows.Count).End(xlUp).Offset(1).Resize(.Rows.Count,.Columns.Count).Value = .Value
                 End With
          End With
     Next
End With
End Sub
note: code edited
 
Upvote 0
change

Code:
With .Sheets(i).Range("a1").CurrentRegion

To
Code:
With .Sheets(i).Range("a3").CurrentRegion
 
Upvote 0
Using this code, works ok BUT, if I only want to copy data from cell b30. If one of my sheets has this cell/ row empty the code copies the line above (which is populated).

Sub Sample()
Dim i As Integer
For i = 2 To ThisWorkbook.Sheets.Count
With Sheets(i)
.Range("b1", .Range("b" & Rows.Count).End(xlUp)).Resize(, 8).Copy _
Sheets("Steve").Range("z" & Rows.Count).End(xlUp).Offset(1)
End With
Next
End Sub

Can it be amended?

Thank you
 
Upvote 0
OK, ref to your PM
is this what you want?
Code:
Sub Sample()
Dim i As Integer, LastR As Range
For i = 2 To ThisWorkbook.Sheets.Count
    With Sheets(i)
            Set LastR = .Range("b" & Rows.Count).End(xlUp)
            If LastR.Row > 30 Then
               .Range("b30",LastR).Resize(,8).Copy _
               Sheets("Steve").Range("z" & Rows.Count).End(xlUp).Offset(1)
            End If
    End With
Next
End Sub
 
Upvote 0
It nearly works but only brings data from one or two sheets and misses the others out..Depiste tmeh haing a record in row 30? Strange..
 
Upvote 0

Forum statistics

Threads
1,225,490
Messages
6,185,294
Members
453,285
Latest member
Wullay

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