vba Excel 将范围传递给函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8663159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel passing a range into a function
提问by Michaelb88
how do you go about passing a range of cells into a function so that you can then process the items separately. Therefore how do I pass a range of cells into a function.
您如何将一系列单元格传递给函数,以便您可以单独处理这些项目。因此,我如何将一系列单元格传递给函数。
I'm trying to do it so I can have a methods that uses the follow
我正在尝试这样做,因此我可以使用以下方法
Function processNumbers(Var as Range)
From this I'm not sure how I can get the number of items in the list and transverse the array to edit the contents. Is there a better way bring in the items than the above.
由此我不确定如何获取列表中的项目数并横向排列数组以编辑内容。有没有比上面更好的方法带入物品。
采纳答案by chris neilsen
Your function declaration as stated is the correct way to do it.
如上所述,您的函数声明是正确的方法。
Function processNumbers(Var as Range) As Variant
NumberOfCells = Var.Cells.Count
NumberOfRows = Var.Rows.Count
NumberOfColumns = Var.Columns.Count
RangeAddress = Var.Address
' Iterate the range (slow)
For Each Cl in Var.Cells
' ...
Next
' Get Values from range as an array
Dim Dat as variant
Dat = var
' Iterate array
For rw = LBound(Dat,1) to UBound(Dat,1)
For col = LBound(Dat,2) to UBound(Dat,2)
' reference Dat(rw,col)
Next col
Nest rw
' Put (modified) values back into range. Note: won't work in a UDF
Val = Dat
End Function