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. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,895
    Location
    It's not that Match does not like the spaces; the problem is that the Budget Category (Budget Savings, Budget Cost, ...) entries no longer match the originals (CostType1, ...) that Match was looking for


    Option Explicit
    
    
    Sub Reformat()
        Dim sOut As String
        Dim wsOut As Worksheet
        Dim rIn As Range, rEnd As Range
        Dim rowIn As Long, rowOut As Long, colIn As Long, colCostType As Long
        
        
        With ActiveSheet
            Set rEnd = .Cells(.Rows.Count, 1).End(xlUp)
            Set rIn = Range(.Cells(3, 1), rEnd)
            Set rIn = Intersect(rIn.EntireRow, rIn.CurrentRegion)
            sOut = .Name & "-Out"
        End With
    
    
        'delete existing
        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", "Budget Savings", "Budget Cost", "Produced", "CostType4", "CostType5", "CostType6", "CostType7", "CostType8", "StartDate", "EndDate", "ExtraCol1", "ExtraCol2")
    
    
        rowOut = rowOut + 1
    
    
        With rIn
            For rowIn = 2 To .Rows.Count
                For colIn = 6 To .Columns.Count
                    'Project
                    wsOut.Cells(rowOut, 1).Value = .Cells(rowIn, 1).Value
                    
                    'description
                    wsOut.Cells(rowOut, 2).Value = .Cells(rowIn, 5).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
    Attached Files Attached Files
    Last edited by Paul_Hossler; 09-01-2021 at 12:27 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

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
  •