任何人都可以在这个逻辑中看到问题吗?
我似乎陷入无限循环,即使代码找到正确的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
答案 0 :(得分:1)
strcomp
的结果。
strcomp("A ","A")
返回1,即使字符串在屏幕上看起来相同。也许trim()
会有所帮助。
除非您需要以非大小写敏感的方式进行比较(例如"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
答案 1 :(得分: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)
答案 2 :(得分: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