Loop Troubles!!

themanof83

Registered User.
Local time
Today, 01:54
Joined
May 1, 2008
Messages
73
Ok,

I'm again at my wits end... I really don't understand why the following code isn't looping through correctly. The code is to pull out a string (path) for each element in the variables array. Then for each element in the three arrays, write the string to the given file i.e. elements 0 first from each array, then elements 1 from each array, then elements 2... etc.
I know that the writing to file part is perfect as it's something I've used a million times. What is seemingly happening is that the code is looping through each If statement individually and incrementing the element number within the If statement, rather than looping through all the If statements for one element then incrementing the element number and looping through the code again.
So instead of getting the first elements from the three arrays I am getting each element from the first array then each element from the second array etc.....

I would very much appreciate your help guys, surely I'm doing something stupid!!

Code:
            For introw = 0 To 20
 
            'Debug.Print CustomCount
            If CustomCount = 0 Then
                'Default to A
                FileSet = "A"
                Chk(introw) = False
            ElseIf CustomCount = CustomMax Then
                'Reset Counter and Increment Set Letter
                CustomCount = 0
                FileSet = IncrementTextString(FileSet)
                Chk(introw) = False
            'Else
                'FileSet = IncrementTextString(FileSet)
            End If
 
            'Debug.Print FileSet
            MyFile = d.Column(2) & d.Column(3) & "\test\AUTOTEST\batch\" & "QTG_" & CustomFile & "_" & FileSet
 
                If Not FileLocked(MyFile) Then
                    'File Number = current iteration
                    fnum = FreeFile()
                    'Open File
                    Open MyFile For Append As fnum
                    'Print to File
                    If (Not (Nz(Path1a(introw), "") = "") And (CustomCount < CustomMax)) Then
                        Print #fnum, Path1a(introw)
                        CustomCount = CustomCount + 1
                        Debug.Print Path1a(introw)
                    End If
                    If (Not (Nz(Path1b(introw), "") = "") And (CustomCount < CustomMax)) Then
                        Print #fnum, Path1b(introw)
                        CustomCount = CustomCount + 1
                        Debug.Print Path1b(introw)
                    End If
                    If (Not (Nz(Path1c(introw), "") = "") And (CustomCount < CustomMax)) Then
                        Print #fnum, Path1c(introw)
                        CustomCount = CustomCount + 1
                        Debug.Print Path1c(introw)
                    End If
                Else
                    'Print to File
                    If (Not (Nz(Path1a(introw), "") = "") And (CustomCount < CustomMax)) Then
                        Print #fnum, Path1a(introw)
                        CustomCount = CustomCount + 1
                    End If
                    If (Not (Nz(Path1b(introw), "") = "") And (CustomCount < CustomMax)) Then
                        Print #fnum, Path1b(introw)
                        CustomCount = CustomCount + 1
                    End If
                    If (Not (Nz(Path1c(introw), "") = "") And (CustomCount < CustomMax)) Then
                        Print #fnum, Path1c(introw)
                        CustomCount = CustomCount + 1
                    End If
                End If
 
            Close #fnum
 
            Debug.Print introw
            Next introw

Cheers!
 
Use subroutines. When I see code repetition like you've got bells go off. Each of those If...End If blocks function exactly the same->Subroutine. Then you have two of those triple If...End If blocks exactly the same->Subroutine. If you logically break down the problem into the smallest possible units of work, and then isolate those units into subroutines, you'll find the bug. If you can't quickly look at a subroutine (read it's name, glance at it's structure) and have a 95% chance of understanding what it does, it's too long->Subroutines.
 
Appreciate the comments about subroutines, and this is something I inherently do once the thing is working.... However, I have found a fundimental flaw in the code itself so will continue churning away... doh!
 

Users who are viewing this thread

Back
Top Bottom