Results 1 to 20 of 27

Thread: Filter a text file and produce a new one

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #23
    VBAX Regular
    Joined
    Jan 2018
    Posts
    55
    Location
    RIC,

    I'm not sure this is what you want but please give it a try.

    Sub PuregeFile()
        Dim vPar As Integer
        Dim vMin(2) As Integer, vMax(2) As Integer
        Dim vTotalMax As Integer
        Dim rng(16) As Range
        Dim i As Integer, j As Integer, n As Integer
        Dim ReadLine As String
        Dim buf
        Dim cnt1 As Integer, cnt2 As Integer, cnt3 As Integer
        
        vPar = Range("Y8")
        vMin(0) = Range("AV4"): vMin(1) = Range("AV6"): vMin(2) = Range("AV8")
        vMax(0) = Range("AX4"): vMax(1) = Range("AX6"): vMax(2) = Range("AX8")
        vTotalMax = Range("AZ7")
        
        For i = 0 To 16
            Set rng(i) = Range("H10").Offset(, i * 3).Resize(15, 3)
        Next i
        
        Open ThisWorkbook.Path & "\_ToPurgeFile.csv" For Input As #1
        Open ThisWorkbook.Path & "\OutputFile.csv" For Output As #2
        
        Do Until EOF(1)
            Line Input #1, ReadLine
            buf = Split(ReadLine, " ")
            cnt3 = 0
            For n = 0 To 2
                cnt1 = 0
                For i = n * 4 + 2 To n * 4 + 5
                    cnt2 = 0
                    For j = 0 To 5
                        cnt2 = cnt2 + WorksheetFunction.CountIf(rng(i), buf(j))
                    Next j
                    If cnt2 = vPar Then
                        cnt1 = cnt1 + 1
                    End If
                Next i
                If (cnt1 >= vMin(n) And cnt1 <= vMax(n)) Then
                    cnt3 = cnt3 + cnt1
                End If
            Next n
            If cnt3 > 0 And cnt3 <= vTotalMax Then
                Print #2, ReadLine
            End If
        Loop
        
        Close #2
        Close #1
    End Sub
    Attached Files Attached Files

Posting Permissions

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