[英]VBA #Value error in Excel 2010 but not in Excel 2016
So I wrote the following code on my pc at home (with Excel 2016 installed) where it works without any problems, but now that i try to use it at my work Excel gives my an #Value error. 因此,我在家里的PC上安装了以下代码(安装了Excel 2016),该代码可以正常工作,但是现在我尝试在工作中使用它,因此Excel出现了#Value错误。 I tried searching up differences between Excel 2016 and 2010, but failed to find anything regarding my issue. 我尝试搜索Excel 2016和2010之间的差异,但未找到任何有关我的问题的信息。
Public Function comparex(a, b As Long) As Byte
Dim a1, a2, b1, b2, e, e1 As Long
Dim y As Integer
y = 1
a1 = a - 1000
a2 = a + 1000
b1 = b - 1000
b2 = b + 1000
Do Until Sheets(2).Cells(y, 3) = ""
e = Sheets(2).Cells(y, 3).Value
e1 = Sheets(2).Cells(y, 4).Value
If a1 < e And a2 > e And b1 < e1 And b2 > e1 Then
comparex = 1
Exit Do
Else
y = y + 1
End If
Loop
End Function
Basically the script is supposed to check if there are values near a and b in a column on another sheet. 基本上,该脚本应该检查另一张纸上的一列中a和b附近是否存在值。
This procedure should work for you. 此过程应为您工作。 Rather than a Byte it returns a boolean, meaning either True or False, which would be more usual than a Byte. 它返回一个布尔值,而不是一个Byte,它表示True或False,这比Byte更常见。
Public Function CompareX(a As Long, b As Long) As Boolean
Dim a1 As Long, a2 As Long
Dim b1 As Long, b2 As Long
Dim e As Long, e1 As Long
Dim R As Long, Rl As Long
y = 1
a1 = a - 1000
a2 = a + 1000
b1 = b - 1000
b2 = b + 1000
With Sheets(2)
Rl = .Cells(.Rows.Count, 3).End(xlUp).Row
For R = 1 To Rl
e = .Cells(R, 3).Value
e1 = .Cells(R, 4).Value
If a1 < e And a2 > e And b1 < e1 And b2 > e1 Then
CompareX = True
Exit For
End If
Next R
End With
End Function
Although the above is more mainstream. 虽然以上更为主流。 it doesn't seem very useful if this case. 在这种情况下,它似乎不是很有用。 Apparently you are attempting to find a row in which all criteria are met. 显然,您正在尝试查找满足所有条件的行。 The reasonable result to expect would be the row number where this was the case. 可以预期的合理结果将是这种情况下的行号。 For such a purpose the function would be constructed slightly different, as shown below. 为此目的,功能将略有不同,如下所示。
Public Function CompareX(a As Long, b As Long) As Long
Dim a1 As Long, a2 As Long
Dim b1 As Long, b2 As Long
Dim e As Long, e1 As Long
Dim R As Long, Rl As Long
y = 1
a1 = a - 1000
a2 = a + 1000
b1 = b - 1000
b2 = b + 1000
With Sheets(2)
Rl = .Cells(.Rows.Count, 3).End(xlUp).Row
For R = Rl To 1 Step -1
e = .Cells(R, 3).Value
e1 = .Cells(R, 4).Value
If a1 < e And a2 > e And b1 < e1 And b2 > e1 Then
CompareX = R
Exit For
End If
Next R
End With
End Function
As you see, the function counts the row backward. 如您所见,该函数向后计数行。 If no match was found it will return zero, meaning, so long as it returns a number a match was found. 如果没有找到匹配项,它将返回零,这意味着,只要它返回一个数字,就会找到匹配项。 However, in case there are more matches than one, the row number will be that of the last match in the list. 但是,如果匹配项多于一个,则行号将是列表中最后一个匹配项的行号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.