excel - 从方阵中选择列向量的函数

标签 excel vba

嗨,我是 VBA 新手,并创建了一些基本功能,但以下证明太高级了。

我想创建一个将方阵转换为其列之一的函数。

      colA  colB   colC
[1,]   1      2      3
[2,]   4      5      6
[3,]   7      8      9

所以 getcol(A1:C3,2) 会输出:
2
5
8

我不确定是否可以从函数输出范围。

它用于数组 Match 函数中,因此:
{Index(G1:G3,Match(2&2,getcol(A1:C3)&getcol(D1:F3),0))}

我尝试过的代码是:
Function getcol(a As Range, b As Integer) As Range
getcol = Range(a).EntireColumn(b)
End Function

最佳答案

这是你正在尝试的吗?我没有测试过索引/匹配。以下代码仅返回相关范围。你如何使用它取决于你。

Option Explicit

Sub Sample()
    Dim rng As Range
    Dim truncRange As Range

    Set rng = Range("A1:C3")

    Set truncRange = getcol(rng, 2)

    If Not truncRange Is Nothing Then
        MsgBox truncRange.Address
    Else
        MsgBox "One of the parameters of getcol() is incorrect"
    End If
End Sub

Function getcol(a As Range, b As Integer) As Range
    Dim rowCount As Long

    '~~> Get the total rows in that range
    rowCount = a.Rows.Count

    '~~> Extract the range from relevant position
    Set getcol = Range(a.Cells(1, b), a.Cells(rowCount, b))
End Function

关于excel - 从方阵中选择列向量的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55394770/

相关文章:

excel - 使用 Python 将值列表写入不同列中的 csv 文件

java - 如何使用 poi 库获取 Excel 文件中特定列的行数

excel - VBA将Excel电子表格逐行导入Access

excel - 用VBA编写Excel Addin,然后放置一个触发它的按钮

vba - 宏来检查值是否在另一个列表中,如果是,则添加今天的日期

vba - VBA 中的文件搜索

java - 使用什么代替 ApachePOI 中已弃用的 CellRangeAddress.valueOf

python - Openpyxl - 我可以使用 Openpyxl 创建水平条形图吗?

arrays - 如何使用数组字典循环工作表

xml - 如何在 Excel 中打开 XML 文件