如果VBA中的语句不能正常工作

时间:2018-05-15 12:50:28

标签: excel vba excel-vba if-statement

由于某种未知的原因,我的If语句不能正常工作,如果寻求任何改进,我试图反转信号,但到目前为止都没有。留下一些代码。

For i = 1 To last
    REFERENCIA = Application.ThisWorkbook.Worksheets("Registos").Cells(i, 8)
    ENTRADAeSAIDA = Application.ThisWorkbook.Worksheets("Registos").Cells(i, 12)
    CC = Application.ThisWorkbook.Worksheets("Registos").Cells(i, 6)

    If REFERENCIA = registos.TextBox1 And registos.ComboBox1 = "SAÍDA" Then

         Worksheets("registos").Select
         ActiveSheet.ListObjects("Tabela1").Range.AutoFilter Field:=8, Criteria1:= _
         registos.TextBox1.Text             'Filtrar referência

         ActiveSheet.ListObjects("Tabela1").Range.AutoFilter Field:=6, Criteria1:= _
         registos.Label11.Caption           'Filtrar CC


         ActiveSheet.ListObjects("Tabela1").Range.AutoFilter Field:=12, Criteria1:= _
        "ENTRADA"                                'Filtrar Entrada

        'Somar quantidades de peças de Entrada
        xty100 = ThisWorkbook.Worksheets("calculos").Range("A1")


    ''       Worksheets("registos").Select
        ActiveSheet.ListObjects("Tabela1").Range.AutoFilter Field:=12, Criteria1:= _
        "SAÍDA"                                            'Filtrar Saída
        xty101 = ThisWorkbook.Worksheets("calculos").Range("A1")
    '        'Somar quantidade de peças de saída

        sumfinal = xty100 - xty101
        xytbx4 = Format(registos.TextBox4, "@")

     If xytbx4 < sumfinal And xytbx4 > 0 Then
    GoTo salto_2

    Else
    MsgBox "Não é possível movimentar mais que o stock atual!Stock " & sumfinal & ""
    GoTo fim
    End If
    End If
Next i

2 个答案:

答案 0 :(得分:2)

你没有修改你的变量,而是来自

 sumfinal = xty100 - xty101   

看来你期望sumfinal是数字。但是来自

xytbx4 = Format(registos.TextBox4, "@")

xytbx4总是一个字符串(因为那是格式返回的),数值为零 - 所以你的IF永远不会是真的

答案 1 :(得分:0)

xytbx4之前检查sumfinalIf的2个值:

MsgBox "xytbx4: " & xtybx4
MsgBox "sumfinal: " & sumfinal

If xytbx4 < sumfinal And xytbx4 > 0 Then

这将返回2 MsgBox,其中包含变量的值。