excel - excel vba - 返回两个值,传递一个输入参数
问题描述
情况就是这样:
我需要从一个函数返回两个值:这个函数需要一个输入参数才能工作。
strTitleName:输入参数
sName:输出参数
sScope:输出参数
Function getScenarioName(strTitleName As String, sName As String, sScope As String)
activateSheet ("Test Scenarios")
Dim rng1 As Range
Dim strSearch As String
strSearch = strTitleName & "*"
Set rng1 = Range("B:B").Find(strSearch, , xlValues, xlWhole)
If Not rng1 Is Nothing Then
'getScenarioName = rng1.Offset(0, 0)
sName = rng1.Address
sScope = rng1.Offset(1, 1).Address
Debug.Print "sName=" & sName
Debug.Print "sScope=" & sScope
End If
如何在子程序中获取 sName 和 sScope 的值?
解决方案
您可以在函数内部创建一个数组来存储这两个值。然后,返回数组。
例如:
'If strTitleName is your only argument, then:
Function getScenarioName(strTitleName As String) As Variant
Dim rng1 As Range
Dim strSearch As String
Dim result(1) As String
activateSheet ("Test Scenarios")
Set rng1 = Range("B:B").Find(strSearch, , xlValues, xlWhole)
strSearch = strTitleName & "*"
result(0) = ""
result(1) = ""
If Not rng1 Is Nothing Then
sName = rng1.Address
sScope = rng1.Offset(1, 1).Address
Debug.Print "sName=" & sName
Debug.Print "sScope=" & sScope
result(0) = "sName=" & sName
result(1) = "sScope=" & sScope
End If
getScenarioName = result
End Function
推荐阅读
- python-3.x - 找不到数据源:kafka
- android - RecyclerView 数据传递给同一个 Activity
- php - 如何在多个给定输入后输出用户详细信息?
- python - 如何关闭使用 pdfquery 打开的文件,它阻止了 pdf 文件?
- c++ - 并行 For 循环:查找已排序数组是否包含重复元素
- r - 如何将分组变量添加到数据集中,该数据集将根据某些条件对观测值及其 N 个邻居进行分类
- r - 筛选R中某个范围内的元素
- python - 使用代码在 python 中启用/禁用断言语句
- r - 如何根据R中的出生日期评估十二生肖?
- node.js - RabbitMQ 在比较同一队列中的其他消息后使用消息并重新排队