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

时间:2019-01-17 16:25:50

标签: excel vba simplify

所以对vba来说是菜鸟,这是我使用任何一种编码的第8天,因此我为在此代码中使用的“蛮力”方法感到抱歉,我知道它的效率不高,并且必须更好的方法。这就是我要问的,我该如何简化这段代码?它运行良好,可以完成所需的工作,但体积较慢。

'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

3 个答案:

答案 0 :(得分:3)

工作表变量将在此处使用。...声明后,将Sheets("Monthly Status")的每个实例替换为变量名(ws


Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Monthly Status")    

If Not RefDate1 Then
    ws.Range("K24:K34").ClearContents
Else
    ws.Range("K24:K25") = RefDate1 - (7 * 26)
    ws.Range("K26") = RefDate1 - (7 * 23)
    'and so on......
End If

答案 1 :(得分:0)

或者

    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

答案 2 :(得分:-1)

简化笨重

非连续版本

node {
   // setup env..
   // copy the deployment unit from another Job...
   step ([$class: 'CopyArtifact',
          projectName: 'webapp_build',
          filter: 'target/orders.war']);
   // deploy 'target/orders.war' to an app host
}

连续(K24:K34)快速版本

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