【问题标题】:Uniqe arrays contains two values唯一数组包含两个值
【发布时间】:2017-01-12 14:05:58
【问题描述】:

我有行:

我想将此值添加到选项卡/数组中,以获得如下对:

[0] = 英,3

[1] = PL, 4

[2] = 美国,5 ...

[x] = 值,列号

请告诉我如何

1) 添加第二个值

2) 检查数组是否包含“Eng”或“PL”

目前为止

Dim CatTab() As Variant
Dim tabSize as Long
For b = 3 To 20
    Category = wsSum.Cells(2, b).Value
    tabSize = tabSize + 1
    ReDim Preserve CatTab(tabSize)
    CatTab(tabSize) = Category
Next

【问题讨论】:

  • 您可以声明一个二维数组,例如Dim CatTab(4,4)。不要认为您需要 Redim,因为您似乎预先知道大小。

标签: arrays vba excel unique dynamic-arrays


【解决方案1】:

我建议使用二维数组来存储单独的值。

Dim CatTab As Variant
'First dimension can't be redimmed, use this for headers. Second dimension can, use this to extend data.
ReDim CatTab(1 To 2, 1 To 1)

Dim tabSize As Long
For b = 3 To 20
    'Check if a resize is required
    If CatTab(1, UBound(CatTab, 2)) <> "" Then ReDim Preserve CatTab(1 To UBound(CatTab, 1), 1 To UBound(CatTab, 2) + 1)
    'Add value
    CatTab(1, UBound(CatTab, 2)) = wssum.Cells(2, b)
    'Add column
    CatTab(2, UBound(CatTab, 2)) = b
Next

遍历以检查特定值..

Dim lng As Long
For lng = LBound(CatTab, 2) To UBound(CatTab, 2)
    If InStr(1, CatTab(1, lng), "Eng") <> 0 Or InStr(1, CatTab(1, lng), "PL") <> 0 Then
        'code here
    End If
Next lng

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    相关资源
    最近更新 更多