+ Reply to Thread
Results 1 to 14 of 14

Excel 2007 : Copying from multiple templates based on criteria.

Hybrid View

  1. #1
    Registered User
    Join Date
    11-23-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    14

    Copying from multiple templates based on criteria.

    I have a summary page where you enter item numbers followed by the item name. I have a worksheet used as a template which I named template >.<. I have a command button that when pressed will make a new sheet copied from the template and name the sheet according to the item. My question is, how can I modify this code to copy from different templates based on criteria that I input next to the item. Say I have Item 1 in A1, Item Name in A2 and Item type in A3. Right now the macro will name the worksheet depending on A2 but will just copy the template worksheet. One more thing, how do I change this code so that I can copy from hidden worksheets? I'm new here so I hope I posted this right.

    Option Explicit
    
    Private Sub CreateSheets_Click()
    Dim strCol As String
    Dim strRow As String
    Dim rngStart As Range
    Dim rngEnd As Range
    Dim rngCell As Range
    Dim strWsName As String
    Dim strSrcName As String
    
    On Error GoTo ErrHnd
    
    'setup column letter and first row number containing names
    'column
    strCol = "Q"
    'row (number is in double quotes)
    strRow = "7"
    
    'turn off screen updating to stop flicker & increase speed
    Application.ScreenUpdating = False
                
    'save this worksheet's name, so we can go back to it later
    strSrcName = ActiveSheet.Name
    
    'set start of data in selected column
    Set rngStart = ActiveSheet.Range(strCol & strRow)
    'find end of data in selected column
    Set rngEnd = ActiveSheet.Range(strCol & CStr(Application.Rows.Count)) _
                .End(xlUp)
    
    'loop through cells in used range
    For Each rngCell In ActiveSheet.Range(rngStart, rngEnd)
        'ignore empty cells in range
        If rngCell.Text <> "" Then
            'get worksheet name
            strWsName = rngCell.Text
            'test if worksheet exists
            On Error Resume Next
             If Worksheets(strWsName) Is Nothing Then
                'worksheet does not exist
                'reinstate error handling
                On Error GoTo ErrHnd
                'copy worksheet named "Template"
                Worksheets("Template").Copy After:=Worksheets(Worksheets.Count)
                'name new sheet
                Worksheets(Worksheets.Count).Name = strWsName
                Else
                'worksheet already exists
                'reinstate error handling
                On Error GoTo ErrHnd
            End If
        End If
    Next rngCell
    
    'go back to the source worksheet
    Worksheets(strSrcName).Activate
    
    'reinstate screen updating
    Application.ScreenUpdating = True
    Exit Sub
    
    'error handler
    ErrHnd:
    Err.Clear
    'go back to the source worksheet
    Worksheets(strSrcName).Activate
    'reinstate screen updating
    Application.ScreenUpdating = True
    
    End Sub
    Last edited by Pololuck; 11-29-2011 at 11:32 AM.

  2. #2
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Copying from multiple templates based on criteria.

    i'd suggest declaring a worksheet variable for ease of use:

    dim whichTemplate as worksheet
    then to point that variable to the right template use this line below (i can't tell which sheet contains the selection - for the purpose of this answer i will assume it's in a sheet named "Summary" and the template selection is sitting in cell a3 of that sheet)

    set whichTemplate = thisworkbook.worksheets(thisworkbook.worksheets("Summary").range("A3").value)
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  3. #3
    Registered User
    Join Date
    11-23-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Copying from multiple templates based on criteria.

    I just started using excel about 3 months ago, if I attach the workbook, do you think you can rewrite the code? I have no idea how to write code, I copied that code from a forum . Thank you for the quick reply though.

  4. #4
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Copying from multiple templates based on criteria.

    sure, i suppose. attach away

  5. #5
    Registered User
    Join Date
    11-23-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Copying from multiple templates based on criteria.

    Here is the attached workbook. If I hide the template it won't copy.
    Attached Files Attached Files

  6. #6
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Copying from multiple templates based on criteria.

    Only change (other than setting your start row to 4) was the following line -- I used this in place of the call to "Template" you had in the attached sheet.

    Worksheets(ThisWorkbook.Worksheets(strSrcName).Cells(rngCell.Row, 3).Value).Copy
    As long as the C column contains the template name, this will work for you.

    SOlution attached.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    11-23-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Copying from multiple templates based on criteria.

    Thank you thank you, one more question though. What do I change for it to copy from these templates if they are hidden?

  8. #8
    Registered User
    Join Date
    11-23-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Copying from multiple templates based on criteria.

    I saw this code in a forum but how would I apply it to my workbook?

    Dim NewPageName As String
    
    Sub NewPage()
    
    Sheets("Master").Visible = True
    Sheets("Master").Copy After:=Worksheets(Worksheets.Count)
    NewPageName = InputBox("What would you like to call your new Worksheet")
    ActiveWindow.ActiveSheet.Name = NewPageName
    Sheets("Master").Visible = False
    
    End Sub

  9. #9
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Copying from multiple templates based on criteria.

    Easiest thing to do was just to unhide all the sheets, run the code, then rehide the templates..

    The macro in the attached sheet will do this. Try it out.
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    11-23-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Copying from multiple templates based on criteria.

    Sorry about last post, I dont know what happened
    For i = 1 To ThisWorkbook.Worksheets.Count
        ThisWorkbook.Worksheets(i).Visible = True
    Next i

  11. #11
    Registered User
    Join Date
    11-23-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Copying from multiple templates based on criteria.

    I get a compile error, variable not defined and it points to the i in
    [code]
    For i = 1 To ThisWorkbook.Worksheets.Count
    ThisWorkbook.Worksheets(i).Visible = True
    Next i[code]

  12. #12
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Copying from multiple templates based on criteria.

    huh, how the heck did that happen...possibly uploaded the wrong sheet. Here's the fix (there was no declaration for the 'i' variable).

    Private Sub CreateSheets_Click()
    Dim strCol As String
    Dim strRow As String
    Dim rngStart As Range
    Dim rngEnd As Range
    Dim rngCell As Range
    Dim strWsName As String
    Dim strSrcName As String
    Dim i As Long
    That last declaration clears things up....attached is corrected sheet.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    11-23-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Copying from multiple templates based on criteria.

    Thank you for your time. I appreciate all the help you've given me.

  14. #14
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Copying from multiple templates based on criteria.

    you are welcome, please mark the post as solved.

    ---edit---
    ha ha, you already marked it! my mistake
    Last edited by GeneralDisarray; 11-29-2011 at 11:35 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