我尝试在其他文件上编写简化的工作形式,但它们确实起作用,所以我不明白为什么这样做不行。我刚刚将所有内容都定义为变体,但仍然无法使用。变量type1和type2是字符串,单词是字符串的范围,而数字是数字的范围。代码是在字符串范围内查找输入的字符串,并使用数字范围内的相应数字进行计算。
Function effectiveness(type1 As Variant, type2 As Variant, words As Range, numbers As Range) As Variant
Dim Types() As Variant
Types = words
Dim Multipliers() As Variant
Multipliers = numbers
Dim i As Variant
Dim h As Variant
Dim col As Variant
col = UBound(Types) - LBound(Types) + 1
Dim d As Variant
d = Excel.WorksheetFunction.Index(words, i)
Dim f As Variant
f = Excel.WorksheetFunction.Index(words, h)
For i = 1 To col
If StrComp(type1, d, vbBinaryCompare) = 0 Then '(0 means they match)
Dim sum As Variant
sum = 0
If StrComp(type1, type2, vbBinaryCompare) = 0 Then
Dim x As Variant
Dim a As Variant
For x = 1 To col
a = Excel.WorksheetFunction.Index(numbers, x, i)
sum = sum + a
Next x
Else
For h = 1 To col
If StrComp(type2, f, vbBinaryCompare) = 0 Then
Dim y As Variant
Dim b As Variant
Dim c As Variant
For y = 1 To col
b = Excel.WorksheetFunction.Index(numbers, y, h)
c = Excel.WorksheetFunction.Index(numbers, y, i)
sum = sum + (b * c)
Next y
End If
Next h
End If
End If
Next i
effectiveness = sum
End Function