+ Reply to Thread
Results 1 to 5 of 5

Random Sample

Hybrid View

  1. #1
    Valued Forum Contributor Saarang84's Avatar
    Join Date
    02-19-2009
    Location
    Chennai, India
    MS-Off Ver
    XL 2003 to 2010
    Posts
    812

    Re: Consolidate Sheets into One after Random Sampling

    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
    Last edited by shg; 10-19-2009 at 12:22 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1