我有一个VBA功能,可以查找某个数字ID来决定使用的百分比。
我想让它更具动态性并且具有用户可以输入ID的范围,因此我不必手动更改它及其静态。正如您在陆上功能中所看到的那样。
Function ownershipOffshore(TaxLot)
If TaxLot = 0 Then
ownershipOffshore = 0#
ElseIf TaxLot = Sheets("Ownership %").Range("K26").Value Then
ownershipOffshore = Sheets("Ownership %").Range("E4").Value
' 51506431 Or TaxLot = 51533686 Or TaxLot = 51533687 Or TaxLot = 51533688 Or TaxLot = 51533689 Or TaxLot = 51706542 Or TaxLot = 51864049 Or TaxLot = 51953620 Or TaxLot = 51981353 Or TaxLot = 52142917 Or TaxLot = 52142919 Or TaxLot = 52142920
ElseIf TaxLot = Sheets("Ownership %").Range("K11:K22").Value Then
ownershipOffshore = Sheets("Ownership %").Range("E3").Value
Else
ownershipOffshore = Sheets("Ownership %").Range("E2").Value
End If
End Function
Function ownershipOnshore(TaxLot)
If TaxLot = 0 Then
ownershipOnshore = 0#
ElseIf TaxLot = 70376132 Then
ownershipOnshore = Sheets("Ownership %").Range("K26").Value
ElseIf TaxLot = 51506431 Or TaxLot = 51533686 Or TaxLot = 51533687 Or TaxLot = 51533688 Or TaxLot = 51533689 Or TaxLot = 51706542 Or TaxLot = 51864049 Or TaxLot = 51953620 Or TaxLot = 51981353 Or TaxLot = 52142917 Or TaxLot = 52142919 Or TaxLot = 52142920 Or TaxLot = 70376132 Then
ownershipOnshore = Sheets("Ownership %").Range("F3").Value
Else
ownershipOnshore = Sheets("Ownership %").Range("F2").Value
End If
End Function
答案 0 :(得分:0)
You can take in a range as a parameter to a function. You can find an example of passing a range into a UDF on my blog.
In your case, you would want to replace this Elif
ElseIf TaxLot = 51506431 Or TaxLot = 51533686 Or TaxLot = 51533687 Or TaxLot = 51533688 Or TaxLot = 51533689 Or TaxLot = 51706542 Or TaxLot = 51864049 Or TaxLot = 51953620 Or TaxLot = 51981353 Or TaxLot = 52142917 Or TaxLot = 52142919 Or TaxLot = 52142920 Or TaxLot = 70376132 Then ownershipOnshore = Sheets("Ownership %").Range("F3").Value Else
With a boolean function.
Private Function IsTaxLotInUserRange(byval rng Range, TaxLot)
Dim cell as Range
For Each cell in rng
If TaxLot = cell.Value Then
IsTaxLotInUserRange = True
Exit Function
End If
Next
End Function
And update your elif with
ElseIf IsTaxLotInRange(rng, TaxLot) Then
ownershipOnshor = ownershipOnshore = Sheets("Ownership %").Range("F3").Value
Else