Excel VBA最佳实践:一个长宏还是多个小宏?

Excel VBA最佳实践:一个长宏还是多个小宏?,vba,excel,Vba,Excel,寻找一些关于编写宏的最佳实践的建议。我目前正在写一个,当完成时,将有大约200个IF/ELSE条款。这些条款中的每一条都有大约10个IF/ELSE子条款 从安排的角度来看,这将是非常混乱的 然而,从最佳实践的角度来看,最好是: 每个子句调用另一个宏,该宏将包含子类 是否将子项保留在主宏的整个主体中 此外,这些选项如何影响宏的速度(如选项1比选项2慢还是快)?它正在处理的电子表格将有大约5万行,约13列 编辑1: 这是代码开头的一个示例(非常长且凌乱): 如果单元格(j,10)=“AT”,则

寻找一些关于编写宏的最佳实践的建议。我目前正在写一个,当完成时,将有大约200个IF/ELSE条款。这些条款中的每一条都有大约10个IF/ELSE子条款

从安排的角度来看,这将是非常混乱的

然而,从最佳实践的角度来看,最好是:

  • 每个子句调用另一个宏,该宏将包含子类
  • 是否将子项保留在主宏的整个主体中
此外,这些选项如何影响宏的速度(如选项1比选项2慢还是快)?它正在处理的电子表格将有大约5万行,约13列

编辑1:

这是代码开头的一个示例(非常长且凌乱):

如果单元格(j,10)=“AT”,则
ElseIf单元格(j,10)=“BE”然后“BE”
如果单元格(j,9)“,则
如果左(单元格(j,9),2)=“B-”,右(单元格(j,9,1))=真,Len(单元格(j,9))=6,则
单元(j,8)=单元(j,9)和单元(j,8)
细胞(j,9)。透明内容物
单元格(j,8)。Style=“输入”
ElseIf IsNumeric(左(单元格(j,9),2))=真,IsNumeric(右(单元格(j,9,1))=真
单元(j,9)=“B-”和单元(j,9)
单元(j,8)=单元(j,9)和单元(j,8)
细胞(j,9)。透明内容物
单元格(j,8)。Style=“输入”
ElseIf IsNumeric(右(单元格(j,9),2))=真,左(单元格(j,9),2)“B-”然后
单元(j,9)=修剪(右单元(j,9,6)和“&左单元(j,9),Len单元(j,9))-6))
单元格(j,9).Style=“输入”
如果结束
如果结束
ElseIf单元格(j,10)=“CH”然后“CH”
如果Len(单元(j,9))=4,则
单元(j,9)=“CH-”&单元(j,9)
Selection.Style=“输入”
'ElseIf Len(Cells(j,9))=7,Left(Cells(j,9),2)=“CH”,然后修复将邮政编码移动到城市名称之前
'Selection.Style=“中立”
如果结束
ElseIf单元格(j,10)=“DE”然后是“德语格式”
如果左(单元格(j,9),1)“D”,则
单元(j,9)=“D-”和单元(j,9)
Selection.Style=“中立”
如果结束
如果IsNumeric(右(单元格(j,9),5))=True,Len(单元格(j,9))=7,则
如果单元格(j,8)“,则
单元(j,8)=单元(j,9)和单元(j,8)
细胞(j,9)。透明内容物
单元格(j,8)。Style=“输入”
ElseIf细胞(j,8)=“然后
单元(j,7)=单元(j,9)和单元(j,7)
单元格(j,7)。Style=“输入”
细胞(j,9)。透明内容物
如果结束
ElseIf为数值型(右(单元格(j,9),5))和Len(单元格(j,9))>8
如果Mid(单元(j,9),Len(单元(j,9)-7),1)=“D”,那么
单元(j,9)=修剪(右侧单元(j,9),7)和“&左侧单元(j,9),Len单元(j,9))-7))
单元格(j,9).Style=“输入”
如果结束
其他的
单元格(j,9)。Style=“坏”
如果结束
ElseIf单元格(j,10)=“IE”然后是“Irish”
如果单元格(j,9)“,则
如果IsNumeric(右(单元格(j,9),1))=True和(Len(单元格(j,9))=9或Len(单元格(j,9))=8),则
ElseIf(Len(Cells(j,9))8或Len(Cells(j,9))9)和Left(Cells(j,9),6)=“都柏林”
如果Mid(单元格(j,9),Len(单元格(j,9))-1,1)=“那么
单元格(j,9)=左侧(单元格(j,9),6)和右侧(单元格(j,9),1)
单元格(j,9).Style=“输入”
ElseIf Mid(细胞(j,9),Len(细胞(j,9))-2,1)=“然后
单元格(j,9)=左侧(单元格(j,9),6)和右侧(单元格(j,9),2)
单元格(j,9).Style=“输入”
如果结束
左侧(单元格(j,9),1)=“A”或左侧(单元格(j,9),1)=“B”或左侧(单元格(j,9),1)=“E”或左侧(单元格(j,9),1)=“F”_
或左(单元格(j,9),1)=“H”或左(单元格(j,9),1)=“I”或左(单元格(j,9),1)=“j”或左(单元格(j,9),1)=“N”_
或左(单元格(j,9),1)=“O”或左(单元格(j,9),1)=“P”或左(单元格(j,9),1)=“Q”或左(单元格(j,9),1)=“B”_
或左(单元格(j,9),1)=“U”或左(单元格(j,9),1)=“V”或左(单元格(j,9),1)=“W”或左(单元格(j,9),1)=“X”_
或左(单元格(j,9),1)=“Y”或左(单元格(j,9),1)=“Z”,然后
单元格(j,9)。Style=“坏”
ElseIf Len(细胞(j,9))>16然后
单元格(j,9)。Style=“坏”
ElseIf(Len(Cells(j,9))8或Len(Cells(j,9))9)和Left(Cells(j,9),6)“都柏林”
如果结束
如果结束
如果单元格(j,8)=“都柏林”,左侧单元格(j,9,6)=“都柏林”,则
细胞(j,8)。透明内容物
ElseIf单元格(j,7)=“都柏林”,左侧单元格(j,9,6)=“都柏林”
细胞(j,7)。透明内容物
ElseIf单元格(j,8)=“软木塞”,左侧单元格(j,9),4)=“软木塞”
细胞(j,8)。透明内容物
如果结束
ElseIf单元格(j,10)=“US”然后是“US”
b=Len(单元(j,9))
如果单元格(j,10)=“US”且b<5且b>1,则
单元格(j,9)。选择
Selection.NumberFormat=“@”
如果b=4,则
单元(j,9)=“0”和单元(j,9)
其他的
单元(j,9)=“00”和单元(j,9)
如果结束
ElseIf细胞(j,10)=“US”和Mid(细胞(j,9),3,1)=“和Len(细胞(j,9))=7
单元格(j,9)=左
If Cells(j, 10) = "AT" Then

    ElseIf Cells(j, 10) = "BE" Then                           ' Belgian formatting
        If Cells(j, 9) <> "" Then
            If Left(Cells(j, 9), 2) = "B-" And IsNumeric(Right(Cells(j, 9), 1)) = True And Len(Cells(j, 9)) = 6 Then
                Cells(j, 8) = Cells(j, 9) & " " & Cells(j, 8)
                Cells(j, 9).ClearContents
                Cells(j, 8).Style = "input"
            ElseIf IsNumeric(Left(Cells(j, 9), 2)) = True And IsNumeric(Right(Cells(j, 9), 1)) = True Then
                Cells(j, 9) = "B-" & Cells(j, 9)
                Cells(j, 8) = Cells(j, 9) & " " & Cells(j, 8)
                Cells(j, 9).ClearContents
                Cells(j, 8).Style = "input"
            ElseIf IsNumeric(Right(Cells(j, 9), 2)) = True And Left(Cells(j, 9), 2) <> "B-" Then
                Cells(j, 9) = Trim(Right(Cells(j, 9), 6) & " " & Left(Cells(j, 9), Len(Cells(j, 9)) - 6))
                Cells(j, 9).Style = "Input"
            End If
        End If

    ElseIf Cells(j, 10) = "CH" Then                                             ' Swiss formatting
        If Len(Cells(j, 9)) = 4 Then
            Cells(j, 9) = "CH-" & Cells(j, 9)
            Selection.Style = "Input"
'            ElseIf Len(Cells(j, 9)) = 7 And Left(Cells(j, 9), 2) = "CH" Then       Fix to include moving the post codes before the city name
'                Selection.Style = "Neutral"
        End If

    ElseIf Cells(j, 10) = "DE" Then                                             ' German formatting
        If Left(Cells(j, 9), 1) <> "D" Then
            Cells(j, 9) = "D-" & Cells(j, 9)
            Selection.Style = "Neutral"
        End If
        If IsNumeric(Right(Cells(j, 9), 5)) = True And Len(Cells(j, 9)) = 7 Then
            If Cells(j, 8) <> "" Then
                Cells(j, 8) = Cells(j, 9) & " " & Cells(j, 8)
                Cells(j, 9).ClearContents
                Cells(j, 8).Style = "Input"
            ElseIf Cells(j, 8) = "" Then
                Cells(j, 7) = Cells(j, 9) & " " & Cells(j, 7)
                Cells(j, 7).Style = "Input"
                Cells(j, 9).ClearContents
            End If
        ElseIf IsNumeric(Right(Cells(j, 9), 5)) And Len(Cells(j, 9)) > 8 Then
            If Mid(Cells(j, 9), Len(Cells(j, 9) - 7), 1) = "D" Then
                Cells(j, 9) = Trim(Right(Cells(j, 9), 7) & " " & Left(Cells(j, 9), Len(Cells(j, 9)) - 7))
                Cells(j, 9).Style = "Input"
            End If
        Else
            Cells(j, 9).Style = "Bad"
        End If

    ElseIf Cells(j, 10) = "IE" Then                                         ' Irish formatting
        If Cells(j, 9) <> "" Then
            If IsNumeric(Right(Cells(j, 9), 1)) = True And (Len(Cells(j, 9)) = 9 Or Len(Cells(j, 9)) = 8) Then
            ElseIf (Len(Cells(j, 9)) <> 8 Or Len(Cells(j, 9)) <> 9) And Left(Cells(j, 9), 6) = "DUBLIN" Then
                If Mid(Cells(j, 9), Len(Cells(j, 9)) - 1, 1) = " " Then
                    Cells(j, 9) = Left(Cells(j, 9), 6) & " " & Right(Cells(j, 9), 1)
                    Cells(j, 9).Style = "Input"
                ElseIf Mid(Cells(j, 9), Len(Cells(j, 9)) - 2, 1) = " " Then
                    Cells(j, 9) = Left(Cells(j, 9), 6) & " " & Right(Cells(j, 9), 2)
                    Cells(j, 9).Style = "Input"
                End If
            ElseIf Left(Cells(j, 9), 1) = "A" Or Left(Cells(j, 9), 1) = "B" Or Left(Cells(j, 9), 1) = "E" Or Left(Cells(j, 9), 1) = "F" _
                Or Left(Cells(j, 9), 1) = "H" Or Left(Cells(j, 9), 1) = "I" Or Left(Cells(j, 9), 1) = "J" Or Left(Cells(j, 9), 1) = "N" _
                Or Left(Cells(j, 9), 1) = "O" Or Left(Cells(j, 9), 1) = "P" Or Left(Cells(j, 9), 1) = "Q" Or Left(Cells(j, 9), 1) = "B" _
                Or Left(Cells(j, 9), 1) = "U" Or Left(Cells(j, 9), 1) = "V" Or Left(Cells(j, 9), 1) = "W" Or Left(Cells(j, 9), 1) = "X" _
                Or Left(Cells(j, 9), 1) = "Y" Or Left(Cells(j, 9), 1) = "Z" Then
                Cells(j, 9).Style = "Bad"
            ElseIf Len(Cells(j, 9)) > 16 Then
                Cells(j, 9).Style = "Bad"
            ElseIf (Len(Cells(j, 9)) <> 8 Or Len(Cells(j, 9)) <> 9) And Left(Cells(j, 9), 6) <> "DUBLIN" Then
            End If
        End If
        If Cells(j, 8) = "DUBLIN" And Left(Cells(j, 9), 6) = "DUBLIN" Then
            Cells(j, 8).ClearContents
        ElseIf Cells(j, 7) = "DUBLIN" And Left(Cells(j, 9), 6) = "DUBLIN" Then
            Cells(j, 7).ClearContents
        ElseIf Cells(j, 8) = "CORK" And Left(Cells(j, 9), 4) = "CORK" Then
            Cells(j, 8).ClearContents
        End If

        ElseIf Cells(j, 10) = "US" Then                                         ' US formatting
        b = Len(Cells(j, 9))
        If Cells(j, 10) = "US" And b < 5 And b > 1 Then
            Cells(j, 9).Select
            Selection.NumberFormat = "@"
            If b = 4 Then
                Cells(j, 9) = "0" & Cells(j, 9)
            Else
                Cells(j, 9) = "00" & Cells(j, 9)
            End If
        ElseIf Cells(j, 10) = "US" And Mid(Cells(j, 9), 3, 1) = " " And Len(Cells(j, 9)) = 7 Then
            Cells(j, 9) = Left(Cells(j, 9), 3) & "0" & Right(Cells(j, 9), 4)
        End If
        If Len(Cells(j, 8)) = 2 And Cells(j, 9) <> "" And Len(Cells(j, 9)) >= 5 Then
            Cells(j, 8) = Cells(j, 8) & " " & Cells(j, 9)
            Cells(j, 9).ClearContents
            Range(Cells(j, 8), Cells(j, 9)).Style = "Neutral"
        ElseIf Len(Cells(j, 9)) = 8 Or Len(Cells(j, 9)) = 12 Then
            e = Application.Match(Left(Cells(j, 9), 2), Worksheets("Codes").Range("G:G"), 0)
            If Selection.Style <> "Neutral" And Not IsError(e) Then
                Selection.Style = "Input"
            End If
        ElseIf (Len(Cells(j, 9)) >= 5) And (Len(Cells(j, 8)) - 2) >= 0 Or (Len(Cells(j, 7)) - 2) >= 0 Then
            If Mid(Cells(j, 7), Len(Cells(j, 7)) - 2, 1) = " " And Cells(j, 8) = "" Then
                Cells(j, 9) = Right(Cells(j, 7), 2) & " " & Cells(j, 9)
                Cells(j, 7) = Trim(Left(Cells(j, 7), Len(Cells(j, 7)) - 2))
                Range(Cells(j, 7), Cells(j, 9)).Style = "Neutral"
            ElseIf Mid(Cells(j, 8), Len(Cells(j, 8)) - 2, 1) = " " And Len(Cells(j, 8)) - 2 >= 0 And IsNumeric(Right(Cells(j, 8), 1)) = False Then
                e = Application.Match(Right(Cells(j, 8), 2), Worksheets("Codes").Range("G:G"), 0)
                If Not IsError(e) Then
                    Cells(j, 9) = Right(Cells(j, 8), 2) & " " & Cells(j, 9)
                    Cells(j, 8) = Trim(Left(Cells(j, 8), Len(Cells(j, 8)) - 2))
                    Cells(j, 9).Style = "Input"
                    Cells(j, 8).Style = "Neutral"
                End If
            ElseIf Mid(Cells(j, 8), 3, 1) = " " And Len(Cells(j, 8)) - 2 >= 0 And IsNumeric(Right(Cells(j, 8), 3)) = True Then
                e = Application.Match(Left(Cells(j, 8), 2), Worksheets("Codes").Range("G:G"), 0)
                If Not IsError(e) Then
                    If Cells(j, 8).Style <> "Neutral" Or Cells(j, 8).Style <> "Input" Then
                        Cells(j, 8).Style = "Input"
                        Cells(j, 9).Style = "input"
                    End If
                End If
            ElseIf Selection.Style <> "Neutral" Then
                Range(Cells(j, 8), Cells(j, 9)).Style = "Bad"
            End If
        Else
            If Selection.Style <> "Neutral" Then
                Selection.Style = "Bad"
            End If
        End If

    End If
Dim rw As Range, c8, c9, c10
Set rw = Rows(j)

'use better variable names here...
Set c8 = rw.Cells(8)
Set c9 = rw.Cells(9)
Set c10 = rw.Cells(10)

If c10 = "AT" Then

ElseIf c10 = "BE" Then

    If c9 <> "" Then

        If Left(c9, 2) = "B-" And IsNumeric(Right(c9, 1)) And Len(c9) = 6 Then
            c8 = c9 & " " & c8
            c9.ClearContents
            c8.Style = "input"
        ElseIf IsNumeric(Left(c9, 2)) And IsNumeric(Right(c9, 1)) = True Then
            c9 = "B-" & c9
            c8 = c9 & " " & c8
            c9.ClearContents
            c8.Style = "input"
        ElseIf IsNumeric(Right(c9, 2)) And Left(c9, 2) <> "B-" Then
            c9 = Trim(Right(c9, 6) & " " & Left(c9, Len(c9) - 6))
            c9.Style = "Input"
        End If

    End If