细胞验证和阵列

发布于 2024-12-12 02:40:26 字数 755 浏览 0 评论 0原文

有两个问题,首先你能提醒我如何在分配多维数组时对其进行填充吗?我总是用 for 循环来完成它,如何静态地完成它似乎让我无法理解?

但我真正想做的是使用数组来验证单元格内容。但是在下面的示例中,我只想使用第二维中的第一个元素。


0,0
1,0
2,0
3,0
4,0

有没有办法更新 .Add Type:=xlValidateList, Formula1:=Join(ary, ",") 行,以便它只从 5 x 4 数组返回这 5 个值?

干杯

亚伦

Sub test()


Dim ary As Variant
ReDim ary(5,4)


ary = Array("Value1", "Value2", "Value3", "test", "test2", "test3", "test4")

With ActiveSheet.Cells(1, 1).Validation
    .Delete
    .Add Type:=xlValidateList, Formula1:=Join(ary, ",")
    .IgnoreBlank = False
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
End With
End Sub

two questions, first can you remind me how to polulate a multidimensional array when assigning it? I always do it with for loop and how to staticly do it seems to escape me?

But what I am really looking to do is to use the array to validate cells contents. however in the example below I want to use only the first element in the 2nd dimension.

ie
0,0
1,0
2,0
3,0
4,0

Is there any way to update the .Add Type:=xlValidateList, Formula1:=Join(ary, ",") line so it only returns these 5 values from a 5 by 4 array?

Cheeers

aaron

Sub test()


Dim ary As Variant
ReDim ary(5,4)


ary = Array("Value1", "Value2", "Value3", "test", "test2", "test3", "test4")

With ActiveSheet.Cells(1, 1).Validation
    .Delete
    .Add Type:=xlValidateList, Formula1:=Join(ary, ",")
    .IgnoreBlank = False
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
End With
End Sub

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

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

发布评论

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

评论(1

傲娇萝莉攻 2024-12-19 02:40:26

将 2D 数组大小调整为 1D 很快

下面的代码用示例数据填充 5*4 数组,然后第二个循环仅将第一个维度提取到新的 1D 数组,其行长度与初始数组相同

Sub test()

    Dim ary As Variant
    Dim X As Variant
    Dim lngRow As Long
    Dim lngCol As Long

    ReDim ary(1 To 5, 1 To 4)
    ReDim X(1 To UBound(ary, 1))

    For lngRow = 1 To UBound(ary, 1)
        For lngCol = 1 To UBound(ary, 2)
            ary(lngRow, lngCol) = "I am row " & lngRow & "  and dimension " & lngCol
        Next
    Next

    For lngRow = 1 To UBound(ary, 1)
    X(lngRow) = ary(lngRow, 1)
    Next

    With ActiveSheet.Cells(1, 1).Validation
        .Delete
        .Add Type:=xlValidateList, Formula1:=Join(X, ",")
        .IgnoreBlank = False
        .InCellDropdown = True
        .ShowInput = True
        .ShowError = True
    End With
End Sub

The resizing of a 2D array to 1D is quick

The code below populates your 5*4 array with sample data, a second loop then extracts only the first dimension to a new 1D array withe the same row length as your initial array

Sub test()

    Dim ary As Variant
    Dim X As Variant
    Dim lngRow As Long
    Dim lngCol As Long

    ReDim ary(1 To 5, 1 To 4)
    ReDim X(1 To UBound(ary, 1))

    For lngRow = 1 To UBound(ary, 1)
        For lngCol = 1 To UBound(ary, 2)
            ary(lngRow, lngCol) = "I am row " & lngRow & "  and dimension " & lngCol
        Next
    Next

    For lngRow = 1 To UBound(ary, 1)
    X(lngRow) = ary(lngRow, 1)
    Next

    With ActiveSheet.Cells(1, 1).Validation
        .Delete
        .Add Type:=xlValidateList, Formula1:=Join(X, ",")
        .IgnoreBlank = False
        .InCellDropdown = True
        .ShowInput = True
        .ShowError = True
    End With
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文