我有这个功能:
Public Price As Integer
Public Function MergeSize(r As Range) As Long
MergeSize = r(1).MergeArea.Cells.Count
If MergeSize <= 10 Then
Price = 70
MergeSize = MergeSize * price
Elseif MergeSize > 10 And MergeSize <= 21 Then
Price = 65
MergeSize = MergeSize * price
Else
Price = 60
MergeSize = MergeSize * Price
End If
End Function
问题是,我想在另一个Sub
中使用“Price”并根据if语句的结果显示它。
以下是我想要使用它的子部分:
Dim Charge As Long
Charge = MergeSize(ActiveCell)
MsgBox "Daily rate of: " & Price & vbNewLine & "Total is: " & Charge
答案 0 :(得分:1)
我应该这样写:
Public Price As Integer
Public Function MergeSize(r As Range) As Long
MergeSize = r(1).MergeArea.Cells.Count
If MergeSize <= 10 Then
Price = 70
Elseif MergeSize > 10 And MergeSize <= 21 Then
Price = 65
Else
Price = 60
End If
MergeSize = MergeSize * Price
End Function
答案 1 :(得分:0)
因为您现在要从VBA运行此代码(而不是从Excel单元格作为UDF),请将您的调用Sub
代码更改为:
Sub test()
Dim Charge As Long
'Charge = MergeSize(Range("B1")) '<-- send the range in the brackets
' === Active Cell ====
Charge = MergeSize(ActiveCell) '<-- send ActiveCell as range in the brackets
MsgBox "Daily rate of: " & Price & vbNewLine & "Total is: " & Charge
End Sub