Solved

Help with Excel formula and/or script copying data to new sheets.

Posted on 2015-01-30
5
86 Views
Last Modified: 2015-02-04
I have sheet named “RawCarData”.   It contains records; 10 columns, one column is a CompanyID that can be “Ford”, “Chevy”, “Honda”, etc.

I need to create additional sheets, one for each unique CompanyID, named “Ford”, “Chevy”, “Honda”, etc.  I need to the copy the data from the “RawCarData” to these new sheets based on the CompanyID.  I hope this makes sense.

Is there a way to do this using formulas or some Excel script?
0
Comment
Question by:HLRosenberger
5 Comments
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 125 total points
Comment Utility
Assuming the data for CompanyID starts in row 2, column 1, then try this:

Sub AddSheets()
Set currentsheet = ActiveSheet
introw = 2
Do Until Cells(introw, 1) = ""
    ActiveWorkbook.Sheets.Add
    ActiveSheet.Name = currentsheet.Cells(introw, 1)
    currentsheet.Activate
    introw = introw + 1
Loop
End Sub

Open in new window

0
 
LVL 18

Accepted Solution

by:
SimonAdept earned 250 total points
Comment Utility
If you create a pivot table (Insert/Pivot Table) and put CompanyID in the Rows area...

You can then use this macro to split it into individual sheets, each containing all the rows of data for one make.
Sub PivotTable_GenerateWkShtPerLine()
Const msgTitle As String = "Pivot table burster"
Dim continue As Variant
Dim pivotSht As Worksheet

Dim pt As String
Dim titlesRange As Range
Dim shtTitlesRange As Range
Dim titlePrefix As String
Dim titleSuffix As String
Dim shtName As String
Dim fitToOnePageWide_999Tall As Boolean

continue = MsgBox("This creates one sheet for each selected row in the pivot table, named according to the column(s) you choose." & vbCrLf & "For best results, ensure you have sorted the base data and set the default table style before running this.", vbOKCancel + vbInformation, msgTitle)
If continue <> vbOK Then Exit Sub Else

Set pivotSht = ActiveSheet
'Test whether the selection is part of a pivot table.
On Error Resume Next
pt = ActiveSheet.Range(Selection.Address).PivotTable.Name
If Err.Number <> 0 Then
    MsgBox "The selected range (" & Selection.Address & ") is not part of a pivot table!", , msgTitle
    Exit Sub
End If
On Error GoTo 0

If Selection.Cells.Count < 2 Then
    If MsgBox("Select a range of cells (usually the totals) for which to show details (by generating a new sheet per pivot row)", vbOKCancel, msgTitle) <> vbOK Then
        Exit Sub
    End If
End If

'Prompt user to select the columns from which to generate sheet names
On Error Resume Next
Set titlesRange = Application.InputBox("Which columns have the data that will form the worksheet names?", msgTitle, , , , , , 8)
If Err.Number <> 0 Then Exit Sub Else
On Error GoTo 0
Debug.Print "Titles from range: " & titlesRange.Address

On Error Resume Next
titlePrefix = Application.InputBox("Add a prefix to the worksheet names if desired", msgTitle, , , , , , 2)
If Err.Number <> 0 Then Exit Sub Else
On Error GoTo 0
Debug.Print "Title prefix string: " & titlePrefix

On Error Resume Next
titleSuffix = Application.InputBox("Add a suffix to the worksheet names if desired", msgTitle, , , , , , 2)
If Err.Number <> 0 Then Exit Sub Else
On Error GoTo 0
Debug.Print "Title suffix string: " & titleSuffix

'
'If MsgBox("Fit result sheets to one landscape page wide?", vbQuestion + vbYesNo, msgTitle) = vbYes Then
'fitToOnePageWide_999Tall = True
'End If


'*** Main Loop ***
Dim c As Variant
Dim i As Variant
For Each c In Selection
    Debug.Print Intersect(titlesRange, Rows(c.Row)).Address
    Set shtTitlesRange = Intersect(titlesRange, Rows(c.Row))
    shtName = ""
    For Each i In shtTitlesRange
        If shtName = "" Then
            shtName = i
        Else
            shtName = shtName & "_" & i
        End If
    Next
    shtName = titlePrefix & shtName & titleSuffix
    shtName = Left(shtName, 31)
    c.ShowDetail = True 'This creates a new worksheet for the pivot table item's detail lines
    ActiveSheet.Name = shtName
    
    
    If fitToOnePageWide_999Tall Then
        Call PageSetup_FitToOnePageWideBy999Tall
    End If
    ActiveSheet.Cells(2, 1).Select
    'Turn off autofilter to make sheet autofit columns more tightly
    Selection.AutoFilter 'This relies on the fact that the result sheet are initially autofiltered (so this command turns the filter OFF)
    ActiveSheet.Columns.AutoFit
    ActiveWindow.FreezePanes = True
    ActiveSheet.Cells(1, 1).Select 'Leave sheet with cell A1 selected
    'switch focus back to pivot sheet, so that intersect statement works.
    pivotSht.Activate
Next
End Sub

Open in new window


See also this previously answered question: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_28577286.html
0
 
LVL 31

Assisted Solution

by:Rob Henson
Rob Henson earned 125 total points
Comment Utility
Alternative to Simon's suggestion with a Pivot Table, use the Company ID as a Page Value, rather than in Row values, then the report is just for that Company.

You can then just use only one report sheet but change the Page Reference as required.

You could also use the Advanced Filter function to filter data and copy to another location if you do want multiple sheets.

Thanks
Rob H
0
 
LVL 18

Assisted Solution

by:SimonAdept
SimonAdept earned 250 total points
Comment Utility
I should have mentioned that I have a different variant of my code to break the pivoted data into separate workbooks (e.g. one per CompanyID) if that's required.
0
 
LVL 1

Author Closing Comment

by:HLRosenberger
Comment Utility
thanks to all.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article will show you how to use shortcut menus in the Access run-time environment.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now