2017-02-01 72 views
1

我的原始问题是如何在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 

它正常工作,因为它是,但它说 - “在这里mergesize功能” 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 

另一侧的问题是我能发送到空函数的输出,并只在一个MsgBox显示?

回答

1

尝试类似下面的代码。 Iv'e标记了我添加了代码的位置,该代码调用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 
+0

再次感谢,晒!我真的不明白如何使用这个公式。我一直得到1的返回值(即时猜测B2)。有没有办法让它适用于我用鼠标指向的任何合并单元格块? – Eran

+0

@Eran你想使用'ActiveCell'?你用鼠标选择的单元格?如果这种情况使用'ExtraCharge = MergeSize(ActiveCell)',请参阅编辑的代码(并删除代码中的前一个代码) –

+0

Yesss!它很棒!户田户田户田!:) – Eran