Extract data from one to many worksheets

Hugh Crown

New Member
Joined
May 13, 2009
Messages
8
Hi everybody,

I'm new to Mr Excel and I hope to gain a little bit of all your knowledge about VB codes.
The project I'm working on requires me to generate single spreadsheets by manager from a consolidated transactions statement. The data is as follows: column A- last name, B- txn description, C-Amount, D- Exp Description, E- Dept, F- Location, G- Addt'l Info, H,I and J are dropdown lists values used for columns D, E and F.
I need to create: 1- A new worksheet for each manager listed in column A and related txns in the same row columns B and C. 2- The data must be paste as values keeping the formatting from source. 3- Additionally data validation in columns D, E and F needs to be paste as is.
I look forward to hear from anybody that can help me with a code to do this since I'm just starting to understand how they work.

Regards,

Hugh
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi there,

Here is a sample of the data that I'm looking to export to new single worksheets:

Excel Workbook
ABCDEFHIJ
1Last NameTransaction DescriptionAmount USDDescriptionDepartmentLocationDropdown DescriptionDepartmentLocation
2DOETrxn 1$ 10.00Dropdown list HDropdown list IDropdown list JDropdown value I1Dropdown value J1Dropdown value K1
3DOETrxn 2$ 15.00Dropdown list HDropdown list IDropdown list JDropdown value I2Dropdown value J2Dropdown value K2
4DOETrxn 3$ 54.27Dropdown list HDropdown list IDropdown list JDropdown value I3Dropdown value J3Dropdown value K3
5DOETrxn 4$ 13.43Dropdown list HDropdown list IDropdown list JDropdown value I4Dropdown value J4Dropdown value K4
6DOETrxn 5$ 112.30Dropdown list HDropdown list IDropdown list JDropdown value I5Dropdown value J5Dropdown value K5
7DOETrxn 6$ 140.39Dropdown list HDropdown list IDropdown list JDropdown value I6Dropdown value J6Dropdown value K6
8PETERSENTrxn 7$ 203.18Dropdown list HDropdown list IDropdown list JDropdown value I7Dropdown value J7Dropdown value K7
9PETERSENTrxn 8$ 396.20Dropdown list HDropdown list IDropdown list JDropdown value I8Dropdown value J8Dropdown value K8
10PETERSENTrxn 9$ 5.74Dropdown list HDropdown list IDropdown list JDropdown value I9Dropdown value J9Dropdown value K9
11PETERSENTrxn 10$ 6.24Dropdown list HDropdown list IDropdown list JDropdown value I10Dropdown value J10Dropdown value K10
12PETERSENTrxn 11$ 3.83Dropdown list HDropdown list IDropdown list JDropdown value I11Dropdown value J11Dropdown value K11
13PETERSENTrxn 12$ 15.00Dropdown list HDropdown list IDropdown list JDropdown value I12Dropdown value J12Dropdown value K12
14PETERSENTrxn 13$ 317.34Dropdown list HDropdown list IDropdown list J
15PETERSENTrxn 14$ 3.83Dropdown list HDropdown list IDropdown list J
16PETERSENTrxn 15$ 54.13Dropdown list HDropdown list IDropdown list J
17PETERSENTrxn 16$ 14.81Dropdown list HDropdown list IDropdown list J
18PETERSENTrxn 17$ 4.40Dropdown list HDropdown list IDropdown list J
19PETERSENTrxn 18$ 6.22Dropdown list HDropdown list IDropdown list J
20PETERSENTrxn 19$ 307.48Dropdown list HDropdown list IDropdown list J
21PETERSENTrxn 20$ 3.89Dropdown list HDropdown list IDropdown list J
22PETERSENTrxn 21$ 7.43Dropdown list HDropdown list IDropdown list J
23PETERSENTrxn 22$ 15.00Dropdown list HDropdown list IDropdown list J
24CROWNTrxn 23$ 10.00Dropdown list HDropdown list IDropdown list J
25CROWNTrxn 24$ 47.84Dropdown list HDropdown list IDropdown list J
26CROWNTrxn 25$ 714.40Dropdown list HDropdown list IDropdown list J
27CROWNTrxn 26$ 6,750.00Dropdown list HDropdown list IDropdown list J
28CROWNTrxn 27$ 588.20Dropdown list HDropdown list IDropdown list J
29CROWNTrxn 28$ 306.20Dropdown list HDropdown list IDropdown list J
30CROWNTrxn 29$ 446.40Dropdown list HDropdown list IDropdown list J
31CROWNTrxn 30$ 339.20Dropdown list HDropdown list IDropdown list J
32CROWNTrxn 31$ 339.20Dropdown list HDropdown list IDropdown list J
33CROWNTrxn 32$ 229.20Dropdown list HDropdown list IDropdown list J
34CROWNTrxn 33$ 140.20Dropdown list HDropdown list IDropdown list J
35CROWNTrxn 34$ 140.20Dropdown list HDropdown list IDropdown list J
36CROWNTrxn 35$ 79.60Dropdown list HDropdown list IDropdown list J
37CROWNTrxn 36$ 248.70Dropdown list HDropdown list IDropdown list J
38CROWNTrxn 37$ 80.20Dropdown list HDropdown list IDropdown list J
39CROWNTrxn 38$ 200.20Dropdown list HDropdown list IDropdown list J
40CROWNTrxn 39$ 197.70Dropdown list HDropdown list IDropdown list J
Cons Statement


I hope to hear from you experts in the matter...

Regards,

Hugh
 
Upvote 0
Here is code I found on the board: Alt+F11 to open VBE; insert|module; paste into the white space.

Code:
Sub CopyToNewSheetsByGroup_2()
Dim strName As String, i As Integer
Dim UsedRng As Range, Rng As Range, FRng As Range, R As Range, c As Range, sh As Worksheet
Dim HdrBoo As Boolean, HdrMsg As Variant, HdrRng As Range
Dim StartTime As Date, TmpSh As Worksheet, TmpRng As Range
Dim GroupIDs As String, ShName As String


    intResponse = MsgBox("This macro will create a worksheet for each unique group identifier" & vbCrLf & _
    "in the user-selected column. This may take a while to" & vbCrLf & _
    "process if there are a lot of groups. Continue?", vbOKCancel, "Separate By Groups")
    
    If intResponse = vbOK Then
        'Get used range for the sort
        Set UsedRng = ActiveSheet.UsedRange
        
        'Ask for column to base your search. If no range is selected procedure stopped
        On Error Resume Next 'set Rng will error if no range selected
        Set Rng = Application.InputBox("Select column with Group ID's" & vbCrLf _
        & "Column must not contain Formulas.", "Pick a Column", , , , , , 8)
        If Rng Is Nothing Or Rng.Columns.Count > 1 Then 'exit if cancel was pressed or more than 1 column is selected
            MsgBox "Operation cancelled"
            Exit Sub
        End If
        
        'Ask if theres a header row. By default HdrBoo is false.
        HdrMsg = MsgBox("Do you have a header row?" & vbLf & _
            "Note: Must be the 1st row in worksheet", vbYesNo, "Header Row?")
        If HdrMsg = vbYes Then
            HdrBoo = True 'variable to indicate if a header is used
        End If
        
        'Start Timer
        StartTime = Timer
        
        'Turn off screen updating & calculation
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        
        On Error GoTo errorhandler
        'Filter unique values
        ActiveSheet.Columns(Rng.Column).AdvancedFilter Action:=xlFilterInPlace, _
            CriteriaRange:=Rng, Unique:=True
            
        'Copy unique values to a temporary sheet
        Set TmpSh = Worksheets.Add 'create temp sheet
        Sheets(Rng.Parent.name).Activate 'return to original sheet
        Set Rng = Range(Rng.End(xlUp), Rng.End(xlDown)) 'make sure entire column is selected
        
        Rng.Copy TmpSh.Range("a1") 'copy unique items to temporary sheet
        TmpSh.Activate
        MyCount = TmpSh.Range([A1], [A1].End(xlDown)).Rows.Count
        
        If MyCount > 21 Then
            intResponse = MsgBox("There are more than 20 different groups." & vbCrLf & vbCrLf & _
            "This could take a while. Continue?", vbOKCancel, "Separate by Groups")
            If intResponse = vbCancel Then GoTo errorhandler
        End If
        
        'Set ranges for header (if applicable) and unique values
        TmpSh.Activate
        If HdrBoo = True Then
            Set HdrRng = Range("A1") '1st row in the range is the header
            Set TmpRng = Range("A2:A" & Range("A1").End(xlDown).Row) 'unique values
        Else
            Set TmpRng = Range("A1:A" & Range("A1").End(xlDown).Row) 'unique values
        End If
        
        Sheets(Rng.Parent.name).Activate 'return to original sheet
        Application.CutCopyMode = False 'turn off copy mode
        ActiveSheet.ShowAllData 'remove Advanced Filter
        Set FRng = Range(Rng.End(xlUp), Rng.End(xlDown)) 'Set Full Range column for later use
        
        'Loop through each unique value to copy row to target in respective new sheets
        For Each c In TmpRng
            i = i + 1 'counter for sheet name
            Set sh = Worksheets.Add(After:=Sheets(Sheets.Count)) 'add a new sheet & name it
            ShName = TrimExcelSheetName(c.Value)
            If SheetExists(ShName) Then
                sh.name = ShName & Sheets.Count
            Else
                sh.name = ShName 'name sheet as string and counter number
            End If
            'Assign counter variable for row number to copy to
            'Also copy Header if True
            If HdrBoo = True Then
                cntr = Sheets(ShName).UsedRange.Rows.Count + 1
                'copy header row to target sheet
                Sheets(Rng.Parent.name).Rows("1:1").Copy Sheets(ShName).Range("A1")
            Else
                cntr = Sheets(ShName).UsedRange.Rows.Count
            End If
            For Each R In FRng
                If R.Value = c Then
                    r2c = R.Row
                    Sheets(Rng.Parent.name).Rows(r2c & ":" & r2c).Copy Sheets(ShName).Range("A" & cntr)
                    cntr = cntr + 1 'increment counter row number
                End If
            Next R
            sh.Cells.EntireColumn.autofit
        Next c
        
        'Turn back on screen updating & remove filter
        Application.ScreenUpdating = True
        Sheets(Rng.Parent.name).Activate 'return to original sheet
        ActiveSheet.AutoFilterMode = False
        
        'Delete Temporary sheet
        Application.DisplayAlerts = False 'avoids delete confirmation message
        TmpSh.Delete
        Application.DisplayAlerts = True
        
        'Display the elapsed time
        MsgBox "The procedure took  " & Format(Timer - StartTime, "00.00") & "  seconds.", _
            vbInformation, "Operation Successfully Completed"
        End If
        ActiveSheet.AutoFilterMode = False
errorhandler:
    If Err <> 0 Then
        MsgBox Err.Number & ": " & Err.Description, , "Error Occurred"
        On Error GoTo 0
    End If
    Application.Calculation = xlCalculationAutomatic
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top