excel - 使用VBA将计算结果输出到Excel中的多个单元格

标签 excel user-defined-functions vba

我使用 VBA 在 Excel 中编写一个函数,该函数将搜索列表以查找某些名称,当某些要查找的名称出现时进行计数,然后将这些计数器值输出到各个单元格。

当我有一个多单元函数时,如何将值分配给函数本身?我在同一列中选择了 4 个相邻的单元格,然后按 CTRL-SHIFT-ENTER 以获得多单元格函数,我只是不知道如何将结果分配给该函数,以便它将显示在选定的单元格中。到目前为止我所做的如下所示:

Function ROM(ByVal lookup_value As Range, _
ByVal lookup_column As Range, _
ByVal return_value_column As Long) As String 

Application.ScreenUpdating = False

Dim i As Long
Dim resultCount As Long
Dim resultsArray() As String
Dim arraySize As Long
Dim myrange As Range
Dim results As String
Dim TSS As Long
Dim OSS As Long
Dim AWS As Long
Dim JLI As Long
Dim answers(1 To 3, 1 To 1) As Variant


' The following code works out how many matches there are for the lookup and creates an
' array of the same size to hold these results

Set myrange = lookup_column
arraySize = Application.WorksheetFunction.CountIf(myrange, lookup_value.Value)
ReDim resultsArray(arraySize - 1)

' A counter for the results

resultCount = 0
TSS = 0
OSS = 0
AWS = 0
JLI = 0

' The equipment ID column is looped through and for every match the corresponding Equipment Type is
' saved into the resultsArray for analysis

For i = 1 To lookup_column.Rows.count
    If Len(lookup_column(i, 1).Text) <> 0 Then
        If lookup_column(i, 1).Text = lookup_value.Value Then

                ' If statement to ensure that the function doesnt cycle to a number larger than the
                ' size of resultsArray

                If (resultCount < (arraySize)) Then
                    resultsArray(resultCount) = (lookup_column(i).Offset(0, return_value_column).Text)
                    results = (lookup_column(i).Offset(0, return_value_column).Text)
                    resultCount = resultCount + 1
                        ' The following code compares the string to preset values and increments
                        ' the counters if any are found in the string

                        If (InStr(results, "TPWS TSS") > 0) Then
                            TSS = TSS + 1

                        ElseIf (InStr(results, "TPWS OSS")) Then
                            OSS = OSS + 1

                        ElseIf (InStr(results, "JUNCTION INDICATOR (1 Route)") > 0) Then
                            JLI = JLI + 1

                        ElseIf (InStr(results, "AWS")) Then
                            AWS = AWS + 1

                        End If

                 End If
        End If
    End If
Next

 answers(1, 1) = TSS
 answers(1, 2) = OSS
 answers(1, 3) = AWS
 answers(1, 4) = 0

  ROM = answers    


Application.ScreenUpdating = True


End Function

当我尝试运行该函数时,它一直显示答案类型不匹配。为多单元格公式选择的单元格为 F18、G18、H18 和 I18。

最佳答案

从 VBA 返回数组函数

  1. 您的函数必须是 Variant 类型
  2. 您的输出数组必须与所选范围匹配 - 在您的情况下它必须是一维的 而您正在指定二维数组的尺寸

试试这个

Function MyArray() As Variant
Dim Tmp(3) As Variant

    Tmp(0) = 1
    Tmp(1) = "XYZ"
    Tmp(2) = 3
    Tmp(3) = 4

    MyArray = Tmp

End Function

现在选择 F18..I18,输入 =MyArray() 并按 Ctrl+Shift+Enter

希望这有帮助。

关于excel - 使用VBA将计算结果输出到Excel中的多个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12802255/

相关文章:

excel - 从 Excel VBA UDF 调用时,Range.Precedents 返回范围而不是其先例。有解决方法吗?

excel - 独立 Excel 宏

vba - 使用 vba 地址用工作表函数结果填充 Excel 范围

java - 如何使用 Java Apache POI 将日期格式从 04-May-2016 转换为 04/05/2016

java - java应用程序可以管理Derby数据库中的触发器和UDF吗?

javascript - 使用任何编程语言或脚本或通过工具更改 JSON 内容以获取 excel 格式的数据?

oracle - Oracle 用户定义聚合函数的问题

excel - 如何验证 2 个变量是整数类型?

c# - 从 ASP.NET 将数据导出为 Excel 文件

c# - 使用 Gembox.Spreadsheet 提取到数据表的无效数据值