如何在vba中使用数组

发布于 2024-11-27 06:07:17 字数 203 浏览 0 评论 0原文

您将如何编写一个 VBA 函数:

  • 接受一个数组、一个可选的最小值和一个可选的最大值,
  • 返回一个数组,其中每个小于最小值的值都被最小值替换,每个大于最大值的值都被最大值替换?

如何通过

  1. 从工作表上的数组公式调用该函数并
  2. 写入 VBA 数组的范围来测试该函数?

How would you write a VBA function that:

  • takes an array, an optional minimum and an optional maximum
  • returns an array with each value less than the minimum replaced by the minimum value, and each value higher than the maximum replaced by the maximum value?

And how could I test the function by

  1. calling it from an array formula on a sheet and
  2. by writing to a range from a VBA array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戏舞 2024-12-04 06:07:17

这更多的是一种概念验证功能,而不是实用功能。如果您不想设置默认值,则需要使用 Variant 类型作为可选参数,而使用“IsMissing”函数。

它需要一个 Excel 范围,如 A1:A5 或 A1:B5,以及 2 个可选参数(最小值和最大值)。它会输出一个字符串,告诉您该范围的新值。

作为测试,在 A1 到 A5 中输入值 1、2、3、4、5。现在,在 B1 中写下以下内容:

=CheckArray(A1:A5,3)

您应该返回结果“范围现在是:3,3,3,4,5”,因为您没有给出最大值。
您还可以这样做:

=CheckArray(A1:A5,,3)

这将返回“现在的范围是:1, 2, 3, 3, 3”

Function CheckArray(ByVal cell_range As range, _
                    Optional ByVal min_value As Variant, _
                    Optional ByVal max_value As Variant)

Dim i As Long, j As Long
Dim vArray As Variant
Dim test As String
vArray = cell_range.Value

For i = 1 To UBound(vArray, 1)
    For j = 1 To UBound(vArray, 2)
        'Check the min. value
        If IsMissing(min_value) = False Then
            If vArray(i, j) < min_value Then
                vArray(i, j) = min_value
            End If
        End If
        'Check the max value
        If IsMissing(max_value) = False Then
            If vArray(i, j) > max_value Then
                vArray(i, j) = max_value
            End If
        End If
    Next
Next
' The function is done. The below is just to spit out a test result.
For i = 1 To UBound(vArray, 1)
    For j = 1 To UBound(vArray, 2)
        test = test & (", " & vArray(i, j))
    Next
Next

CheckArray = "The range is now: " & vbLf & Right(test, Len(test) - 2)

End Function

This is more of a proof of concept function than a practical one. You need to use type Variant for the optional parameters if you don't want to set a default value and instead us the "IsMissing" function.

It takes an excel range, like A1:A5, or A1:B5, and 2 optional paramters (min and max). It'll spit out a string telling you the new values of the range.

As a test, put values 1,2,3,4,5 in A1 through A5. Now, in B1, write this:

=CheckArray(A1:A5,3)

You should get back the result "The range is now: 3, 3, 3, 4, 5" since you did not give a max.
You can also do:

=CheckArray(A1:A5,,3)

And that will return "The range is now: 1, 2, 3, 3, 3"

Function CheckArray(ByVal cell_range As range, _
                    Optional ByVal min_value As Variant, _
                    Optional ByVal max_value As Variant)

Dim i As Long, j As Long
Dim vArray As Variant
Dim test As String
vArray = cell_range.Value

For i = 1 To UBound(vArray, 1)
    For j = 1 To UBound(vArray, 2)
        'Check the min. value
        If IsMissing(min_value) = False Then
            If vArray(i, j) < min_value Then
                vArray(i, j) = min_value
            End If
        End If
        'Check the max value
        If IsMissing(max_value) = False Then
            If vArray(i, j) > max_value Then
                vArray(i, j) = max_value
            End If
        End If
    Next
Next
' The function is done. The below is just to spit out a test result.
For i = 1 To UBound(vArray, 1)
    For j = 1 To UBound(vArray, 2)
        test = test & (", " & vArray(i, j))
    Next
Next

CheckArray = "The range is now: " & vbLf & Right(test, Len(test) - 2)

End Function
蓝天白云 2024-12-04 06:07:17

如果您只想测试您的 vba,您可以使用硬编码数组创建一个过程。

如果它是用户定义的函数,您可以使用范围作为参数来调用它,但您可能必须设置断点,因为 UDF 不会像经典 vba 那样显示错误。

If you only want to test your vba, you could create a procedure with a hard-coded array.

If it is a user-defined function, you can call it with a range as an argument, but you will probably have to set breakpoints because UDF do not display errors as classical vba does.

情场扛把子 2024-12-04 06:07:17

我真的不明白为什么你需要一个数组来实现这一点,而且你可以在没有 VBA 的情况下做到这一点!
假设您的 MIN 是 3,MAX 是 16,那么 =MAX(MIN(D6;16);3) 就可以了。
而且,如果您确实愿意,您仍然可以将其作为数组公式输入。

I don't really see why you would need an array for that, and you can do that without VBA !
Say your MIN is 3 and your MAX is 16, then =MAX(MIN(D6;16);3) will do the trick.
And, if you really want to, you can still enter that as an array formula.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文