+ Reply to Thread
Results 1 to 12 of 12

macro to create copy of template based on two criteria and paste data from master sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    04-18-2012
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010
    Posts
    11

    macro to create copy of template based on two criteria and paste data from master sheet

    I have a master sheet which contains several line items and 4 deal templates. I want a macro to loop therough each line item in master sheet and create copy of 1 of the 4 templates depending upon 2 conditions ie asset type and trensaction type ie buy or sell.

    Any help is really appreciated.
    Attached Files Attached Files

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: macro to create copy of template based on two criteria and paste data from master shee

    Hi, deepsi1,

    you should clarify if you want to copy the data into the Columns A to D of the new sheets or into the yellow fields (if so then why are there 2 for Name in the Gsec sheets?).

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    04-18-2012
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: macro to create copy of template based on two criteria and paste data from master shee

    Hi,

    I want to copy the data in Sheet1 in Columns A to D in New Sheets in Yellow Marked Fields in newly copied sheets.

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: macro to create copy of template based on two criteria and paste data from master shee

    Hi, deepsi1,

    give this a try:
    Option Explicit
    
    Sub EF920361()
    Dim wsMaster As Worksheet
    Dim wsCopy As Worksheet
    Dim lngLast As Long
    Dim lngCounter As Long
    Dim lngArr As Long
    Dim blnPlus As Boolean
    Dim varArr1
    Dim varArr2
    Dim varArr
    
    varArr1 = Array("B2", "B4", "B7", "C7")
    varArr2 = Array("B2", "B4", "B8", "C8")
    
    Set wsMaster = Sheets("Sheet1")
    lngLast = wsMaster.Cells(Rows.Count, "E").End(xlUp).Row
    
    For lngCounter = 2 To lngLast
      Select Case wsMaster.Cells(lngCounter, "E").Value
        Case "GSE", "GGB", "ZCG", "TBL"
          blnPlus = True
          If wsMaster.Cells(lngCounter, "F").Value = "P" Then
             Sheets("Gsec Pur").Copy After:=Worksheets(Worksheets.Count)
             Set wsCopy = ActiveSheet
          Else
            Sheets("Gsec Sale").Copy After:=Worksheets(Worksheets.Count)
            Set wsCopy = ActiveSheet
          End If
        Case Else
          If wsMaster.Cells(lngCounter, "F").Value = "P" Then
             Sheets("Buy").Copy After:=Worksheets(Worksheets.Count)
             Set wsCopy = ActiveSheet
          Else
            Sheets("Sale").Copy After:=Worksheets(Worksheets.Count)
            Set wsCopy = ActiveSheet
          End If
      End Select
      
      wsCopy.Name = wsMaster.Cells(lngCounter, "A").Value
      If blnPlus Then
        varArr = varArr2
      Else
        varArr = varArr1
      End If
      
      For lngArr = LBound(varArr) To UBound(varArr)
        wsCopy.Range(varArr(lngArr)).Value = wsMaster.Cells(lngCounter, lngArr + 1).Value
      Next lngArr
      
      Erase varArr
    Next lngCounter
    
    Set wsCopy = Nothing
    Set wsMaster = Nothing
    
    End Sub
    Ciao,
    Holger

  5. #5
    Registered User
    Join Date
    04-18-2012
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: macro to create copy of template based on two criteria and paste data from master shee

    Hi Holger,

    That works great. Almost what i wanted. But a slight change is required. I am attaching the file with the result.As you will see in sheets M17 and M18 the units and prices were not pasted in Correct Position. The code used the position for Gsec Pur and Gsec Sale sheets thought they were not. (they are in different rows in GSec sheets and Non Gsec Sheets)
    I request you to please edit it for me.

    But otherwise a marvelous code.
    Thanks.
    Attached Files Attached Files

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: macro to create copy of template based on two criteria and paste data from master shee

    Hi, deepsi1,

    please alter the code like this:
    For lngCounter = 2 To lngLast
      blnPlus = False
      Select Case wsMaster.Cells(lngCounter, "E").Value
    Ciao,
    Holger

  7. #7
    Registered User
    Join Date
    04-18-2012
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: macro to create copy of template based on two criteria and paste data from master shee

    Hi Holger,

    Sorry to bring this up late, I need a little extra bit.
    I may have to run the code again and again as data comes to me in bits and parts. So what I need is when i add data to sheet 1, if the sheet for deal id already exists it should skip to next line , such that it will move to additional data in sheet1 when i run the macro 2nd or 3rd time.

    Thanks a Ton. The modification worked perfectly.

  8. #8
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: macro to create copy of template based on two criteria and paste data from master shee

    Hi, deepsi1,

    either place a boolean value next to the data already worked on or use either a loop or try to activate the sheet to find out what action needs to be taken (skip or add a new one). IŽd go with the additional information in the original Master.

    Ciao,
    Holger

  9. #9
    Registered User
    Join Date
    04-18-2012
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: macro to create copy of template based on two criteria and paste data from master shee

    Hi Holger,

    In case I add additional columns (fields) in Sheet1 , say column G and H containing additional values that i need to paste in the Deal Sheets in Cell F7 & G7, how do I edit the code.

  10. #10
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: macro to create copy of template based on two criteria and paste data from master shee

    Hi, deepsi1,

    add the cells to copy and paste in the Arrays:
    varArr1 = Array("B2", "B4", "B7", "C7", "F7", "G7")
    varArr2 = Array("B2", "B4", "B8", "C8", "F8", "G8")
    The copying will be done by the length of the Arrays but the cells would need to be directly side by side (no other data in the columns from A to F). If you want other values in there you would need another array holding the columns from which to take and in which order.

    Ciao,
    Holger

  11. #11
    Registered User
    Join Date
    04-18-2012
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: macro to create copy of template based on two criteria and paste data from master shee

    Hi Holger,

    That was really quick. You are brilliant. I tried it and it worked perfectly.
    Can I request you again to provide me something for "if the sheet for deal id in Sheet1 already exists it should skip to next line , such that it will move to additional data in sheet1 when i run the macro 2nd or 3rd time.". The reason being that even though I will get the data in Parts I receive consolidated data not just the new line items. I know the code will become heavy and too complex, But I would really like to see if it can be done.

  12. #12
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: macro to create copy of template based on two criteria and paste data from master shee

    Hi, deepsi,

    maybe use another Boolean for the check of whether the sheet already exists or needs to be copied. Code is untested:
    Sub EF920361_2()
    Dim wsMaster As Worksheet
    Dim wsCopy As Worksheet
    Dim lngLast As Long
    Dim lngCounter As Long
    Dim lngArr As Long
    Dim blnPlus As Boolean
    Dim blnNewSh As Boolean
    Dim varArr1
    Dim varArr2
    Dim varArr
    
    varArr1 = Array("B2", "B4", "B7", "C7", "F7", "G7")
    varArr2 = Array("B2", "B4", "B8", "C8", "F8", "G8")
    
    Set wsMaster = Sheets("Sheet1")
    lngLast = wsMaster.Cells(Rows.Count, "E").End(xlUp).Row
    
    For lngCounter = 2 To lngLast
      blnPlus = False
      Select Case wsMaster.Cells(lngCounter, "E").Value
        Case "GSE", "GGB", "ZCG", "TBL"
          blnPlus = True
          If NewSheet(wsMaster.Cells(lngCounter, "A").Value) Then
            If wsMaster.Cells(lngCounter, "F").Value = "P" Then
               Sheets("Gsec Pur").Copy After:=Worksheets(Worksheets.Count)
               Set wsCopy = ActiveSheet
            Else
              Sheets("Gsec Sale").Copy After:=Worksheets(Worksheets.Count)
              Set wsCopy = ActiveSheet
            End If
          Else
            Set wsCopy = Sheets(wsMaster.Cells(lngCounter, "A").Value)
          End If
        Case Else
          If NewSheet(wsMaster.Cells(lngCounter, "A").Value) Then
            If wsMaster.Cells(lngCounter, "F").Value = "P" Then
               Sheets("Buy").Copy After:=Worksheets(Worksheets.Count)
               Set wsCopy = ActiveSheet
            Else
              Sheets("Sale").Copy After:=Worksheets(Worksheets.Count)
              Set wsCopy = ActiveSheet
            End If
          Else
            Set wsCopy = Sheets(wsMaster.Cells(lngCounter, "A").Value)
          End If
      End Select
      
      wsCopy.Name = wsMaster.Cells(lngCounter, "A").Value
      If blnPlus Then
        varArr = varArr2
      Else
        varArr = varArr1
      End If
      
      For lngArr = LBound(varArr) To UBound(varArr)
        wsCopy.Range(varArr(lngArr)).Value = wsMaster.Cells(lngCounter, lngArr + 1).Value
      Next lngArr
      
      Erase varArr
    Next lngCounter
    
    Set wsCopy = Nothing
    Set wsMaster = Nothing
    
    End Sub
    
    Private Function NewSheet(strShtName As String) As Boolean
    On Error Resume Next
    Sheets(strShtName).Select
    NewSheet = Err <> 0
    End Function
    Evaluate could be used to check the existence of the sheet instead of activating the sheet like shown in the Function above.

    Ciao,
    Holger

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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