Results 1 to 11 of 11

Thread: VBA to export data with altered format to a csv

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    VBAX Regular
    Joined
    Jan 2013
    Posts
    7
    Location
    Quote Originally Posted by Paul_Hossler View Post


    Option Explicit
    
    
    Sub Reformat()
        Dim sOut As String
        Dim wsOut As Worksheet
        Dim rIn As Range
        Dim rowIn As Long, rowOut As Long, colIn As Long, colCostType As Long
        
        Set rIn = ActiveSheet.Cells(1, 1).CurrentRegion
    
    
    
    
        'delete existing
        sOut = ActiveSheet.Name & "-Out"
        Application.DisplayAlerts = False
        On Error Resume Next
        Worksheets(sOut).Delete
        Application.DisplayAlerts = True
        On Error GoTo 0
        
        Set wsOut = Worksheets.Add
        ActiveSheet.Name = sOut
        rowOut = 1
        
        wsOut.Cells(1, 1).Resize(1, 14).Value = Array("Proj", "Description", "CostType1", "CostType2", "CostType3", "CostType4", "CostType5", "CostType6", "CostType7", "CostType8", "StartDate", "EndDate", "ExtraCol1", "ExtraCol2")
    
    
        rowOut = rowOut + 1
    
    
        With rIn
            For rowIn = 2 To .Rows.Count
                For colIn = 4 To .Columns.Count
                    'Project
                    wsOut.Cells(rowOut, 1).Value = .Cells(rowIn, 1).Value
                    
                    'description
                    wsOut.Cells(rowOut, 2).Value = .Cells(rowIn, 3).Value
    
    
                    'cost type
                    colCostType = Application.WorksheetFunction.Match(.Cells(rowIn, 2).Value, wsOut.Rows(1), 0)
                    
                    'if there's a value
                    If .Cells(rowIn, colIn).Value > 0 Then
                        wsOut.Cells(rowOut, colCostType).Value = .Cells(rowIn, colIn).Value
                        wsOut.Cells(rowOut, 11).Value = DateSerial(.Cells(1, colIn).Value, 1, 1)
                        wsOut.Cells(rowOut, 12).Value = DateSerial(.Cells(1, colIn).Value + 1, 1, 1)
                        
                        rowOut = rowOut + 1
                    End If
                Next colIn
            Next rowIn
        End With
    
    
    
    
        'delete the extra empty row
        With wsOut.Cells(1, 1).CurrentRegion
            If Len(Cells(.Rows.Count, 12).Value) = 0 Then .Rows(.Rows.Count).Delete
        End With
        
    
    
    
    
    End Sub
    This is awesome and works beautifully in my example! I figured I'd be able to adapt it for my needs, but my actual spreadsheet isn't quite structured the way it is in my example. The headers start on row 3 and data on row 4. Col C and D can be ignored. Some of the actual budget categories have spaces and Match doesn't seem to like that. Also, all cells in the source are formatted as General.

    Proj Budget Category junk junk Desc 2021 2022 2023 2024
    Proj1 Budget Savings words1 123 4545
    Proj2 Budget Cost words2 515 321 4545
    Proj1 Produced words3 54 544
    Proj3 Budget Savings words4 5654

    I apologize! I was attempting to make it easier for others to offer help, but may have produced the opposite result...
    Last edited by Drivium; 09-01-2021 at 10:41 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •