Function f(arr() As Variant) As String Dim i, j As Integer, s As String For i = LBound(arr) To UBound(arr) s = "" For j = LBound(arr, 2) To UBound(arr, 2) s = s & Chr(9) & arr(i, j) Next j f = f & s & vbCrLf Next i End FunctionSub Macro1() Dim arr() As Variant arr = Selection'选中单元格必须超过2个以上 MsgBox f(arr) End Sub
楼主,我的代码不正是按你的意思写的吗? 莫非是你在调用函数前可以随意选中单元格,但是调用的函数执行到某条语句后才对当前选择的单元格进行操作? 如果是这样你所调用的函数根本不需要参数了:Function f() As String Dim i, j As Integer, s As String Dim arr() As Variant '''这里可以有很多操作,包括选中单元格 arr = Selection'选中单元格必须超过2个以上 For i = LBound(arr) To UBound(arr) s = "" For j = LBound(arr, 2) To UBound(arr, 2) s = s & Chr(9) & arr(i, j) Next j f = f & s & vbCrLf Next i msgbox f End FunctionSub Macro1() Dim arr() As Variant call f End Sub
真是不好意思,辛苦这位朋友了,可能我说不是很准确。我只想编一个函数,在一个单元格插入这个函数后(而不是用宏来调用),再把选定的值赋给参数。下面的函数的参数是两个单变量,插入这个函数后,可以选定两个单元格,单元格的值会传递给两个单变量。麻烦您帮我把这两个变量改成数组参数。谢谢!!! Public Function test(a, b As String) As String test = a & b End Function 改成: Public Function test(a(), b() As String) As String Dim i, j As Integer, s As String
'给数组a、b赋值的代码,这部分可就靠您了。a、b为一维数组。 For i = LBound(a) To UBound(a) s = s & a(i) Next i For j = LBound(b) To UBound(b) s = s & b(j) Next j test = s End Function
Dim i, j As Integer, s As String
For i = LBound(arr) To UBound(arr)
s = ""
For j = LBound(arr, 2) To UBound(arr, 2)
s = s & Chr(9) & arr(i, j)
Next j
f = f & s & vbCrLf
Next i
End FunctionSub Macro1()
Dim arr() As Variant
arr = Selection'选中单元格必须超过2个以上
MsgBox f(arr)
End Sub
莫非是你在调用函数前可以随意选中单元格,但是调用的函数执行到某条语句后才对当前选择的单元格进行操作?
如果是这样你所调用的函数根本不需要参数了:Function f() As String
Dim i, j As Integer, s As String
Dim arr() As Variant
'''这里可以有很多操作,包括选中单元格
arr = Selection'选中单元格必须超过2个以上
For i = LBound(arr) To UBound(arr)
s = ""
For j = LBound(arr, 2) To UBound(arr, 2)
s = s & Chr(9) & arr(i, j)
Next j
f = f & s & vbCrLf
Next i
msgbox f
End FunctionSub Macro1()
Dim arr() As Variant
call f
End Sub
Public Function test(a, b As String) As String
test = a & b
End Function
改成:
Public Function test(a(), b() As String) As String
Dim i, j As Integer, s As String
'给数组a、b赋值的代码,这部分可就靠您了。a、b为一维数组。 For i = LBound(a) To UBound(a)
s = s & a(i)
Next i For j = LBound(b) To UBound(b)
s = s & b(j)
Next j
test = s
End Function