难道你不知道代码缩进么?
If xxx Then
    If xxx Then
        For xxx
            xxx
        Next
    Else
        xxxx
    End If
    For xxx
        If xxx Then
            xxx
        End If
        xxx
    Next
End If
xxx

解决方案 »

  1.   

    另外,我的习惯是,写一个 If 语句后,预先加上(Else 如果有)End If,再返回来写中间的语句。这就保证了 If 和 End If 总是成对的。一般来说,即便写多层嵌套的 If 和 End If,我写代码时脑子里一定会有整个架构的图景,以及当前所写代码在架构中所处的位置。这有点像棋手的“下盲棋”。因此,事前想好一段代码的逻辑很重要,不要临时修修补补。
      

  2.   

    举个实例:
    '按名称模糊查找
            If Left(Text1.Text, 1) = ":" Then '[105]
                Text1.Text = Right(Text1.Text, Len(Text1.Text) - 1)
                LV1.ListItems.Clear
                s = "select * from tmb where mc like '%" & Replace(Text1.Text, " ", "") & "%'"
                RST.Open s, CNN
                Do Until RST.EOF
                    LV1.ListItems.Add , , RST.Fields("mc").Value
                    LV1.ListItems(LV1.ListItems.Count).SubItems(1) = RST.Fields("dj").Value
                    LV1.ListItems(LV1.ListItems.Count).SubItems(2) = 0
                    LV1.ListItems(LV1.ListItems.Count).SubItems(4) = RST.Fields("tm").Value
                    RST.MoveNext
                Loop
                Text1.Text = ""
            Else '[105]
    '--------------------------------------------------------------------------------------------------------------------------------------------------
            If Right(Text1.Text, 1) = "." Then Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) '最后是点的,删了
        If Text1.Text = Empty Then '[104]
            If LV1.ListItems.Count > 0 And Shift = 1 Then  '结帐
                s = "select max(dh) as zddh from xcb" '营业表
                RST.Open s, CNN
                d = IIf(RST.Fields("zddh").Value > 0, RST.Fields("zddh").Value + 1, 1)
                RST.Close
                For i = 1 To LV1.ListItems.Count
                    s = "insert into xcb(dh,[date],mc,dj,sl,zj,tm) values(" & d & ",#" & Now & "#,'" & LV1.ListItems(i) & "'," & _
                        LV1.ListItems(i).SubItems(1) & "," & LV1.ListItems(i).SubItems(2) & "," & LV1.ListItems(i).SubItems(3) & "," & LV1.ListItems(i).SubItems(4) & ")"
                    RST.Open s, CNN
                    s = "select * from dmb where tm=" & CheckString(LV1.ListItems(i).SubItems(4)) '店面表
                    RST.Open s, CNN, 1, 3
                    If RST.RecordCount > 0 Then
                    'If RST.Fields("sl").Value > 0 Then
                        RST.Fields("sl").Value = IIf(RST.Fields("sl").Value > LV1.ListItems(i).SubItems(2), RST.Fields("sl").Value - LV1.ListItems(i).SubItems(2), 0) '减去相应的数量,以后还要完善
                        RST.Update
                    'End If
                    'Else
                    End If
                    RST.Close
                Next i
                LV1.ListItems.Clear
                WritTextFile "结帐:" & d & "," & Now
                StatusBar1.Panels(1).Text = "结帐完成"
            End If
        Else '[104]
    '将输入信息赋值到数组------------------------------------------------------------------------------------------------------------------------------
            arr() = Split(Text1.Text, ",")
            If UBound(arr) < 1 Then Text1.Text = Text1.Text & ",0" '没有逗号,人工加上(这里不能&1,因为要做一个有无数量输入的判断)
            arr() = Split(Text1.Text, ",")
            Text1.Text = ""
    '不在数据库查找的,直接加入------------------------------------------------------------------------------------------------------------------------
            If IsNumeric(arr(0)) And IsNumeric(arr(1)) Then '当单价数量是数字时,分二行,减少错误
                If arr(0) < 9999 And arr(0) > 0 And arr(1) < 9999 And arr(1) >= 0 Then '直接加入散装物品
                    LV1.ListItems.Add , , "散装物品"
                    LV1.ListItems(LV1.ListItems.Count).SubItems(1) = arr(0) '单价
                    'LV1.ListItems(LV1.ListItems.Count).SubItems(2) = IIf(arr(1) = 0, 1, arr(1)) '数量
                    If arr(1) > 0 Then LV1.ListItems(LV1.ListItems.Count).SubItems(2) = arr(1) '数量
                    Text1.Text = "1"
                End If
            End If
    '用SQL语句打开记录集-------------------------------------------------------------------------------------------------------------------------------
            If Text1.Text <> "1" And IsNumeric(arr(1)) Then '[103]
              s = "select * from tmb where tm=" & CheckString(Left(arr(0), 22)) '" & Left(arr(0), 22) & "'" '条码表
              RST.Open s, CNN
    '--------------------------------------------------------------------------------------------------------------------------------------------------
            'If IsNull(RST.Fields("tm").Value) Then '条形码空
            If RST.RecordCount = 0 Then 'Or (RST.RecordCount > 0 And RST.Fields("dj").Value = 0) Then '无记录 [101]
                frmDB.Text1(0).Text = arr(0)
                frmDB.Show
                frmDB.Move Me.Left + Me.Width, 0
            Else '以下有记录 [101]
            If RST.Fields("dj").Value = 0 Then '有记录但单价为0,这里不能合到上面语句,否则要产生错误 [107]
                frmDB.Text1(0).Text = arr(0)
                frmDB.Show
                frmDB.Move Me.Left + Me.Width, 0
            Else '[107]
            If RST.Fields("cm").Value = True Then '重码[102]
                s = InputBox("货品重码,请输入单价:")
                If s = "" Or Not IsNumeric(s) Then s = 0
                If s < 9999 And s > 0 Then
                    LV1.ListItems.Add , , "重码物品"
                    LV1.ListItems(LV1.ListItems.Count).SubItems(1) = s
                    StatusBar1.Panels(1).Text = "重码物品"
                'Else
                    'StatusBar1.Panels(1).Text = "输入有误"
                End If
            Else '[102]
    '--------------------------------------------------------------------------------------------------------------------------------------------------
                Set Lvist1 = LV1.FindItem(RST.Fields("tm").Value, lvwSubItem)
                If Lvist1 Is Nothing Then
                    B = 0
                Else
                    Lvist1.Selected = True
                    B = LV1.SelectedItem.Index
                End If
                Set Lvist1 = Nothing
                'For i = 1 To LV1.ListItems.Count
                '    If LV1.ListItems(i).SubItems(4) = RST.Fields("tm").Value Then '列表已有
                '        B = i
                '        Exit For
                '    End If
                'Next i
    '最常用的新扫描入来的物品--------------------------------------------------------------------------------------------------------------------------
                If RST.Fields("szwp").Value = 0 Or arr(1) > 0 Then '[106]
                    If B = 0 Then '列表没有
                        LV1.ListItems.Add , , RST.Fields("mc").Value
                        LV1.ListItems(LV1.ListItems.Count).SubItems(1) = RST.Fields("dj").Value '单价
                        If arr(1) > 0 Then LV1.ListItems(LV1.ListItems.Count).SubItems(2) = arr(1) '数量
                        LV1.ListItems(LV1.ListItems.Count).SubItems(4) = RST.Fields("tm").Value '要此条码辨别列表是否存在
                    Else '列表已有
                        s = IIf(arr(1) = 0, 1, arr(1))
                        LV1.ListItems(B).SubItems(2) = Val(LV1.ListItems(B).SubItems(2)) + s
                        LV1.ListItems(B).SubItems(3) = LV1.ListItems(B).SubItems(1) * s + LV1.ListItems(B).SubItems(3)
                    End If 'If B = 0 Then '列表没有
                Else '是散装计量物品[106]
                    s = InputBox("计量物品,请输入数量:", , 1)
                    If Not IsNumeric(s) Or s = "" Then s = 0 '这里要分二行,减少一个Err
                    If s > 9999 Then s = 0
                        'LV1.ListItems(LV1.ListItems.Count).SubItems(2) = IIf(IsNumeric(s) = True And s > 0 And s < 9999, s, 1) '数量
                    If s > 0 Then
                        If B = 0 Then '列表没有
                            LV1.ListItems.Add , , RST.Fields("mc").Value
                            LV1.ListItems(LV1.ListItems.Count).SubItems(1) = RST.Fields("dj").Value '单价
                            LV1.ListItems(LV1.ListItems.Count).SubItems(2) = s '数量
                            'If s = 0 Then LV1.ListItems.Remove LV1.ListItems.Count '删了
                            LV1.ListItems(LV1.ListItems.Count).SubItems(4) = RST.Fields("tm").Value '要此条码辨别列表是否存在
                        Else '列表已有
                            LV1.ListItems(B).SubItems(2) = Val(LV1.ListItems(B).SubItems(2)) + s
                            LV1.ListItems(B).SubItems(3) = LV1.ListItems(B).SubItems(1) * s + LV1.ListItems(B).SubItems(3)
                        End If
                    End If
                End If '[106]
    '--------------------------------------------------------------------------------------------------------------------------------------------------
                If LV1.ListItems.Count > 0 Then StatusBar1.Panels(1).Text = "等待结帐"
            End If '[102]
            End If '[107]
            End If '[101]
            RST.Close
          End If '[103]
          Text1.Text = ""
        End If '[104]
        End If '[105]
    以上代码有缩进吧,[107]是发现错误才添加的,有了编号方便准确
      

  3.   

    select case ...
        case ...
            ....
        case ....
            ....
        case ....
            ....
        case else
            ....
    end select
      

  4.   

    逻辑设计尽可能简化,去掉不必要的 Else。语句之间的逻辑关系要清晰易读。另外,缩进要规整。:    If Right(Text1.Text, 1) = "." Then Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1) '最后是点的,删了    If Left(Text1.Text, 1) = ":" Then
            Text1.Text = Mid(Text1.Text, 1)
            s = "select * from tmb where mc like '%" & Replace(Text1.Text, " ", "") & "%'"
            RST.Open s, CNN
            LV1.ListItems.Clear
            Do Until RST.EOF
                LV1.ListItems.Add , , RST.Fields("mc").Value
                LV1.ListItems(LV1.ListItems.Count).SubItems(1) = RST.Fields("dj").Value
                LV1.ListItems(LV1.ListItems.Count).SubItems(2) = 0
                LV1.ListItems(LV1.ListItems.Count).SubItems(4) = RST.Fields("tm").Value
                RST.MoveNext
            Loop
            Text1.Text = ""
            Exit Sub '退出过程,省去 Else 判断
        End If 
    '--------------------------------------------------------------------------------------------------------------------------------------------------
        
        If Text1.Text = Empty And LV1.ListItems.Count > 0 And Shift = 1 Then  '结帐
            s = "select max(dh) as zddh from xcb" '营业表
            RST.Open s, CNN
            d = IIf(RST.Fields("zddh").Value > 0, RST.Fields("zddh").Value + 1, 1)
            RST.Close
            For i = 1 To LV1.ListItems.Count
                s = "insert into xcb(dh,[date],mc,dj,sl,zj,tm) values(" & d & ",#" & Now & "#,'" & LV1.ListItems(i) & "'," & _
                    LV1.ListItems(i).SubItems(1) & "," & LV1.ListItems(i).SubItems(2) & "," & LV1.ListItems(i).SubItems(3) & "," & LV1.ListItems(i).SubItems(4) & ")"
                CNN.Execute s '不返回记录集的语句不要用打开记录集命令
                s = "select * from dmb where tm=" & CheckString(LV1.ListItems(i).SubItems(4)) '店面表
                RST.Open s, CNN, 1, 3
                If Not RST.EOF Then
                    RST.Fields("sl").Value = IIf(RST.Fields("sl").Value > LV1.ListItems(i).SubItems(2), RST.Fields("sl").Value - LV1.ListItems(i).SubItems(2), 0) '减去相应的数量,以后还要完善
                    RST.Update
                End If
                RST.Close
            Next i
            LV1.ListItems.Clear
            WritTextFile "结帐:" & d & "," & Now
            StatusBar1.Panels(1).Text = "结帐完成"
            Exit Sub
        End If    If InStr(Text1, ".") < 1 Then Text1.Text = Text1.Text & ",0" '没有逗号,人工加上(这里不能&1,因为要做一个有无数量输入的判断)
        arr() = Split(Text1.Text, ",")
    '......总之,写代码之前,要先做逻辑设计,不要写到哪里算哪里。
      

  5.   

    关于 Select Case:Case 是可以嵌套的。但是,Case 的判断内容必须是统一的,像楼主这样判断一个字符串不同部分的内容,用 Select Case 就反而复杂化。当然了,这与最初的协议设计有关。如果事先设计了规范的 Flag,用 Case 可能更简单。另外,还可以把具体业务设计成子过程,在分支判断代码段中仅仅根据 Flag 调用业务子过程,代码就更清晰了。
      

  6.   

    也是一个不错的想法
    即便是有了缩进,当一大堆if endif混在一起的时候确实不好肉眼匹配
      

  7.   

    个人认为中间最好不要有exit sub,因为到最后还要清内存、显示操作状态等等。
      

  8.   

    有些功能可以写到单独的函数或者过程,这样也可以清晰很多
    if …… Then 
      call ……
    else
      call ……
    endif