vba 如何在excel VBA中根据行和列ID查找单元格值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16450512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:35:27  来源:igfitidea点击:

How to find cell value based on row and Column ID in excel VBA

excel-vbavbaexcel

提问by herrtodd33

Need a VBA Sub to find a cell value based on Row and Column ID. In the example below I need to select the value where East and RT3 intersect which is 80.

需要一个 VBA Sub 来根据行和列 ID 查找单元格值。在下面的示例中,我需要选择 East 和 RT3 相交的值,即 80。

    A   B   C   D   E
1   null    RT1 RT2 RT3 RT4
2   North   31  40  78  11
3   South   32  41  79  12
4   East    33  42  80  13
5   West    34  43  81  14

回答by glh

Use similar to below not tested:

使用类似于以下未测试

Function getcell(ct as string, rt as string) as range

    With activecell
        R = .columns("A").find(rt).row
        C = .rows("1").find(ct).Column
        'The below will escape the function if none where found
        If r = nothing or c = nothing then exit function
        Set getcell = .cells(r, c)
    End with

End function

回答by David

There are different ways to do this, but one approach is to use a function with parameters. You didn't say how you intent to pass the parameters, so I just used a sub to call the function.

有不同的方法可以做到这一点,但一种方法是使用带参数的函数。你没有说你打算如何传递参数,所以我只是用一个子来调用函数。

Function GetValue(row As Integer, col As Integer)
    GetValue = ActiveSheet.Cells(row, col)
End Function

Sub CallGetValue()
    Dim cellval As String
    cellval = GetValue(row:=4, col:=4)
    MsgBox (cellval)
End Sub