【发布时间】:2014-03-06 23:11:23
【问题描述】:
我有一个 Excel 工作表,其中有一列中有一些字符串。有时所有条目都相同,有时则不同:
我写了一个函数将范围作为参数传递:
=Dent_WG(A1:A6)
VBA 函数应确定哪种情况为真(所有条目 = “Al”,或至少一个条目 = “Ag”),然后分别返回 0 或 12:
Function DentWG(WG_Mat As Range) As Single
Dim dat As Variant, rw As Variant, temp As Single
dat = WG_Mat
temp = 0
For rw = LBound(dat, 1) To UBound(dat, 1)
If dat(rw, 1) = "Ag" Then
temp = 12
End If
Next
If temp = 12 Then
DentWG = 12
Else
DentWG = 0
End If
End Function
但是,该函数始终返回 0,即使对于“Ag”出现在范围内的第二种情况也是如此。我确定我未能正确地将范围转换为数组或将预期的逻辑正确应用于该数组。
【问题讨论】: