+ Reply to Thread
Results 1 to 15 of 15

Thread: Copy rows from 1 sheet by Group

  1. #1
    Registered User
    Join Date
    08-19-2010
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2010
    Posts
    12

    Copy rows from 1 sheet by Group

    I have attached an Excel book. I am very new to Excel VBA, hence request to please help me in building a macro with this:

    Sheet1 contains data for groups and subgroups as shown.

    Sheet2 contains multiple rows for each sub group.

    How do I write a macro so that it dos the following things:

    1. Iterate Sheet1 and create new sheets based on Group name. Here, create 4 new sheets as A, B, C, D

    2. Iterate Sheet2 and copy rows of sub groups to the corresponding Group sheets which are newly created. Ex: Copy all rows starting with 'aa' or 'aaa' to the group sheet A

    3. Similarly carry out for other groups

    The data in Sheet1 and Sheet2 can be changed and the macro should handle it accordingly.
    Attached Files Attached Files

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Copy rows from 1 sheet by Group

    'SHEET1 TO MANY SHEETS
    I have a macro that may be "ready to use" for parsing rows of data from one sheet to many sheets named for the same values.It not only can parse the rows, it can create the sheets if they are missing.

    I've edited the macro and inserted it into your workbook. I've adjusted your sheet1 so it's simpler to evaluate. Press Alt-F8 and run the macro.
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    08-19-2010
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Copy rows from 1 sheet by Group

    Thank you...

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Copy rows from 1 sheet by Group

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  5. #5
    Registered User
    Join Date
    08-19-2010
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Copy rows from 1 sheet by Group

    Err.. Hi again. I had a doubt again with this.

    MyArr is declared as a Variant. But in the sheet, if MyArr holds only 1 value(A Range Object), and I loop through it using:

    For Itm = 1 To UBound(MyArr)
    'or
    For Itm = LBound(MyArr) To UBound(MyArr)
    it is giving a type mismatch. Please suggest where am I going wrong here...
    Last edited by rogerabhi; 08-31-2010 at 12:40 AM.

  6. #6
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Copy rows from 1 sheet by Group

    Post your misbehaving workbook and we can look at it together.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  7. #7
    Registered User
    Join Date
    08-19-2010
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Copy rows from 1 sheet by Group

    It's the same sheet "ParseItems.xlsm". I tried with this code instead:
    
    'Check for more than one value in list
        If ws.Range("EE" & Rows.Count).End(xlUp).Row >= 2 Then
    ...
    Here, if the Range equals to 2, MyArr will hold only one value.
    Won't the loop work if there is only one value?

  8. #8
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Copy rows from 1 sheet by Group

    I do not experience any errors with the code given on the sample workbook. If you've changed the values in some way and are now experiencing an error, I need to see that.

    If I were to just guess, I'd say it should work with any number of values.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  9. #9
    Registered User
    Join Date
    08-19-2010
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Copy rows from 1 sheet by Group

    In the code for the same file, there is an error handling statement like this:

    If Oops Then MsgBox "Only one value found, aborting parse process..."

    How to change this so that even if only one value is there to parse, the sub routine should go ahead in parsing?

  10. #10
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Copy rows from 1 sheet by Group

    I added an entry to the sample sheet that was a single entry and it worked.

    All I have to go on is the sample sheet and your questions. I can't duplicate the problem you're having. Upload a sample sheet that actually is misbehaving and I'm sure I can help quickly, so far I can't.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  11. #11
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Copy rows from 1 sheet by Group

    The light's just came on..took me a while. Try this:
    Option Explicit
    
    Sub ParseItems()
    'Author:    Jerry Beaucaire
    'Original Date:    11/11/2009
    'Summary:    Based on selected column, data is filtered to individual sheets
    '        Creates sheets and sorts sheets alphabetically in workbook
    '        6/10/2010 - added check to abort if only one value in vCol
    '        7/22/2000 - added ability to parse numeric values consistently
    Dim LR As Long, Itm As Long, MyCount As Long, vCol As Long
    Dim ws As Worksheet, MyArr As Variant, vTitles As String, Oops As Boolean
    
    Application.ScreenUpdating = False
    
    'Column to evaluate from, column A = 1, B = 2, etc.
       vCol = 27
     
    'Sheet with data in it
       Set ws = Sheets("Sheet2")
    
    'Range where titles are across top of data, as string, data MUST
    'have titles in this row, edit to suit your titles locale
        vTitles = "A1:AA1"
       
    'Spot bottom row of data
       LR = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    'Add key formula in empty column
        ws.Range("AA1") = "Key"
        With ws.Range("AA2:AA" & LR)
            .FormulaR1C1 = "=VLOOKUP(RC1, Sheet1!C1:C2, 2, 0)"
            .Value = .Value
        End With
    
    'Get a temporary list of unique values from column A
          ws.Range("AA:AA").SpecialCells(xlConstants).AdvancedFilter _
            Action:=xlFilterCopy, CopyToRange:=ws.Range("EE1"), Unique:=True
    
    'Sort the temporary list
        ws.Columns("EE:EE").Sort Key1:=ws.Range("EE2"), _
            Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _
            MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    
    'Put list into an array for looping
    '(values cannot be the result of formulas, must be constants)
        MyArr = Application.WorksheetFunction.Transpose(ws.Range("EE1:EE" _
            & Rows.Count).SpecialCells(xlCellTypeConstants))
    
    'clear temporary worksheet list
        ws.Range("EE:EE").Clear
    
    'Turn on the autofilter, one column only is all that is needed
        ws.Range(vTitles).AutoFilter
    
    'Loop through list one value at a time
    'In case values are numerical, we convert them to text with ""
        For Itm = 2 To UBound(MyArr)
            ws.Range(vTitles).AutoFilter Field:=vCol, Criteria1:=MyArr(Itm) & ""
        
            If Not Evaluate("=ISREF('" & MyArr(Itm) & "'!A1)") Then    'create sheet if needed
                Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = MyArr(Itm) & ""
            Else                                                      'clear sheet if it exists
                Sheets(MyArr(Itm) & "").Move After:=Sheets(Sheets.Count)
                Sheets(MyArr(Itm) & "").Cells.Clear
            End If
        
            ws.Range("A" & Range(vTitles).Resize(1, 1) _
                .Row & ":C" & LR).Copy Sheets(MyArr(Itm) & "").Range("A1")
            
            ws.Range(vTitles).AutoFilter Field:=vCol
            MyCount = MyCount + Sheets(MyArr(Itm) & "") _
                .Range("A" & Rows.Count).End(xlUp).Row - 1
            Sheets(MyArr(Itm) & "").Columns.AutoFit
        Next Itm
        
    'Cleanup
        ws.AutoFilterMode = False
        ws.Range("AA:AA").Clear
        MsgBox "Rows with data: " & (LR - 1) & vbLf & "Rows copied to other sheets: " _
                    & MyCount & vbLf & "Hope they match!!"
    
    Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  12. #12
    Registered User
    Join Date
    08-19-2010
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Copy rows from 1 sheet by Group

    Thank for the reply. A reference stated: " A Variant allows VBA to make its own decision as to what type of data it is holding". So, if MyArr holds more than 1 variable, the UBound function returns fine as it treats it as an array. But if MyArr holds only 1 variable, UBound gives a type mismatch..

  13. #13
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Copy rows from 1 sheet by Group

    In post #11 I changed the macro to always include the header in the array resolution, so when only one value is in the list, it still has two item, the header and that one item. Then I always start the loop later with the second item in the array which is one real item.

    It should be working now, I was able to duplicate your problem and post #11 shows how I resolved it.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  14. #14
    Registered User
    Join Date
    08-19-2010
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Copy rows from 1 sheet by Group

    Thanks.. It worked perfect.

  15. #15
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Copy rows from 1 sheet by Group

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

+ 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.2.0