2017-03-08 41 views
1

不同VBA警报我有这样的代码:当结果是由0

DM.OnUpdate="CalculDiferenta" 
DM.OnInsert="CalculDiferenta" 

Sub CalculDiferenta 

If Dsrid.Value=50000 Then 

stl.first 
Do While Not Stl.Eof 
Diferenta.Value=Cantv.Value-Cantc.Value 


Stl.Next 
Loop 

end if 
End Sub 

它计算在文档中的2点数量的列之间的差异。 现在,我想要提醒,如果有任何区别(Cantv.Value-Cantc.Value <> 0)。代码应该检查文档的每一行是否有差异,当它找到第一个时,停止并显示msgbox。

我这样做了,但我不确定没关系。当最后一行有差异时,它显示出弹出。

DM.OnUpdate="VerificareDiferente" 
DM.OnInsert="VerificareDiferente" 

Public Sub VerificareDiferente 

If Dsrid.value=50000 and Cantv.Value-Cantc.Value <> 0 then 

stl.first 
      Do While Not Stl.Eof 
     MsgBox "Exista diferente intre cantitate comandata si cantitate verificata.", vbInformation, "Atentie !!!" 
      Stl.Next 
      Loop 
     end if 
End Sub 

你能帮助我吗?谢谢。

+3

考虑编辑代码块的清晰度。我怀疑if和while语句的顺序有错误。 – putonspectacles

回答

2

你只是放错了地方的If说法,应该是内环路:

DM.OnUpdate = "VerificareDiferente" 
DM.OnInsert = "VerificareDiferente" 

Public Sub VerificareDiferente() 
    Stl.first 
    Do While Not Stl.EOF 
     If Dsrid.Value = 50000 And Cantv.Value - Cantc.Value <> 0 Then 
      MsgBox "Exista diferente intre cantitate comandata si cantitate verificata.", _ 
       vbInformation, "Atentie !!!" 
     Else 
     End If 
     Stl.Next 
    Loop 
End Sub 
+0

太棒了,就是这个问题:D –

+0

我可以再问一个问题吗? 它弹出一个msgbox的每一行是差异。即使我有1行差异或“n”行,我怎样才能弹出一个msgbox? –

+1

@IonutP。在循环内部添加一个计数器('i = i + 1'或者smthg就是这样),并且把循环后面的'MsgBox'放在一个测试中,比如'If i> 0 Then MsgBox';) – R3uK