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

寻找一些关于编写macros的最佳实践的build议。 我现在正在写一个完整的,将有大约200 IF / ELSE条款。 这些条款中的每一条都将有大约10个IF / ELSE子条款。

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

但是,从最佳实践的angular度来看,最好是:

  • 每个子句调用另一个macros,它将包含子条款?
  • 把主要macros的总体内的子条款留下来?

另外,这些选项中的每一个如何影响macros的速度(如选项1比选项2更慢或更快)? 它正在处理的电子表格将有大约5万行,大约13列。

编辑1:

这是代码开始的一个示例(很长很乱):

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 

编辑2:

数据格式为ADD1(单元格(j,5),ADD2(.6),ADD3(… 7),ADD4(..8),ADD5(… 9)格式化)和ADD6(..10,ISO国家代码)。

代码的重点是检查每一行,如果邮政编码是正确的国家格式(如美国,爱尔兰,奥地利等),那么它什么都不做。 但是,如果有部件丢失或错误的顺序,它会去修复故障(添加详细信息,删除重复的数据,移动数据或将数据连接到一个字段),或突出显示数据为有故障。

由于每个国家都有不同的规定,一些国家有一个以上的规则,所以会变得非常混乱。

在代码中摆脱许多重复的因素对于可读性会有很大的帮助。 例如,使用一些范围variables来删除所有重复的Cells(j, xx) – 这将清理您的代码很多,维护/重构整洁的代码更容易。

 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