Google Answers Logo
View Question
 
Q: Excel Macro- for each change in an a name extract data & creat new xls file ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Excel Macro- for each change in an a name extract data & creat new xls file
Category: Computers > Software
Asked by: jpcp-ga
List Price: $50.00
Posted: 03 Dec 2003 07:30 PST
Expires: 02 Jan 2004 07:30 PST
Question ID: 283045
An excel macro is need to create spreadsheets by extracting data from
a file by name and creating a new file based on name changes.  The
macro would read the sample data and create the first file Arg.XLS
Acct	        AcctName        Date	        Amount
0000105400	Arg      	1/0/1900	26.14
0000105400	Arg     	1/0/1900	22.66
0000105400	Arg      	1/0/1900	17905.64
0000105400	Arg      	1/0/1900	11774.30

Then create the next file  Australia Hat.XLS and so on....

The file is sorted
The names are not constant - meaning sometimes there will be more names
The amount of lines per name will vary

Sample data
Col A            B               C               D
Acct	        AcctName        Date	        Amount
0000105400	Arg      	1/0/1900	26.14
0000105400	Arg     	1/0/1900	22.66
0000105400	Arg      	1/0/1900	17905.64
0000105400	Arg      	1/0/1900	11774.30
0000105401	Australia Hat   1/0/1900	170120.40
0000105401	Australia Hat   1/0/1900	143907.30
0000105401	Australia Hat	1/0/1900	-55142.40
0000105401	Australia Hat	1/0/1900	414.96
0000105412	China Cup   	1/0/1900	103842.32
0000105412	China Cup   	1/0/1900	945.86
0000105412	China Cup   	1/0/1900	960.76
0000105412	China Cup   	1/0/1900	53628.46
0000105412	China Cup   	1/0/1900	920.00
0000105412	China Cup   	1/0/1900	-876.00
0000105413	Columbia U      1/0/1900	19.38
0000105413	Columbia U	1/0/1900	136771.80
0000105413	Colombia U	1/0/1900	-131425.36
0000105414	Costal	        1/0/1900	77309.46
0000105414	Costal  	1/0/1900	77545.26
0000105414	Costal  	1/0/1900	52893.72
0000105414	Costal  	1/0/1900	125983.20
0000105414	Costal  	1/0/1900	123455.50
0000105414	Costal  	1/0/1900	-122324.66
0000105414	Costal  	1/0/1900	3288.00
0000105416	Czechy  	1/0/1900	46652.68
0000105416	Czechy  	1/0/1900	64348.54
0000105416	Czechy  	1/0/1900	45049.86
0000105419	Dominican	1/0/1900	810.00

Request for Question Clarification by hammer-ga on 03 Dec 2003 14:34 PST
1. Which version of Excel?

2. It looks like the driving column is the second column (AcctName).
We need to know how can we reliably find the second column. For
example, is this a Tab-delimited file, where we can look for the
column after the tab? Or does the AcctName always start at the 15th
character? Something like that.

3. Should we always skip the first row, assuming that it is column headers?

- Hammer

Request for Question Clarification by hammer-ga on 03 Dec 2003 14:42 PST
Oops. I just realized that the input file is also an Excel
spreadsheet. I was looking at your sample data as being a text file
exported from something else.

Never mind the original questions. I only need to know which version of Excel.

- Hammer
Answer  
Subject: Re: Excel Macro- for each change in an a name extract data & creat new xls file
Answered By: maniac-ga on 03 Dec 2003 17:57 PST
Rated:5 out of 5 stars
 
Hello Jpcp,

The macro listed at the end of the answer should handle the problem
you have set. The first part of the answer will explain the two
subroutines and how they work.

The subroutine named FileMaker takes two parameters
 - the header to be pasted at the top
 - the name of the file to be written to (with added .XLS)
and it assumes that the current selection is the data to be output.

FileMaker copies the data, creates the new worksheet, pastes the
header and then the data, writes the file, and then closes the
worksheet. This is a helper function, called twice from the main
function. You may also get prompted Yes / No to overwrite the files if
you don't clean up the directory between each macro run. That can be a
helpful debug indication - you can see the selected range in the
background while the prompt is shown.

The main function ExtractData assumes you have selected at least one
cell in the "Current Region" of data to be processed. It will do the
rest once started. It also assumes the data to be processed is in a
single region but may have more / less rows or columns.

ExtractData first declares two objects
 - MyRange is used for the range of data to be operated on
 - Header will get the first row of data

The first few lines determine the size of the current region and
initializes other variables.

A key variable named "FileCol" can be changed if the column of data to
be used for the name of the file needs to be changed. Note - in your
sample data, you have the same Acct for "Columbia U" and "Colombia U"
(a slight misspelling) - hence the macro as written will generate two
files for this Acct. If you change the value of MatchCol to 1, it will
match on Acct instead of AcctName and then the account number will be
used to determine the change and only one file will be generated for
this Acct, having all three lines. Make this change if you need it.

The "For Each" loop goes through each row. The first row is remembered
as Header. The second time through, S$ is initialized with the first
value to match. On all subsequent rows, if the match value changes,
the range of values will be selected and FileMaker is called. The loop
also keeps track of the "current row" and "selection row" ( the one
with the first matching value).

After the last row is processed, the last range of matching values are
selected and FileMaker is called one last time to complete the file
output.

If you have any problems at all, do not hesitate to use a
clarification request to get further information on how to use,
modify, or better understand this set of macros.

Good luck with your work.

  --Maniac

Note - as a result of formatting, it may be necessary to paste a
couple lines back together. I have tried to keep the line length below
70 to reduce that possibility (the few continued lines) but keep that
in mind if it does not run right the first time.

Also, if you have not used macros before - use the menu
  Tools -> Macro -> Visual Basic Editor
In Visual Basic, use
  Insert -> Module
to bring up a window an paste the remaining part of this answer into
that window. Go back to Excel using
  View -> Microsoft Excel
You should now be able to use
  Tools -> Macro
select the macro Extract_Data and then Run to generate the files. Be
sure to save the macro with your data or if interested, I can describe
how to add it to a separate file (or your default file) so the macro
is available if you need to run it with more than one data
spreadsheet.

--- The macro is below this line ---

Sub FileMaker(Header, S$)
'
' FileMaker Macro
' Macro written 12/3/2003 by Maniac
' Assume current selection is the region to be written into the
' file named S$.xls. The header is pasted above the data values.
'

'
    Selection.Copy
    Workbooks.Add
    HeaderCols = Header.Columns.Count
    With ActiveSheet
        .Range(.Cells(1, 1), .Cells(1, HeaderCols)).Value = Header.Value
        .Cells(2, 1).Select
        .Paste
    End With
    Application.CutCopyMode = False
    ActiveWorkbook.SaveAs FileName:=S$ + ".xls"
    ActiveWindow.Close
 End Sub
 
 Sub ExtractData()
 '
 ' ExtractData Macro
 ' Macro written 12/3/2003 by Maniac
 ' Assume current selection is with the region of data to be
 ' saved into files, each file named based on the value in the
 ' second column (NameCol)
 '
 
'   Extract the information on the region to process
    Dim MyRange As Object
    Dim Header As Object
    
    Set MyRange = Selection.CurrentRegion
    With MyRange
        FirstCol = .Column
        LastCol = .Columns(.Columns.Count).Column - FirstCol + 1
        FirstRow = .Row
        LastRow = .Rows(.Rows.Count).Row - FirstRow + 1
    End With
    M$ = "" ' Default to no match name
    F$ = "" ' Default to no file name
    CurRow = 1
    SelRow = 1
    
'   Adjust following lines and/or comment to adjust macro operation

    FileCol = 2
    MatchCol = 2

'   Main loop, go through each row to end of range

    For Each r In MyRange.Rows
        If M$ <> r.Cells(1, MatchCol) Then
            If M$ <> "" Then
                MyRange.Range(MyRange.Cells(SelRow, 1), _
                  MyRange.Cells(CurRow - 1, LastCol)).Select
                Call FileMaker(Header, F$)
                SelRow = CurRow
                M$ = r.Cells(1, MatchCol)
                F$ = r.Cells(1, FileCol)
            Else
                If CurRow = 1 Then
                    Set Header = r
                Else
                    M$ = r.Cells(1, MatchCol)
                    F$ = r.Cells(1, FileCol)
                    SelRow = CurRow
                End If
            End If
        Else
            ' Do nothing if in middle of range of cells
        End If
        CurRow = CurRow + 1
    Next r
    ' Don't forget the last region of values
    MyRange.Range(MyRange.Cells(SelRow, 1), _
      MyRange.Cells(CurRow - 1, LastCol)).Select
    Call FileMaker(Header, F$)
End Sub
jpcp-ga rated this answer:5 out of 5 stars and gave an additional tip of: $10.00
Answer right on the button, very fast response

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy