Tri*_*tan 5 excel vba range excel-vba
我想写一个VBA函数,它有一个Range作为可选参数.例如:
Public Function testfunc(S As String, Optional R As Range) As String
testfunc = S
For Each cell In R
testfunc = testfunc + cell
Next cell
End Function
Run Code Online (Sandbox Code Playgroud)
我尝试了上面的功能,但我得到了一个#VALUE!错误.我还尝试在For(R)Then ... End If语句中包装For循环.
什么是处理可选范围的方法,如果范围存在,那么它是通过For Each循环迭代的?
试试这个
Public Function testfunc(S As String, Optional R As Range = Nothing) As String
testfunc = S
if not R is nothing then
For Each cell In R
testfunc = testfunc & cell
Next cell
end if
End Function
Run Code Online (Sandbox Code Playgroud)
我已经在Excel 2007中测试了这个.你需要将它放入模块,而不是工作表或工作簿代码部分.然后可以从带有或不带Range对象的VBA调用该函数,或者作为工作表函数调用该函数.
归档时间: |
|
查看次数: |
20197 次 |
最近记录: |