在VBA

时间:2016-03-15 15:34:44

标签: excel vba excel-vba if-statement

有没有人知道是否有更快的方法对此进行编码?我正在处理Excel工作表,如果单击复选框,则单元格中会出现True。然后,如果单元格为真,那么......然而,有11种可能的选择意味着有数百万种可能的组合,并且编写每行代码并仔细检查它就是杀了我。下面的代码示例(只有10种可能的组合。

   If Worksheets("Overview").Range("B36").Value = "" And Worksheets("Overview").Range("C36").Value = "" And Worksheets("Overview").Range("D36").Value = "" And Worksheets("Overview").Range("E36").Value = "" And Worksheets("Overview").Range("F36").Value = "" And Worksheets("Overview").Range("G36").Value = "" And Worksheets("Overview").Range("H36").Value = "" And Worksheets("Overview").Range("I36").Value = "" And Worksheets("Overview").Range("J36").Value = "" And Worksheets("Overview").Range("K36").Value = "" And Worksheets("Overview").Range("L36").Value = "" Then
MsgBox "Please select engagement components."

    ElseIf Worksheets("Overview").Range("B36").Value = "True" And Worksheets("Overview").Range("C36").Value = "" And Worksheets("Overview").Range("D36").Value = "" And Worksheets("Overview").Range("E36").Value = "" And Worksheets("Overview").Range("F36").Value = "" And Worksheets("Overview").Range("G36").Value = "" And Worksheets("Overview").Range("H36").Value = "" And Worksheets("Overview").Range("I36").Value = "" And Worksheets("Overview").Range("J36").Value = "" And Worksheets("Overview").Range("K36").Value = "" And Worksheets("Overview").Range("L36").Value = "" Then
MsgBox "Cost: " & Worksheets("Billing Rates").Range("C33").Value & vbNewLine & "Hours: " & Worksheets("Billing Rates").Range("C25").Value & vbNewLine & "Scope: "

ElseIf Worksheets("Overview").Range("B36").Value = "True" And Worksheets("Overview").Range("C36").Value = "True" And Worksheets("Overview").Range("D36").Value = "" And Worksheets("Overview").Range("E36").Value = "" And Worksheets("Overview").Range("F36").Value = "" And Worksheets("Overview").Range("G36").Value = "" And Worksheets("Overview").Range("H36").Value = "" And Worksheets("Overview").Range("I36").Value = "" And Worksheets("Overview").Range("J36").Value = "" And Worksheets("Overview").Range("K36").Value = "" And Worksheets("Overview").Range("L36").Value = "" Then
MsgBox "Cost: " & Worksheets("Billing Rates").Range("C33").Value + Worksheets("Billing Rates").Range("D33").Value & vbNewLine & "Hours: " & Worksheets("Billing Rates").Range("C25").Value + Worksheets("Billing Rates").Range("D25").Value & vbNewLine & "Scope: "

    ElseIf Worksheets("Overview").Range("B36").Value = "True" And Worksheets("Overview").Range("C36").Value = "True" And Worksheets("Overview").Range("D36").Value = "True" And Worksheets("Overview").Range("E36").Value = "" And Worksheets("Overview").Range("F36").Value = "" And Worksheets("Overview").Range("G36").Value = "" And Worksheets("Overview").Range("H36").Value = "" And Worksheets("Overview").Range("I36").Value = "" And Worksheets("Overview").Range("J36").Value = "" And Worksheets("Overview").Range("K36").Value = "" And Worksheets("Overview").Range("L36").Value = "" Then
MsgBox "Cost: " & Worksheets("Billing Rates").Range("C33").Value + Worksheets("Billing Rates").Range("D33").Value + Worksheets("Billing Rates").Range("E33").Value & vbNewLine & "Hours: " & Worksheets("Billing Rates").Range("C25").Value + Worksheets("Billing Rates").Range("D25").Value + Worksheets("Billing Rates").Range("E25").Value & vbNewLine & "Scope: "

     ElseIf Worksheets("Overview").Range("B36").Value = "True" And Worksheets("Overview").Range("C36").Value = "True" And Worksheets("Overview").Range("D36").Value = "True" And Worksheets("Overview").Range("E36").Value = "True" And Worksheets("Overview").Range("F36").Value = "" And Worksheets("Overview").Range("G36").Value = "" And Worksheets("Overview").Range("H36").Value = "" And Worksheets("Overview").Range("I36").Value = "" And Worksheets("Overview").Range("J36").Value = "" And Worksheets("Overview").Range("K36").Value = "" And Worksheets("Overview").Range("L36").Value = "" Then
MsgBox "Cost: " & Worksheets("Billing Rates").Range("C33").Value + Worksheets("Billing Rates").Range("D33").Value + Worksheets("Billing Rates").Range("E33").Value + Worksheets("Billing Rates").Range("F33") & vbNewLine & "Hours: " & Worksheets("Billing Rates").Range("C25").Value + Worksheets("Billing Rates").Range("D25").Value + Worksheets("Billing Rates").Range("E25").Value + Worksheets("Billing Rates").Range("F25").Value & vbNewLine & "Scope: "

    ElseIf Worksheets("Overview").Range("B36").Value = "True" And Worksheets("Overview").Range("C36").Value = "True" And Worksheets("Overview").Range("D36").Value = "True" And Worksheets("Overview").Range("E36").Value = "True" And Worksheets("Overview").Range("F36").Value = "True" And Worksheets("Overview").Range("G36").Value = "" And Worksheets("Overview").Range("H36").Value = "" And Worksheets("Overview").Range("I36").Value = "" And Worksheets("Overview").Range("J36").Value = "" And Worksheets("Overview").Range("K36").Value = "" And Worksheets("Overview").Range("L36").Value = "" Then
MsgBox "Cost: " & Worksheets("Billing Rates").Range("C33").Value + Worksheets("Billing Rates").Range("D33").Value + Worksheets("Billing Rates").Range("E33").Value + Worksheets("Billing Rates").Range("F33") + Worksheets("Billing Rates").Range("G33") & vbNewLine & "Hours: " & Worksheets("Billing Rates").Range("C25").Value + Worksheets("Billing Rates").Range("D25").Value + Worksheets("Billing Rates").Range("E25").Value + Worksheets("Billing Rates").Range("F25").Value + Worksheets("Billing Rates").Range("G25") & vbNewLine & "Scope: "

    ElseIf Worksheets("Overview").Range("B36").Value = "True" And Worksheets("Overview").Range("C36").Value = "True" And Worksheets("Overview").Range("D36").Value = "True" And Worksheets("Overview").Range("E36").Value = "True" And Worksheets("Overview").Range("F36").Value = "True" And Worksheets("Overview").Range("G36").Value = "True" And Worksheets("Overview").Range("H36").Value = "" And Worksheets("Overview").Range("I36").Value = "" And Worksheets("Overview").Range("J36").Value = "" And Worksheets("Overview").Range("K36").Value = "" And Worksheets("Overview").Range("L36").Value = "" Then
MsgBox "Cost: " & Worksheets("Billing Rates").Range("C33").Value + Worksheets("Billing Rates").Range("D33").Value + Worksheets("Billing Rates").Range("E33").Value + Worksheets("Billing Rates").Range("F33") + Worksheets("Billing Rates").Range("G33") + Worksheets("Billing Rates").Range("H33") & vbNewLine & "Hours: " & Worksheets("Billing Rates").Range("C25").Value + Worksheets("Billing Rates").Range("D25").Value + Worksheets("Billing Rates").Range("E25").Value + Worksheets("Billing Rates").Range("F25").Value + Worksheets("Billing Rates").Range("G25").Value + Worksheets("Billing Rates").Range("H25") & vbNewLine & "Scope: "

    ElseIf Worksheets("Overview").Range("B36").Value = "True" And Worksheets("Overview").Range("C36").Value = "True" And Worksheets("Overview").Range("D36").Value = "True" And Worksheets("Overview").Range("E36").Value = "True" And Worksheets("Overview").Range("F36").Value = "True" And Worksheets("Overview").Range("G36").Value = "True" And Worksheets("Overview").Range("H36").Value = "True" And Worksheets("Overview").Range("I36").Value = "" And Worksheets("Overview").Range("J36").Value = "" And Worksheets("Overview").Range("K36").Value = "" And Worksheets("Overview").Range("L36").Value = "" Then
MsgBox "Cost: " & Worksheets("Billing Rates").Range("C33").Value + Worksheets("Billing Rates").Range("D33").Value + Worksheets("Billing Rates").Range("E33").Value + Worksheets("Billing Rates").Range("F33") + Worksheets("Billing Rates").Range("G33").Value + Worksheets("Billing Rates").Range("H33").Value + Worksheets("Billing Rates").Range("I33").Value & vbNewLine & "Hours: " & Worksheets("Billing Rates").Range("C25").Value + Worksheets("Billing Rates").Range("D25").Value + Worksheets("Billing Rates").Range("E25").Value + Worksheets("Billing Rates").Range("F25").Value + Worksheets("Billing Rates").Range("G25") + Worksheets("Billing Rates").Range("H25") + Worksheets("Billing Rates").Range("I25").Value & vbNewLine & "Scope: "

    ElseIf Worksheets("Overview").Range("B36").Value = "True" And Worksheets("Overview").Range("C36").Value = "True" And Worksheets("Overview").Range("D36").Value = "True" And Worksheets("Overview").Range("E36").Value = "True" And Worksheets("Overview").Range("F36").Value = "True" And Worksheets("Overview").Range("G36").Value = "True" And Worksheets("Overview").Range("H36").Value = "True" And Worksheets("Overview").Range("I36").Value = "True" And Worksheets("Overview").Range("J36").Value = "" And Worksheets("Overview").Range("K36").Value = "" And Worksheets("Overview").Range("L36").Value = "" Then
MsgBox "Cost: " & Worksheets("Billing Rates").Range("C33").Value + Worksheets("Billing Rates").Range("D33").Value + Worksheets("Billing Rates").Range("E33").Value + Worksheets("Billing Rates").Range("F33") + Worksheets("Billing Rates").Range("G33").Value + Worksheets("Billing Rates").Range("H33").Value + Worksheets("Billing Rates").Range("I33").Value + Worksheets("Billing Rates").Range("J33").Value & vbNewLine & "Hours: " & Worksheets("Billing Rates").Range("C25").Value + Worksheets("Billing Rates").Range("D25").Value + Worksheets("Billing Rates").Range("E25").Value + Worksheets("Billing Rates").Range("F25").Value + Worksheets("Billing Rates").Range("G25") + Worksheets("Billing Rates").Range("H25") + Worksheets("Billing Rates").Range("I25").Value & vbNewLine & "Scope: "

    ElseIf Worksheets("Overview").Range("B36").Value = "True" And Worksheets("Overview").Range("C36").Value = "True" And Worksheets("Overview").Range("D36").Value = "True" And Worksheets("Overview").Range("E36").Value = "True" And Worksheets("Overview").Range("F36").Value = "True" And Worksheets("Overview").Range("G36").Value = "True" And Worksheets("Overview").Range("H36").Value = "True" And Worksheets("Overview").Range("I36").Value = "True" And Worksheets("Overview").Range("J36").Value = "True" And Worksheets("Overview").Range("K36").Value = "" And Worksheets("Overview").Range("L36").Value = "" Then
MsgBox "Cost: " & Worksheets("Billing Rates").Range("C33").Value + Worksheets("Billing Rates").Range("D33").Value + Worksheets("Billing Rates").Range("E33").Value + Worksheets("Billing Rates").Range("F33") + Worksheets("Billing Rates").Range("G33").Value + Worksheets("Billing Rates").Range("H33").Value + Worksheets("Billing Rates").Range("I33").Value + Worksheets("Billing Rates").Range("J33").Value + Worksheets("Billing Rates").Range("K33").Value & vbNewLine & "Hours: " & Worksheets("Billing Rates").Range("C25").Value + Worksheets("Billing Rates").Range("D25").Value + Worksheets("Billing Rates").Range("E25").Value + Worksheets("Billing Rates").Range("F25").Value + Worksheets("Billing Rates").Range("G25") + Worksheets("Billing Rates").Range("H25") + Worksheets("Billing Rates").Range("I25").Value + Worksheets("Billing Rates").Range("J25").Value & vbNewLine & "Scope: "

    ElseIf Worksheets("Overview").Range("B36").Value = "True" And Worksheets("Overview").Range("C36").Value = "True" And Worksheets("Overview").Range("D36").Value = "True" And Worksheets("Overview").Range("E36").Value = "True" And Worksheets("Overview").Range("F36").Value = "True" And Worksheets("Overview").Range("G36").Value = "True" And Worksheets("Overview").Range("H36").Value = "True" And Worksheets("Overview").Range("I36").Value = "True" And Worksheets("Overview").Range("J36").Value = "True" And Worksheets("Overview").Range("K36").Value = "True" And Worksheets("Overview").Range("L36").Value = "" Then
MsgBox "Cost: " & Worksheets("Billing Rates").Range("C33").Value + Worksheets("Billing Rates").Range("D33").Value + Worksheets("Billing Rates").Range("E33").Value + Worksheets("Billing Rates").Range("F33") + Worksheets("Billing Rates").Range("G33").Value + Worksheets("Billing Rates").Range("H33").Value + Worksheets("Billing Rates").Range("I33").Value + Worksheets("Billing Rates").Range("J33").Value + Worksheets("Billing Rates").Range("K33").Value + Worksheets("Billing Rates").Range("L33").Value & vbNewLine & "Hours: " & Worksheets("Billing Rates").Range("C25").Value + Worksheets("Billing Rates").Range("D25").Value + Worksheets("Billing Rates").Range("E25").Value + Worksheets("Billing Rates").Range("F25").Value + Worksheets("Billing Rates").Range("G25") + Worksheets("Billing Rates").Range("H25") + Worksheets("Billing Rates").Range("I25").Value + Worksheets("Billing Rates").Range("J25").Value + Worksheets("Billing Rates").Range("K25").Value + Worksheets("Billing Rates").Range("L25").Value

4 个答案:

答案 0 :(得分:3)

awnser在其他ifs或顺序ifs中执行ifs,存储值,并在最后一行显示消息:

Dim msg as String
If A = True Then
    If B = True Then
        msg = "A and B is True"
    Else
        msg = "A is True B is False"
    End If
End If
MsgBox msg

或者

If A = True Then
    msg = "A is true"
Else
    msg = "A is false"
EndIf
If B = True Then
    msg = msg & " and B is True"
Else
    msg = msg & " and B is False"
End If
MsgBox msg

您无需反复检查相同的条件。

答案 1 :(得分:1)

从结算率中检索到的成本和小时数值似乎与概览工作表中正在检查的单元格相比有一列,虽然位于不同的行上。

call.execute().body

我无法调和你打算用范围:标签做的事情。

答案 2 :(得分:1)

我可能会这样做:

Dim ws As Worksheet, wsB As Worksheet
Set ws = Worksheets("Overview")
Set wsB = Worksheets("BillingRates")

Dim trueCount As Integer
Dim i As Integer
Dim Cst, Hrs

For i = 1 To 11

    If ws.Range(Chr(65 + i) & "36").Value = "True" Then
        trueCount = trueCount + 1
        Cst = Cst + wsB.Range(Chr(66 + i) & "33").Value
        Hrs = Hrs + wsB.Range(Chr(66 + i) & "25").Value
    End If

    If trueCount = 0 Then
        MsgBox "Please select engagement components."
    Else
        MsgBox "Cost: " & Cst _
        & vbNewLine & "Hours: " & Hrs _
        & vbNewLine & "Scope: "
    End If

Next i

答案 3 :(得分:0)

感谢RBarryYoung和Jeeped帮助解决了这个问题,如果没有你的帮助,我真的会在一条小溪上。我想出了如何让弹出窗口重复出现。以下是我最终使用的最终代码:

Sub Way_EatFresh()

Dim ws As Worksheet, wsB As Worksheet
Set ws = Worksheets("Overview")
Set wsB = Worksheets("Billing Rates")

Dim trueCount As Integer
Dim i As Integer
Dim Cst, Hrs

For i = 1 To 11

If ws.Range(Chr(65 + i) & "36").Value = "True" Then
    trueCount = trueCount + 1
    Cst = Cst + wsB.Range(Chr(66 + i) & "33").Value
    Hrs = Hrs + wsB.Range(Chr(66 + i) & "25").Value
End If

Next i

If trueCount > 0 Then
   MsgBox "Cost: " & Cst _
    & vbNewLine & "Hours: " & Hrs _
    & vbNewLine & ""
End If

If trueCount = 0 Then
     MsgBox "Please select engagement components."
End If
End Sub