Hi,
Had been trying out coding for this.. Here's what i've come up with:
Option Explicit
Sub MergeWorksheets()
Dim sh, ShtDest As Worksheet
Dim StartRow, Last, shtLast As Long
Dim CopyRng As Range
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Delete the sheet "MergedSheet" if it exist
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("MergedSheet").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'Add a worksheet with the name " MergedSheet"
Set ShtDest = ActiveWorkbook.Worksheets.Add
ShtDest.Name = " MergedSheet"
StartRow = 2 'Fill in the start row
'Loop through all worksheets and copy the data to the ShtDest
For Each sh In ActiveWorkbook.Worksheets 'Needs tweaking here, to copy only specific sheets
'Loop through all worksheets except the MergedSheet worksheet and Information worksheet
‘More sheets can be added to the array if required
If IsError(Application.Match(sh.Name, Array(ShtDest.Name, "Information"), 0)) Then
Last = LastRow(ShtDest) 'Find the last row with data on the ShtDest and sh
shtLast = LastRow(sh)
'If sh is not empty and if the last row >= StartRow copy the CopyRng
If shtLast > 0 And shtLast >= StartRow Then
Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shtLast)) 'Set range to copy
'Test if there enough rows in the ShtDest to copy all data
If Last + CopyRng.Rows.Count > ShtDest.Rows.Count Then
MsgBox "There are not enough rows in Destination sheet"
GoTo ExitTSub
End If
CopyRng.Copy
With ShtDest.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
End If
Next
ExitTSub:
Application.GoTo ShtDest.Cells(1)
ShtDest.Columns.AutoFit 'AutoFit column width
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
I'll already have some worksheets in the whole macro that i'm trying to integrate and i'll generate worksheets separately for each user for whom i'll need to choose samples.
The line indicated above in red needs to be tweaked accordingly in such a way that i'll collate all samples chosen for these users to one master sheet. Simply to say, I don't want to export from all worksheets in the workbook. How can this be done? Can anybody help??
Ranga
Bookmarks