NEWBIE,这是否变成意大利面条代码?

时间:2016-02-09 01:58:51

标签: excel vba excel-vba

所以我在试图学习这个问题的时候已经24小时了。我知道这可以简化为更小的方法和功能,但我还没那么远。所以这段代码,它的工作方式,我还没有让它崩溃太难。任何帮助和建议都非常感激。 谢谢你们。

Private Sub CommandButton2_Click()

Dim myInOut As String

Dim myToolType As String

Dim myOrderNumber As String

Dim myQTY As Integer

Dim myToolDesc As String

Dim myEmployee As String

Dim myDate As Date

Dim myLastRowA As Long

Dim myLastRowB As Long

Dim myRow As Long

Dim myNewTool As Long

Dim myNewRow As Long

myDate = DateTime.Now

myLastRowA = ActiveWorkbook.Sheets("Sheet1").Range("A3").End(xlDown).Row - 2

If ActiveWorkbook.Sheets("Sheet1").Cells(3, 2).Value = "" Then

    Exit Sub
End If


'Finds the Matching Row for the In/Out Order Number AND Updates QTY

'MsgBox "LastRowA - " & myLastRowA       'Debugging

For a = 1 To myLastRowA

    myNewTool = True
    myLastRowB = ActiveWorkbook.Sheets("Sheet2").Range("A3").End(xlDown).Row
    'MsgBox "LastRowB - " & myLastRowB & vbCrLf & "OrderNumber - " & myOrderNumber   'Debugging
        With ActiveWorkbook.Sheets("Sheet1")
            myInOut = .Cells(3, 1).Value
            myToolType = .Cells(3, 2).Value
            myOrderNumber = .Cells(3, 3).Value
            myQTY = .Cells(3, 4).Value
            myToolDesc = .Cells(3, 5).Value
            myEmployee = .Cells(3, 6).Value

            For b = 1 To myLastRowB
                If ActiveWorkbook.Sheets("Sheet2").Cells(b, 2).Value = myOrderNumber Then
                    myRow = ActiveWorkbook.Sheets("Sheet2").Cells(b, 2).Row
                    'MsgBox "True " & myRow      'Debugging
                        If myInOut = "IN" Then
                            'Adds In/Out QTY to the Stock QTY
                            ActiveWorkbook.Sheets("Sheet2").Cells(myRow, 4).Value = ActiveWorkbook.Sheets("Sheet2").Cells(myRow, 4).Value + myQTY
                            ActiveWorkbook.Sheets("Sheet1").Rows(3).Delete
                            myNewTool = False
                        ElseIf myInOut = "OUT" Then
                            'Subtracts In/Out QTY to the Stock QTY
                            ActiveWorkbook.Sheets("Sheet2").Cells(myRow, 4).Value = ActiveWorkbook.Sheets("Sheet2").Cells(myRow, 4).Value - myQTY
                            ActiveWorkbook.Sheets("Sheet1").Rows(3).Delete
                            myNewTool = False
                        End If
                End If
            Next b
        If myNewTool = True Then
            myNewRow = myLastRowB + 1
            'MsgBox "NewTool" & myNewTool        'Debugging
            If myInOut = "IN" Then
                ActiveWorkbook.Sheets("Sheet2").Cells(myNewRow, 1).Value = myToolType
                ActiveWorkbook.Sheets("Sheet2").Cells(myNewRow, 2).Value = myOrderNumber
                ActiveWorkbook.Sheets("Sheet2").Cells(myNewRow, 3).Value = myToolDesc
                ActiveWorkbook.Sheets("Sheet2").Cells(myNewRow, 4).Value = myQTY
                ActiveWorkbook.Sheets("Sheet1").Rows(3).Delete
            ElseIf myInOut = "OUT" Then
                MsgBox "THE ORDER NUMBER OF " & myOrderNumber & " IS NOT IN THE DATABASE" & vbCrLf & myToolType & " " & myToolDesc & vbCrLf & "DOES NOT EXIST. PLEASE EDIT YOUR SELECTION."
                Exit Sub
            End If
        End If
    End With
Next a

End Sub

1 个答案:

答案 0 :(得分:1)

这是一个Sub中的很多功能,所以是的,它是朝向意大利面条代码的趋势。

人们对Sub应该有多长时间有不同的看法,但根据经验,你通常希望例程,函数和其他代码单元不超过10-20行;例外情况应该非常罕见。即使在删除注释和空行后,您仍有大约60行。那是在那里起床的。您还有十几个变量,嵌套循环和许多if语句。因此,代码的cyclomatic complexity会变得相当高。

此外,正如评论中所指出的,您多次重复使用多个标识符,例如ActiveWorkbook.Sheets("Sheet1").Range("A3")。这使得代码难以阅读,如果有任何变化会导致代码中断,并在代码运行时影响其中一个值。

所以,是的,你应该试着把它分解成更小的部分。