Excel 如何简化这个庞大的VBA If/Then语句

Excel 如何简化这个庞大的VBA If/Then语句,excel,vba,simplify,Excel,Vba,Simplify,所以我是vba的新手,这是我第八天使用任何类型的代码,所以我为我在代码中使用的“暴力”方法感到抱歉,我知道它没有效率,必须有更好的方法来做到这一点。这正是我要问的,我如何简化代码?它运行良好,并做它需要做的,但它的庞大和缓慢 'RefDate1=cell K36 Dim RefDate1 As Date RefDate1 = Sheets("Monthly Status").Range("K36") If RefDate1 = False Then Sheets("Monthly Statu

所以我是vba的新手,这是我第八天使用任何类型的代码,所以我为我在代码中使用的“暴力”方法感到抱歉,我知道它没有效率,必须有更好的方法来做到这一点。这正是我要问的,我如何简化代码?它运行良好,并做它需要做的,但它的庞大和缓慢

'RefDate1=cell K36

Dim RefDate1 As Date

RefDate1 = Sheets("Monthly Status").Range("K36")
If RefDate1 = False Then
Sheets("Monthly Status").Range("K34").ClearContents
Sheets("Monthly Status").Range("K33").ClearContents
Sheets("Monthly Status").Range("K32").ClearContents
Sheets("Monthly Status").Range("K31").ClearContents
Sheets("Monthly Status").Range("K30").ClearContents
Sheets("Monthly Status").Range("K29").ClearContents
Sheets("Monthly Status").Range("K28").ClearContents
Sheets("Monthly Status").Range("K27").ClearContents
Sheets("Monthly Status").Range("K26").ClearContents
Sheets("Monthly Status").Range("K25").ClearContents
Sheets("Monthly Status").Range("K24").ClearContents
Else
Sheets("Monthly Status").Range("K34").Value = (RefDate1 - (7 * 6))
Sheets("Monthly Status").Range("K33").Value = (RefDate1 - (7 * 8))
Sheets("Monthly Status").Range("K32").Value = (RefDate1 - (7 * 9))
Sheets("Monthly Status").Range("K31").Value = (RefDate1 - (7 * 11))
Sheets("Monthly Status").Range("K30").Value = (RefDate1 - (7 * 12))
Sheets("Monthly Status").Range("K29").Value = (RefDate1 - (7 * 19))
Sheets("Monthly Status").Range("K28").Value = (RefDate1 - (7 * 20))
Sheets("Monthly Status").Range("K27").Value = (RefDate1 - (7 * 22))
Sheets("Monthly Status").Range("K26").Value = (RefDate1 - (7 * 23))
Sheets("Monthly Status").Range("K25").Value = (RefDate1 - (7 * 26))
Sheets("Monthly Status").Range("K24").Value = (RefDate1 - (7 * 26))
End If

一个工作表变量将在此处执行。。。。声明后,用变量名(
ws
)交换
工作表(“每月状态”)
的每个实例


简化笨重的 非连续版本 连续(K24:K34)快速版本 或者

    Dim RefDate1 As Date
    With  Sheets("Monthly Status")  'use with to save retyping
        RefDate1 =.Range("K36")
        If RefDate1 = False Then
            .Range("K24:K34").ClearContents

        Else
          Dim v
          v = Array(26, 26, 23, 22, 20, 20, 19, 12, 11, 9, 8, 6)   'set up an array

          Dim x  'and a counter
          With Range("K24")  'start at the top
               For x = 0 To 11  'going down 11 cells
              .Offset(x, 0).Value = (RefDate1 - (7 * v(x)))  'an offset of x rows,zero columns
              Next x
          End With
    End If
    end with

你能解释一下怎么做吗?我知道什么是循环,只是不知道如何在这种情况下使用它。抱歉,我对这张
表格(“每月状态”).范围(“K24:K34”).ClearContents
非常陌生。只需一行即可清除该范围。
Sub Bulky()

    ' Sheet Name, Cells List, Date Cell, Weeks List, Days in Week
    Const cSheet As String = "Monthly Status"
    Const cCells As String = "K34,K33,K32,K31,K30,K29,K28,K27,K26,K25,K24"
    Const cDateCell As String = "K36"
    Const cWeeks As String = "6,8,9,11,12,19,20,22,23,26,26"
    Const cDays As Long = 7

    Dim vntC As Variant   ' Cells Array
    Dim vntW As Variant   ' Weeks Array
    Dim RefDate1 As Date  ' Date
    Dim i As Long         ' Arrays Row Counter

    With Sheets(cSheet)
        RefDate1 = .Range(cDateCell).Value
        If RefDate1 = False Then
            .Range(cCells).ClearContents
          Else
            vntC = Split(cCells, ",")
            vntW = Split(cWeeks, ",")
            For i = 0 To UBound(vntC)
                .Range(vntC(i)).Value = RefDate1 - (cDays * CLng(Trim(vntW(i))))
            Next
        End If
    End With

End Sub
Sub Bulky2()

    ' Sheet Name, Source Range, Date Cell, Weeks List, Days in Week
    Const cSheet As String = "Monthly Status"
    Const cCells As String = "K24:K34"
    Const cDateCell As String = "K36"
    Const cWeeks As String = "26,26,23,22,20,19,12,11,9,8,6"
    Const cDays As Long = 7

    Dim vntT As Variant   ' Target Array
    Dim vntW As Variant   ' Weeks Array
    Dim RefDate1 As Date  ' Date
    Dim i As Long         ' Arrays Row Counter

    With Sheets(cSheet)
        RefDate1 = .Range(cDateCell).Value
        If RefDate1 = False Then
            .Range(cCells).ClearContents
          Else
            vntW = Split(cWeeks, ",")
            ReDim vntT(1 To UBound(vntW) + 1, 1 To 1)
            For i = 1 To UBound(vntT)
                vntT(i, 1) = RefDate1 - (cDays * CLng(Trim(vntW(i - 1))))
            Next
            .Range(cCells) = vntT
        End If
    End With

End Sub
    Dim RefDate1 As Date
    With  Sheets("Monthly Status")  'use with to save retyping
        RefDate1 =.Range("K36")
        If RefDate1 = False Then
            .Range("K24:K34").ClearContents

        Else
          Dim v
          v = Array(26, 26, 23, 22, 20, 20, 19, 12, 11, 9, 8, 6)   'set up an array

          Dim x  'and a counter
          With Range("K24")  'start at the top
               For x = 0 To 11  'going down 11 cells
              .Offset(x, 0).Value = (RefDate1 - (7 * v(x)))  'an offset of x rows,zero columns
              Next x
          End With
    End If
    end with