2

谁能看到这个逻辑中的问题?

我似乎陷入了无限循环,即使找到正确的 acqCode,代码也只会转到下一页。

 flagFound = 0

 Do 
        If StrComp(temp1, acqCode) = 0 then 
            Print #iOutFileNum, "It's 1st!!!! :)" 
            flagFound = 1
        Elseif StrComp(temp2, acqCode) = 0 then
            Print #iOutFileNum, "It's 2nd!!!! :)" 
            flagFound = 1
        Elseif StrComp(temp3, acqCode) = 0 then
            Print #iOutFileNum, "It's 3rd!!!! :)" 
            flagFound = 1
        Elseif StrComp(temp4, acqCode) = 0 then
            Print #iOutFileNum, "It's 4th!!!! :)"
            flagFound = 1
        Elseif StrComp(temp5, acqCode) = 0 then
            Print #iOutFileNum, "It's 5th!!!! :)"
            flagFound = 1
        Elseif StrComp(temp6, acqCode) = 0 then
            Print #iOutFileNum, "It's 6th!!!! :)"
            flagFound = 1
        Else
            If flagFound = 0 then 
                'go to next page
            End If
        End If
    Loop Until flagFound = 1'to end Do While
4

3 回答 3

1

检查逻辑,我找不到任何问题。我会看看的结果strcomp

strcomp("A ","A")返回 1,即使字符串在屏幕上看起来相同。也许 atrim()会有所帮助。

除非您需要以不区分大小写的方式进行比较(例如"ABC" = "abc"),否则直接字符串比较可能更具可读性。该if..elseif..else构造也可以重写为select case

flagFound = 0
Do
    Select Case acqcode
        Case temp1
            Debug.Print "It's 1st!!!! :)"
            flagFound = 1
        Case temp2
            Debug.Print "It's 2nd!!!! :)"
            flagFound = 1
        Case temp3
            Debug.Print "It's 3rd!!!! :)"
            flagFound = 1
        Case temp4
            Debug.Print "It's 4th!!!! :)"
            flagFound = 1
        Case temp5
            Debug.Print "It's 5th!!!! :)"
            flagFound = 1
        Case temp6
            Debug.Print "It's 6th!!!! :)"
            flagFound = 1
        Case Else
            Debug.Print "next page"
    End Select
Loop Until flagFound = 1 'to end Do While
于 2012-08-21T20:34:33.690 回答
0

尝试这个

flagFound = 0

Do 
    If StrComp(temp1, acqCode) = 0 then 
        Print #iOutFileNum, "It's 1st!!!! :)" 
        flagFound = 1
    Elseif StrComp(temp2, acqCode) = 0 then
        Print #iOutFileNum, "It's 2nd!!!! :)" 
        flagFound = 1
    Elseif StrComp(temp3, acqCode) = 0 then
        Print #iOutFileNum, "It's 3rd!!!! :)" 
        flagFound = 1
    Elseif StrComp(temp4, acqCode) = 0 then
        Print #iOutFileNum, "It's 4th!!!! :)"
        flagFound = 1
    Elseif StrComp(temp5, acqCode) = 0 then
        Print #iOutFileNum, "It's 5th!!!! :)"
        flagFound = 1
    Elseif StrComp(temp6, acqCode) = 0 then
        Print #iOutFileNum, "It's 6th!!!! :)"
        flagFound = 1
    End If

       If flagFound = 0 then 
            'go to next page
       End If

Loop Until flagFound = 1'to end Do While
于 2012-08-25T20:02:37.923 回答
0

我还用我自己的假数据检查了你的逻辑(我使用 Excel 进行测试,但如果你使用的是另一个应用程序,基本原理仍然相同)。

使用您的确切逻辑,我能够在 6 个条件中的任何一个条件下使循环终止于我想要的任何页面。所以这意味着你的一般逻辑是合理的。

由于您没有显示如何temp*填充变量或显示源数据,我可以得出的最可能的结论是您没有有效条件。

由于您的陈述,您有一个无限循环,因此只有另一种可能性被忽略。你的循环是否有可能实际上不是无限的(如果你会用完页面或出现溢出或我们的内存错误)?如果循环不是无限的,那么这可能意味着问题可能出在您的Print陈述中。尝试用 Debug.Print 替换它们,不要写入外部文件。如果它运行正确,那么这可能是你的问题。如果它仍然表现相同,那么这意味着您的问题必须是您的 String 比较永远不会正确的事实。

但是,如果它被锁定或似乎处于无限循环中,则您的Print语句中的文件可能试图写入也从未打开过。Print您没有显示它,但我相信您需要在尝试使用该语句写入文件之前执行以下操作:

fileNum = FreeFile ' next file number
Open file For Append As #fileNum  'or Output instead of append for a new file 
                                  ' (overwriting existing file)
于 2012-08-22T00:19:49.487 回答