【问题标题】:Pass repeated row values to an array in excel将重复的行值传递给excel中的数组
【发布时间】:2016-09-20 14:48:38
【问题描述】:

我在 excel 中有一个如下表,

Hub Depend
a   b
a   c
b   d
b   e
b   f
c   g
c   h
d   i
d   j
f   k
h   l
h   m
m   o

我需要将 Hubs 和 Depends 提取到这样的数组中

(b,d) and (b,e) and (b,f).

我的代码是:

For i = 2 To lastrow
sid = Cells(i, 1).Value
res = WorksheetFunction.VLookup(sid, Sheet1.Range("A:A"), 2, False)
Next

但是当我使用 VLOOKUP 时,它只会给我 (b,d) 并且在找到第一个匹配项后不会处理。

【问题讨论】:

  • 你想要的是以这种方式获取和排列:myArray() ===> myArray(1) = (a,b), myArray(2) = (a,c)
  • 是的,exacxtley,但我不知道 vlookup 是否可以特别使用 VBA 来做到这一点

标签: arrays excel vba vlookup


【解决方案1】:

嗯,希望对你有帮助:

Sub SinaNouri()
    Dim i As Range
    Dim rng1 As Range
    Dim c As Integer 'just a counter
    Dim myArray()
    Set rng1 = Range("A2:A14") 'the range of the first column

    c = 0
    For Each i In rng1
        c = c + 1
        ReDim Preserve myArray(1 To c)
        myArray(c) = "(" & i.Value & "," & i.Offset(0, 1).Value & ")"
    Next i
End Sub

注意

myArray(#) 将包含文本 (Strings) 而不是数字。如果你想处理数字,请使用:

Sub SinaNouri()
    Dim i As Range
    Dim rng1 As Range
    Dim c As Integer 'just a counter
    Dim myArray()
    Set rng1 = Range("A2:A14") 'the range of the first column

    c = 0
    For Each i In rng1
        c = c + 1
        ReDim Preserve myArray(1 To c)
        myArray(c) = i.Value & "," & i.Offset(0, 1).Value '!!!!!!
    Next i
End Sub

myArray(#) 将它与Split 一起使用,这样Split(myArray(2), ",")。您删除逗号,并仅获取数字以根据需要使用它。

【讨论】:

    猜你喜欢
    • 2021-08-21
    • 2010-11-24
    • 2018-05-07
    • 2017-10-06
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多