简化if语句

时间:2018-01-10 08:41:25

标签: excel-vba excel-vba-mac vba excel

我有几个if语句,如下所示。假设这是一种非常糟糕/长期的编码方式,但不确定我应该采取哪些不同的方式。有人有任何建议吗?

谢谢

For a = 1 To Leagues
     If a = 1 Then
    SheetName = "E0"
    ElseIf a = 2 Then
        SheetName = "E1"
     ElseIf a = 3 Then
        SheetName = "E2"
      ElseIf a = 4 Then
        SheetName = "E3"
       ElseIf a = 5 Then
        SheetName = "EC"
        ElseIf a = 6 Then
            SheetName = "SC0"
         ElseIf a = 7 Then
            SheetName = "SC1"
          ElseIf a = 8 Then
            SheetName = "SC2"
           ElseIf a = 9 Then
            SheetName = "SC3"
            ElseIf a = 10 Then
                SheetName = "D1"
             ElseIf a = 11 Then
                SheetName = "D2"
              ElseIf a = 12 Then
                SheetName = "SP1"
               ElseIf a = 13 Then
                SheetName = "SP2"
                ElseIf a = 14 Then
                    SheetName = "I1"
                 ElseIf a = 15 Then
                    SheetName = "I2"
                  ElseIf a = 16 Then
                    SheetName = "F1"
                   ElseIf a = 17 Then
                    SheetName = "F2"
                    ElseIf a = 18 Then
                        SheetName = "N1"
                     ElseIf a = 19 Then
                        SheetName = "B1"
                      ElseIf a = 20 Then
                        SheetName = "P1"
                       ElseIf a = 21 Then
                        SheetName = "T1"
                        ElseIf a = 22 Then
                        SheetName = "G1"
    End If

2 个答案:

答案 0 :(得分:3)

当您将多个选项与单个变体(在您的情况下为a)进行比较时,使用Select Case而不是If...Then来提高可读性更加简单。

看看:

Sub test()

    Select Case a
        Case 1
            SheetName = "E0"
        Case 2
            SheetName = "E1"
        Case 3
            SheetName = "E2"
        Case 4
            SheetName = "E3"
        Case 5
            SheetName = "EC"
        Case 6
            SheetName = "SC0"
        Case 7
            SheetName = "SC1"
        Case 8
            SheetName = "SC2"
        Case 9
            SheetName = "SC3"
        Case 10
            SheetName = "D1"
        Case 11
            SheetName = "D2"
        Case 12
            SheetName = "SP1"
        Case 13
            SheetName = "SP2"
        Case 14
            SheetName = "I1"
        Case 15
            SheetName = "I2"
        Case 16
            SheetName = "F1"
        Case 17
            SheetName = "F2"
        Case 18
            SheetName = "N1"
        Case 19
            SheetName = "B1"
        Case 20
            SheetName = "P1"
        Case 21
            SheetName = "T1"
        Case 22
            SheetName = "G1"
    End Select

End Sub

我应该补充一点,Select Case也可以在此语法中使用(不会在您的实例中使用,但将来要记住的事项)

Select Case a
    Case 1 To 5
        sheetname = "E0"
    Case 6, 8 To 9
        sheetname = "E1"
    Case 7
        sheetname = "E2"
    Case Else
        sheetname = "Blah"
End Select

答案 1 :(得分:2)

使用数据字段数组不需要IfCase

您可以将工作表中列出的所有工作表名称存储到基于1的2维数据字段数组中。这允许您通过一个代码行获取第n个工作表名称:SheetName = v(n, 1)(或示例中的SheetName = v(a, 1))。

Sub test()
Dim a         As Integer
Dim sheetname As String
Dim v         ' variant, receives 2-dim data field contents
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("MySheetNameList") ' << change to sheet where you store your sheet names
' create one based 2-dim datafield array from your sheetname list
' (assuming start in cell A2, omitting title row)
  v = ws.Range("A2:A" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
' ... example value for a
  a = 22
' assign sheet name avoiding any IF, CASE or SWITCH
  sheetname = v(a, 1)       ' get e.g. 22nd item (
  Debug.Print a, sheetname
End Sub