+ Reply to Thread
Results 1 to 2 of 2

VBA Create Sheets & Copy Applicable Data

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA Create Sheets & Copy Applicable Data

    Hi, I wonder whether someone may be able to help me please.

    Using a post which I found here http://www.mrexcel.com/forum/excel-q...worksheet.html

    I've put together a very simple script shown (below) which creates sheets with the sheet name derived from cell values within a given range. The script then copies additional data from the 'Source' sheet and pastes this into the applicable 'Destination' sheet.

    Sub NewWorksheetForEachDept()
    
    Dim WBO As Workbook
    Dim ThisWS
    Dim rngFilter As Range 'filter range
    Dim rngUniques As Range 'Unique Range
    Dim cell As Range
    Dim counter As Integer
    Dim rngResults As Range 'filter range
    Dim LastRow As Long
    Dim Values As Range
    Dim iX As Integer
    
    Set WBO = ThisWorkbook
    Set rngFilter = Range("P4", Range("P" & Rows.Count).End(xlUp))
    Set rngResults = Range("A1", Range("O" & Rows.Count).End(xlUp))
    
    With rngFilter
        .AdvancedFilter Action:=xlFilterInPlace, Unique:=True
        Set rngUniques = Range("P5", Range("P" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
    
    End With
    
    For Each cell In rngUniques
        Worksheets.Add After:=Worksheets(Worksheets.Count)
            ThisWS = cell.Value
                ActiveSheet.Name = ThisWS
                    'counter = counter + 1
                    rngFilter.AutoFilter Field:=1, Criteria1:=cell.Value
                    rngResults.SpecialCells(xlCellTypeVisible).Copy Destination:=WBO.Sheets(ThisWS).Range("A1")
    
                LastRow = Cells(Rows.Count, "B").End(xlUp).Row
                    If LastRow >= StartRow Then
                        With Range("B5:O" & LastRow)
                            .Sort Key1:=.Cells(1, 1), Order1:=xlAscending, Key2:=.Cells(1, 2), order2:=xlAscending
                        End With
                    End If
           Next cell
    End Sub
    The problem I have is that this script runs against the whole workbook, so if there is more than one sheet which contains data when the macro is run, the script naturally has trouble in selecting the correct 'Source' information.

    So I know that I need to specify the 'Source' sheet but I'm a little unsure about how to go about it. I just wondered whether someone may be able to look at this please and offer some guidance on how I may change this so that I can specify the 'Source' sheet as "Unique Records" and create the new worksheets in a new workbook whilst still keeping the rest of the current functionality.

    Many thanks and kind regards

  2. #2
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Create Sheets & Copy Applicable Data

    Hi all, I just wanted to let you know that I've now managed to get this to work.

    I can only assume that there must a typo because using the code above, except for creating the new workbook create the new sheets within, I can extract the correct source data.

    Many thanks and kind regards
    Last edited by hobbiton73; 01-20-2014 at 03:19 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2013, 01:48 PM
  2. Create multiple sheets, copy data send a mail
    By raasukutti in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-22-2012, 11:48 AM
  3. Replies: 10
    Last Post: 02-14-2011, 12:51 PM
  4. Create New Sheets & Copy Specific Data To Each
    By bdb1974 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-11-2010, 05:45 PM
  5. copy applicable data from one sheet to another
    By Onesie in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-13-2009, 04:51 AM

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