Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

Convert spreadsheet report to a flat file

Latest post Thu, Jun 12 2008 5:04 PM by Woza2712. 2 replies.
  • Thu, Jun 12 2008 9:41 AM

    • Woza2712
    • Top 150 Contributor
    • Joined on Mon, Jun 9 2008
    • Epping, NSW, Australia
    • Posts 2
    • Points 26

    Convert spreadsheet report to a flat file

    I have a spreadsheet report from our accounting system in the following format (which goes on for several thousand lines)

    Job 1 Feb Mar
    Salaries & Wages 1000 1200
    Oncosts 50 60
    Depreciation 100 100
    Consulting 80 0
    Occupancy Costs 40 40
    Utilities 10 12
    Other Expense 3 8
    Job 2
    Salaries & Wages 1100 1010
    Oncosts 55 51
    Depreciation 100 100
    Consulting 12 16
    Occupancy Costs 20 20
    Utilities 8 7
    Other Expense 6 8

    What I want it to do is use a macro to change this to a flat file format like this:

    Feb Mar
    Job 1 Salaries & Wages 1000 1200
    Job 1 Oncosts 50 60
    Job 1 Depreciation 100 100
    Job 1 Consulting 80 0
    Job 1 Occupancy Costs 40 40
    Job 1 Utilities 10 12
    Job 1 Other Expense 3 8
    Job 2 Salaries & Wages 1100 1010
    Job 2 Oncosts 55 51
    Job 2 Depreciation 100 100
    Job 2 Consulting 12 16
    Job 2 Occupancy Costs 20 20
    Job 2 Utilities 8 7
    Job 2 Other Expense 6 8

    Does anyone know how I can do this?

     

    • Post Points: 21
  • Thu, Jun 12 2008 3:56 PM In reply to

    • Nick Hodge
    • Top 10 Contributor
    • Joined on Sun, Dec 23 2007
    • Southampton
    • Posts 234
    • Points 3,866
    • MVP

    Re: Convert spreadsheet report to a flat file

    Woza

    I've made a few assumptions:

    1. You have Excel 97 to 2003 (It will work in 2007, but not if there are over 65536 rows)
    2. Your top left of your data is in A1

    If so, then this code should work.

    Sub MoveData()
    Dim lStartLastrow As Long, lEndLastRow As Long
    Dim myCell As Range
    Dim strJobNo As String
    lStartLastrow = Range("A65536").End(xlUp).Row
    
    Columns("A:A").Insert
    Range("A1").Value = "JobNo."
    Range("B1").Value = "Expense"
    Range("E1").Value = "Index"
    
    strJobNo = "Job1"
    
    With Range("E2")
        .Value = 1
        .AutoFill Range("E2:E" & lStartLastrow), xlFillSeries
    End With
    
    For Each myCell In Range("A2:A" & lStartLastrow)
        If Left(myCell.Offset(0, 1).Value, 3) = "Job" Then
            strJobNo = myCell.Offset(0, 1).Value
            myCell.Offset(0, 1).Clear
        ElseIf myCell.Offset(0, 1).Value = "" Then myCell.Value = ""
        Else: myCell.Value = strJobNo
        End If
    Next myCell
    
    Range("A1:E" & lStartLastrow).Sort Key1:=Range("A2"), Order1:=xlAscending, Key2:=Range("E2") _
            , Order2:=xlAscending, Header:=xlYes
    lEndLastRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
    Range("A" & lEndLastRow & ":A" & lStartLastrow).EntireRow.Delete
    Range("A1:E" & lStartLastrow).Sort Key1:=Range("E2"), Order1:=xlAscending, Header:=xlYes
    Columns("E:E").Delete
    Columns("A:D").AutoFit
    End Sub

     

    Regards
    Nick Hodge
    Microsoft MVP, Excel
    Southampton, UK

    Filed under:
    • Post Points: 21
  • Thu, Jun 12 2008 5:04 PM In reply to

    • Woza2712
    • Top 150 Contributor
    • Joined on Mon, Jun 9 2008
    • Epping, NSW, Australia
    • Posts 2
    • Points 26

    Re: Convert spreadsheet report to a flat file

     Thanks Nick. I will give this a go.

    Regards,

    Woza

     

    • Post Points: 5
Page 1 of 1 (3 items) | RSS
Copyright Excel User Group and the relevant contributors, 2008. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.