使用子

时间:2017-02-01 09:23:27

标签: excel excel-vba vba

我最初的问题是如何在if - > then语句中使用函数的输出,而Shai的帮助非常有帮助(这里:Using an output of aformula in another)。

但我现在要做的是在子中使用此功能。所以我有这个子(现在还不完整):

Private Sub CommandButto1_click()
Dim answer As Integer
Dim Response As VbMsgBoxResult
Dim late As VbMsgBoxResult

answer = MsgBox("Price for only one product?", vbYesNoCancel + vbQuestion, "Payment")
If answer = vbYes then
   late = MsgBox("Is the customer late and has to be charged extra?", vbQuestion + vbYesNoCancel)

   If late = vbYes then 
       MsgBox "mergesize function here"
   End If
End If

End Sub

它可以正常工作,但它说的是 - MsgBox"在这里合并功能"是我想添加我的功能的地方:

Public Function MergeSize(r As Range) As Long

MergeSize = r(1).MergeArea.Cells.Count

If MergeSize <= 10 Then
    MergeSize = MergeSize * 70
Else
    MergeSize = MergeSize * 65
End If

End Function

另一个问题是我可以将函数的输出发送到null并让它只显示在msgbox中吗?

1 个答案:

答案 0 :(得分:1)

尝试类似下面的代码。 我标记了我添加调用Function MergeSize的代码的位置。我使用Range("B2")作为合并范围。

<强>代码

Private Sub CommandButto1_click()

Dim answer As Integer
Dim Response As VbMsgBoxResult
Dim late As VbMsgBoxResult

answer = MsgBox("Price for only one product?", vbYesNoCancel + vbQuestion, "Payment")
If answer = vbYes Then
   late = MsgBox("Is the customer late and has to be charged extra?", vbQuestion + vbYesNoCancel)

   If late = vbYes Then
        '===== Added the 3 lines below =====
        Dim ExtraCharge As Long

        ExtraCharge = MergeSize(Range("B2")) '<-- Range("B2") is a Merged Cells
        ' === Ver 2.0 - to use with ActiveCell ===
        ExtraCharge = MergeSize(ActiveCell) '<-- ActiveCell is a Merged Cells

        MsgBox "Extra Charge is " & ExtraCharge
   End If
End If

End Sub