+ Reply to Thread
Results 1 to 4 of 4

Create New Sheets & Copy Specific Data To Each

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Austin
    Posts
    660

    Create New Sheets & Copy Specific Data To Each

    Hi all,

    I'm trying to put together a macro which will first create a new workbook sheet for each
    cell which holds a value within a given range. The new sheets are named after each
    cells value in the range. I got this part to work.

    Next, I want to copy the cell values in each row of another sheet to one of the new sheets, if the value found in col. "B" of the other sheet matches the newly created sheets name. I think I might almost have it. I just need to figure a way to make it loop
    through each row data for each new sheet that's been created.


    Here a Step by Step "Logic" Flow Example:

    Sheet 1. Range("S7").value = "Red
    Sheet 1.Range("S8").value ="Blue"
    Sheet 1.Range("S9").value="Green"

    (3) New sheets are created after values found in Range: Red,Blue,Green

    Sheet2.Range("B25:B").Find("Red,Blue,Green")
    For Each Red in Range Then
    ws.("Red").Cells(LastRow, "C").Value = cell.Offset(0, 1).Value
    ws.("Red").Cells(LastRow, "D").Value = cell.Offset(0, 2).Value
    ws.("Red").Cells(LastRow, "E").Value = cell.Offset(0, 3).Value
    ws.("Red").Cells(LastRow, "F").Value = cell.Offset(0, 4).Value


    For Each Blue in Range Then
    ws.("Blue").Cells(LastRow, "C").Value = cell.Offset(0, 1).Value
    ws.("Blue").Cells(LastRow, "D").Value = cell.Offset(0, 2).Value
    ws.("Blue").Cells(LastRow, "E").Value = cell.Offset(0, 3).Value
    ws.("Blue").Cells(LastRow, "F").Value = cell.Offset(0, 4).Value

    For Each Green in Range Then
    ws.("Green").Cells(LastRow, "C").Value = cell.Offset(0, 1).Value
    ws.("Green").Cells(LastRow, "D").Value = cell.Offset(0, 2).Value
    ws.("Green").Cells(LastRow, "E").Value = cell.Offset(0, 3).Value
    ws.("Green").Cells(LastRow, "F").Value = cell.Offset(0, 4).Value

    Since the number of new sheets created is dependent on the number of cells with a value in the first range given
    I'd actually like to have the data copy to the new sheets within the For loop
    after the sheets is created, if possible.
    The way I don't have to specifically name the new sheet for where the data is to be copied.
    However, I found out you cannot
    have to For Each,Loop within another For Each,Loop.

    So the copying of the data may have to happen after all sheets have been created
    and the process steps out of the sheet creation loop.

    Here's the code I've put together.

    
    Sub Create_NEW_Sheet_And_Copy_Specific_DATA_it ()
    Dim ws As Worksheet
    Dim wsName As String
    Dim r       As Range
    Dim cell    As Range
    Dim rng As Range
    
    Set r = Sheets("Master").Range("S7:S10")
    If r Is Nothing Then Exit Sub
    Application.EnableEvents = False
    For Each cell In r
    If Not IsEmpty(cell) Then
    With cell
       newname = cell.Value
    Set ws = Worksheets.Add
    ws.Move After:=Sheets(Sheets.Count)
    ws.Name = newname
    End With
    End If
    
    Next cell
    
      'For Each sht In ActiveWorkbook.Worksheets
      ' MsgBox sht.Name
    
    With Sheets("BOM")
      Sheets("BOM").Activate
    Set rng = Range("$A$25:$G$" & Cells(Rows.Count, "B").End(xlUp).Row)
    For Each cell In rng
    'On Error Resume Next
    LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row + 7
    If cell.Value = ws.Name Then
    With cell
    ws.Cells(LastRow, "C").Value = cell.Offset(0, 1).Value
    ws.Cells(LastRow, "D").Value = cell.Offset(0, 2).Value
    ws.Cells(LastRow, "E").Value = cell.Offset(0, 3).Value
    ws.Cells(LastRow, "F").Value = cell.Offset(0, 4).Value
    ws.Cells(LastRow, "G").Value = cell.Offset(0, 5).Value
    End With
    End If
    Next cell
    End With
    Any help ,as always is greatly appreciated.

    Thanks,

    BDB
    Last edited by bdb1974; 05-11-2010 at 05:04 PM.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Create New Sheets & Copy Specific Data To Each

    Post a sample workbook, you know it always helps!

  3. #3
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Austin
    Posts
    660

    Re: Create New Sheets & Copy Specific Data To Each

    I'm getting closer.

    This is actually working!!!!
    The correct data is being outputted to the correct sheets.
    However, my data rows on the new sheets are being spaced apart by 7 empty rows.


    
    Sub Create_NEW_Sheet()
    Dim ws As Worksheet
    Dim wsName As String
    Dim r       As Range
    Dim cell    As Range
    Dim rng As Range
    
    Set r = Sheets("Master").Range("S7:S10")
    If r Is Nothing Then Exit Sub
    Application.EnableEvents = False
    For Each cell In r
    If Not IsEmpty(cell) Then
    With cell
       newname = cell.Value
    Set ws = Worksheets.Add
    ws.Move After:=Sheets(Sheets.Count)
    ws.Name = newname
    End With
    End If
    GoTo A:
    B:
    Next cell
    Exit Sub
      'For Each sht In ActiveWorkbook.Worksheets
      ' MsgBox sht.Name
    A:
    With Sheets("BOM")
      Sheets("BOM").Activate
    Set rng = Range("$B$25:$B$" & Cells(Rows.Count, "B").End(xlUp).Row)
    For Each cell In rng
    'On Error Resume Next
    LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row + 7
    If Not ws.Name = cell.Value Then
    GoTo C:
    Else
    If ws.Name = cell.Value Then
    With cell
    ws.Cells(LastRow + 1, "A").Value = cell.Offset(0, -1).Value
    ws.Cells(LastRow + 1, "B").Value = cell.Offset(0, 0).Value
    ws.Cells(LastRow + 1, "C").Value = cell.Offset(0, 1).Value
    ws.Cells(LastRow + 1, "D").Value = cell.Offset(0, 2).Value
    ws.Cells(LastRow + 1, "E").Value = cell.Offset(0, 3).Value
    ws.Cells(LastRow + 1, "F").Value = cell.Offset(0, 4).Value
    ws.Cells(LastRow + 1, "G").Value = cell.Offset(0, 5).Value
    End With
    End If
    End If
    C:
    Next cell
    End With
    GoTo B:
    BDB

  4. #4
    Forum Contributor
    Join Date
    12-10-2008
    Location
    Austin
    Posts
    660

    Re: Create New Sheets & Copy Specific Data To Each

    Ok, I Changed

    LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row + 7
    to

    LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
    This took care of the spacing. But,I'd like my data row to start on row 7.

    BDB

+ 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