我是一个编码新手,所以要善良,但我正在寻找一种方法来返回具有最高报销率的票据费率类型。在我搜索的三个变量中,我想存储返回到表中的变量的名称,以便用户可以确定使用了哪种帐单类型。这是从数据库中提取计费率的代码。
CenterAllRate = DLookup("CenterBillRateAllVendors", "Centers", "CenterID = " & Chr(34) & CenterID & Chr(34))
CenterHospitalRate = DLookup("CenterBillRateHospitalOnly", "Centers", "CenterID = " & Chr(34) & CenterID & Chr(34))
VendorRate = DLookup("VendorReimbRate", "Vendors", "VendorID = " & Chr(34) & Me.Parent.VendorID & Chr(34))
ReimbursementRate = MaxOfList(CenterAllRate, CenterHospitalRate, VendorRate)
我正在使用我在网上找到的这个预建功能,以找到三个选项中最高的报销率。
Function MaxOfList(ParamArray varValues()) As Variant
Dim i As Integer 'Loop controller.
Dim varMax As Variant 'Largest value found so far.
varMax = Null 'Initialize to null
For i = LBound(varValues) To UBound(varValues)
If IsNumeric(varValues(i)) Or IsDate(varValues(i)) Then
If varMax >= varValues(i) Then
'do nothing
Else
varMax = varValues(i)
End If
End If
Next
MaxOfList = varMax
End Function
如何返回具有最高值的变量名称,或者在创建记录时使用别名?
由于
答案 0 :(得分:1)
由于你是一名编码新手,这是一个深入挖掘的好机会。 :)
我会创建一个对象来包装Name
和Rate
属性并将其传递给我的函数。
一个例子:
简单的报销类:
Option Explicit
Private name_ As String
Private rate_ As Variant
Public Property Get Name() As String
Name = name_
End Property
Public Property Let Name(Value As String)
name_ = Value
End Property
Public Property Get Rate() As Variant
Rate = rate_
End Property
Public Property Let Rate(Value As Variant)
rate_ = Value
End Property
您的方法稍作修改:
Function MaxOfList(varValues As Variant) As Reimbursement
Dim i As Integer 'Loop controller.
Dim varMax As Reimbursement 'Largest value found so far.
Set varMax = New Reimbursement
For i = LBound(varValues) To UBound(varValues)
If IsNumeric(varValues(i).Rate) Or IsDate(varValues(i).Rate) Then
If varMax.Rate < varValues(i).Rate Then
With varMax
.Name = varValues(i).Name
.Rate = varValues(i).Rate
End With
End If
End If
Next
Set MaxOfList = varMax
End Function
最后,致电并测试它:
Sub T()
Dim centerAll As New Reimbursement
Dim centerHospital As New Reimbursement
Dim vendor As New Reimbursement
Dim maxReimbursement As New Reimbursement
With centerAll
.Name = "centerAll"
'.Rate = DLookup("CenterBillRateAllVendors", "Centers", "CenterID = " & Chr(34) & CenterID & Chr(34))
.Rate = 3
End With
With centerHospital
.Name = "centerHospital"
'.Rate = DLookup("CenterBillRateHospitalOnly", "Centers", "CenterID = " & Chr(34) & CenterID & Chr(34))
.Rate = 8
End With
With vendor
.Name = "vendor"
'.Rate = DLookup("VendorReimbRate", "Vendors", "VendorID = " & Chr(34) & Me.Parent.VendorID & Chr(34))
.Rate = 5
End With
Set maxReimbursement = MaxOfList(Array(centerAll, centerHospital, vendor))
Debug.Print maxReimbursement.Name, maxReimbursement.Rate
End Sub
'Output
'centerHospital 8
答案 1 :(得分:0)
欢迎来到SO。
我设计了一个可能解决问题的糟糕解决方案,但它可以在某些特定条件下运行:
MaxOfList(CenterAllRate, CenterHospitalRate, VendorRate)
MaxOfList(AnotherAllRate, DifferentAllRate, ExampleRate)
我的解决方案将无法返回正确的名字。它只适用于CenterAllRate, CenterHospitalRate, VendorRate
好的,此后这是我糟糕的解决方案。我声明了一个Public String var来存储名为HighestVar
的列表中最高的名称。每次调用函数MaxofList
Option Explicit
Public HighestVar As String 'We declare the public Var to store name of max rate
Function MaxOfList(ParamArray varValues()) As Variant
Dim i As Integer 'Loop controller.
Dim varMax As Variant 'Largest value found so far.
HighestVar = "" 'We Reset the var
varMax = Null 'Initialize to null
For i = LBound(varValues) To UBound(varValues)
If IsNumeric(varValues(i)) Or IsDate(varValues(i)) Then
If varMax >= varValues(i) Then
'do nothing
Else
varMax = varValues(i)
Select Case i 'We use select Case to store in HighestVar the name of highest rate
Case 0
HighestVar = "CenterAllRate"
Case 1
HighestVar = "CenterHospitalRate"
Case 2
HighestVar = "VendorRate"
End Select
End If
End If
Next
MaxOfList = varMax
End Function
每次调用函数MaxofList
时,您都会在var HighestVar
中获得最高费率的名称。只需在您想要的地方使用它。例如:
CenterAllRate = DLookup("CenterBillRateAllVendors", "Centers", "CenterID = " & Chr(34) & CenterID & Chr(34))
CenterHospitalRate = DLookup("CenterBillRateHospitalOnly", "Centers", "CenterID = " & Chr(34) & CenterID & Chr(34))
VendorRate = DLookup("VendorReimbRate", "Vendors", "VendorID = " & Chr(34) & Me.Parent.VendorID & Chr(34))
ReimbursementRate = MaxOfList(CenterAllRate, CenterHospitalRate, VendorRate)
Msgbox HighestVar & " is the highest rate" 'We show in Msgbox highest rate
希望这可以帮到你。让我知道。