简体   繁体   English

用于多个vlookup的Excel VBA代码

[英]Excel VBA code for multiple vlookup

For a conduit network, I am trying to find the pipes that drain to a manhole. 对于管道网络,我正在尝试寻找排泄到人孔的管道。 There can be multiple pipes that can drain to a single manhole. 可以有多个可以排到一个人孔的管道。 My data-structure is organized in the following way: 我的数据结构是按以下方式组织的:

   Stop Node    Label
  .......................
    MH-37       CO-40
    MH-37       CO-40
    MH-39       CO-43
    MH-37       CO-44
    MH-39       CO-45
    MH-41       CO-46
    MH-35       CO-47
    MH-44       CO-50
    MH-39       CO-51
    MH-44       CO-52

and so on. 等等。

Of course, in Excel, we can workaround the multiple vlookup question using array equations. 当然,在Excel中,我们可以使用数组方程式解决多个vlookup问题。 However, I am not sure how it is done in Excel VBA coding. 但是,我不确定在Excel VBA编码中是如何完成的。 I need to automate the whole process and hence Excel VBA coding. 我需要自动化整个过程,因此需要Excel VBA编码。 This task is part of a bigger assignment. 此任务是一项更大任务的一部分。

Following is the function code I wrote so far: 以下是我到目前为止编写的功能代码:

Function Conduitt(M As String) As String()

Dim Stop_Node As Variant /* All Manhole label */
Dim Conduit As Variant /* All conduit label */
Dim compare As Variant /* Query Manhole label */
Dim Result() As String
Dim countc As Integer

Stop_Node = ActiveSheet.Range("B2:B73").Value
Conduit = ActiveSheet.Range("C2:C73").Value
compare = M

countc = 1

Do While countc <= 72

If Application.IsError(Application.Match(Stop_Node(countc), compare)) = 0 Then

Result(countc) = Conduit(countc)

End If

countc = countc + 1

Loop

Conduitt = Result()

End Function

If you compare the sample of data I provided before, For Manhole MH-39 , corresponding conduit labels are, CO-43 , CO-45 and CO-51 . 如果您将我之前提供的数据样本进行比较,对于Manhole MH-39 ,相应的导管标签为CO-43CO-45CO-51 I thought, with countc changing due to do loop, it will go through the list and find the exact matches for MH-39 and return CO-43 , CO-45 and CO-51 . 我以为,由于do循环导致countc发生变化,它将遍历列表并找到MH-39的确切匹配项,并返回CO-43CO-45CO-51

Objective is to return these conduit labels only as a string array with three rows (for MH-39 case). 目的是仅将这些导管标签作为三行的字符串数组返回 (对于MH-39情况)。

So far, when I run the code, I get : 到目前为止,当我运行代码时,我得到:

Run-time error '9': Subscript out of range. 运行时错误'9':下标超出范围。

I searched different forums and found it happens when non-existing array elements are referenced. 我搜索了不同的论坛,并发现当引用不存在的数组元素时会发生这种情况。 At this point, my limited knowledge and experience are not helping decipher the puzzle. 在这一点上,我有限的知识和经验无法帮助您解开谜题。

After some suggestions from R3uK , got the code fixed. 经过R3uK的一些建议后,代码已修复。 Apparently, when a range is assigned to a variant array (as in the case of Stop_Node and Conduit), the variant will be multi-dimensional. 显然,将范围分配给变量数组时(如Stop_Node和Conduit),变量将是多维的。 So, updated the code accordingly and incorporated Preserve with Redim. 因此,相应地更新了代码,并将Preserve与Redim合并在一起。

İn case you are interested, the updated code: 如果您有兴趣,请更新代码:

Function Conduitt(Manhole As String) As String()

Dim Stop_Node As Variant
Dim Conduit As Variant
Dim Result() As String

ReDim Result(0)

Stop_Node = ActiveSheet.Range("B2:B73").Value
Conduit = ActiveSheet.Range("C2:C73").Value

For i = LBound(Stop_Node) To UBound(Stop_Node)
If Stop_Node(i, 1) <> Manhole Then
Else
    Result(UBound(Result)) = Conduit(i, 1)
    ReDim Preserve Result(UBound(Result) + 1)
End If
Next i
ReDim Preserve Result(UBound(Result) - 1)

Conduitt = Result

In fact, you never ReDim your Result() so it is just an empty array with no actual cell (not even an empty cell), you first need to ReDim it. 实际上,您永远不需要ReDim您的Result()因此它只是一个没有实际单元格的空数组(甚至没有一个空单元格),您首先需要对其进行ReDim

Here is my version, I didn't use the function Match but that should work anyway : 这是我的版本,我没有使用Match函数,但是无论如何应该起作用:

Function Conduitt(ManHole As String) As String()

Dim Stop_Node As Variant '/* All Manhole label */
Dim Conduit As Variant '/* All conduit label */
Dim Result() As String

ReDim Result(0)

Stop_Node = ActiveSheet.Range("B2:B73").Value
Conduit = ActiveSheet.Range("C2:C73").Value

For i = LBound(Stop_Node) To UBound(Stop_Node)
    If Stop_Node(i,1) <> ManHole Then
    Else
        Result(UBound(Result)) = Stop_Node(i,1)
        ReDim Preserve Result(UBound(Result) + 1)
    End If
Next i
ReDim Preserve Result(UBound(Result) - 1)

Conduitt = Result()

End Function

Well, see you solved it, but here is an alternative solution (had to post it now that I have worked on it) 好吧,看到您已经解决了它,但是这里有一个替代解决方案(我已经处理了,现在必须发布它)

Function ConduittCheck(manhole As String) As String()
Dim result() As String

Dim manholeRange As Range
Dim conduittRange As Range
Set manholeRange = Range("manholes")
Set conduittRange = Range("conduitts")

Dim counter As Integer
Dim size As Integer
size = 0

For counter = 0 To manholeRange.Rows.Count
    If manholeRange.Rows.Cells(counter, 1) = manhole Then
        ReDim Preserve result(size)
        result(size) = conduittRange.Rows.Cells(counter, 1)
        size = size + 1
    End If
Next counter
ConduittCheck = result()
End Function

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

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