简体   繁体   English

excel VBA对于每个循环,范围错误#VALUE

[英]excel VBA For each loop for range error #VALUE

I have used for each loop through my defined function in excel VBA but the function output is #VALUE. 我已经在excel VBA中通过定义的函数使用了每个循环,但函数输出为#VALUE。 I think the error is inside my loop that is as follow: 我认为错误是在我的循环内,如下所示:

Thank you guys for comments. 谢谢大家的评论。 My function code is as follow that I have corrected the original one I sent : 我的功能代码如下,我已更正了我发送的原始代码:

Public Function MaxWindDir(warr As Range) As Integer
Dim WindDir() As Integer
Dim maxwindsp As Integer
Dim wcell As Range
Dim i As Integer
Dim MaxDir As Integer
i = 0
maxwindsp = Application.Max(warr)
For Each wcell In warr
    If wcell.Value = maxwindsp Then
        WindDir(i) = Range("J" & wcell.Row).Value
        i = i + 1
    End If
Next wcell
If i = 1 Then
    MaxWindDir = WindDir(0)
Else
    MaxDir = WindDir(0)
    For i = 0 To UBound(WindDir)
        If WindDir(i) >= MaxDir Then
            MaxDir = WindDir(i)
        End If
    Next i
    MaxWindDir = MaxDir
End If
End Function

Please help me out of this error. 请帮我解决这个错误。

The error is occurring because your WindDir array is undimensioned, so the lines WindDir(i)... will throw an error. 发生错误是因为您的WindDir数组是无尺寸的,所以WindDir(i)...行将引发错误。

It's not great form to iteratively redimension an array within a loop, but the added ReDim Preserve line below in your code shows you what I mean: 在循环中迭代地重新定义数组的格式不是很好的形式,但是下面在代码中添加的ReDim Preserve行显示了我的意思:

For Each wcell In warr
    If wcell.Value = maxwindsp Then
        ReDim Preserve WindDir(i)
        WindDir(i) = Range("J" & wcell.Row).Value
        i = i + 1
    End If
Next wcell

Your post throws up a couple of other points too: 您的帖子也提出了其他几点:

  1. Perhaps consider calling your UDFs from a module while you develop them. 也许考虑在开发UDF时从模块中调用它们。 This way any errors will be more meaningfully displayed to you. 这样,任何错误都会更有意义地显示给您。 If you run the following routine, calling your function as posted, the undimensioned array issue would be highlighted immediately: 如果您运行以下例程,并按发布后的方式调用函数,那么未标注尺寸的数组问题将立即突出显示:

     Public Sub RunMe() Dim r As Range Set r = Sheet1.Range("K2:K19") 'or whatever the range is Debug.Print MaxWindDir(r) End Sub 
  2. Why trouble yourself with the array at all? 为什么要为阵列烦恼呢? Given that you're accessing the WindDir values in the first loop, why not just check for the maximum there? 假设您在第一个循环中访问WindDir值,为什么不只检查那里的最大值? This way, your entire function could simply be: 这样,您的整个功能可能只是:

     Public Function MaxWindDir(warr As Range) As Long Dim wcell As Range Dim maxWindSpd As Long Dim windSpd As Long Dim windDir As Long maxWindSpd = Application.Max(warr) For Each wcell In warr.Cells windSpd = wcell.Value2 If windSpd = maxWindSpd Then windDir = warr.Worksheet.Cells(wcell.Row, "J").Value2 If windDir >= MaxWindDir Then MaxWindDir = windDir End If Next End Function 
  3. Note @Comitern's point about the Range qualifier (and my rather clumsy attempt at keeping faithful to your posted code whilst still qualifying the range). 请注意@Comitern关于Range限定符的观点(以及我相当笨拙的尝试,即忠实于您发布的代码,同时仍然限定范围)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM