Results 1 to 20 of 117

Thread: Convert Many Invoice Formats to a Standard Format

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #11
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,886
    Location
    Third try. See if this works for you

    test1.csv is the input you provided a long time ago, and test1-out.csv has a 9 in front of the invoice for matching stores and dates



    Option Explicit
    
    Sub FixCSV()
        Dim sCSV As String
        Dim wbCSV As Workbook
        Dim wsCSV As Worksheet
        Dim rCSV As Range, rCSV1 As Range
        Dim i As Long, j As Long    
        sCSV = Application.GetOpenFilename("ERP File, *.CSV")
        If sCSV = "False" Then Exit Sub
        Application.ScreenUpdating = False    
        Workbooks.Open Filename:=sCSV    
        Set wbCSV = ActiveWorkbook
        Set wsCSV = ActiveSheet    
        With wsCSV      '   Guessing
            .Cells(1, 1).Value = "Date"
            .Cells(1, 2).Value = "Invoice"
            .Cells(1, 3).Value = "Store"
            .Cells(1, 4).Value = "Product"
            .Cells(1, 5).Value = "Qty"
            .Cells(1, 6).Value = "Cost"
            .Cells(1, 7).Value = "InvCred"
            .Cells(1, 8).Value = "Something"    
            Set rCSV = .Cells(1, 1).CurrentRegion
            Set rCSV1 = rCSV.Cells(2, 1).Resize(rCSV.Rows.Count - 1, rCSV.Columns.Count)        
            With .Sort
                .SortFields.Clear
                .SortFields.Add Key:=rCSV1.Columns(1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                .SortFields.Add Key:=rCSV1.Columns(3), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                .SortFields.Add Key:=rCSV1.Columns(7), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal        
                .SetRange rCSV
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With   
            .Cells(2, 1).Select
        End With        
        With ActiveWindow
            .SplitColumn = 0
            .SplitRow = 1
        End With
        ActiveWindow.FreezePanes = True    
        With rCSV
            For i = 2 To .Rows.Count
                If .Cells(i, 7).Value = "C" Then    '  CREDIT?
                    j = i
                    'same store and same date
                    Do While (.Cells(j, 3).Value = .Cells(i - 1, 3).Value) And _
                        (.Cells(j, 1).Value = .Cells(i - 1, 1).Value)
                        .Cells(j, 2).Value = "'9" & .Cells(i - 1, 2).Value   '   add leading 9
                        .Cells(j, 7).Value = "-C"    '  add marker
                        j = j + 1
                    Loop
                End If
            Next i    
            Call .Columns(7).Replace("-C", "C", xlWhole)
        End With    
        i = InStrRev(wbCSV.Name, ".")
        sCSV = wbCSV.Path & Application.PathSeparator & Left(wbCSV.Name, i - 1) & "-out.csv"    
        Application.DisplayAlerts = False
        On Error Resume Next
        Kill sCSV
        On Error GoTo 0
        Application.DisplayAlerts = True    
        wbCSV.SaveAs sCSV, xlCSV
        wbCSV.Close False    
        Application.ScreenUpdating = False    
        MsgBox "CSV Converted as " & sCSV    
    End Sub
    Attached Files Attached Files
    Last edited by Aussiebear; 05-04-2025 at 01:03 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

Posting Permissions

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