[英]Excel VBA New range from selection
I'm working with a complex UserDefined function that works only with a single and contiguous range of data. 我正在使用复杂的UserDefined函数,该函数仅适用于单个连续的数据范围。 Therfore I would like to create a function, which for a given range, returns me a new range emptied of blank cells, which will serve as input for the initial function. 因此,我想创建一个函数,对于给定的范围,该函数将为我返回清空空白单元格的新范围,该范围将用作初始函数的输入。
Here's what what I have unsuccessfuly done so far: 到目前为止,这是我未成功完成的操作:
Public Function NewRange(rng) As Range
rng.SpecialCells(xlCellTypeConstants).Select
rng.SpecialCells(xlCellTypeFormulas).Select
rng = Union(rng.SpecialCells(xlCellTypeConstants), _
rng.SpecialCells(xlCellTypeFormulas)).Select
Set NewRange = rng
End Function
Could you help me please in solving it? 您能帮我解决一下吗?
Public Function NewRange(ByRef rng As Range) As Range
Dim nrC As Range, nrF As Range
If Len(rng.Value2) = 0 Then Set rng = rng.Parent.UsedRange.Cells(1, 1)
Set rng = rng.CurrentRegion
On Error Resume Next
Set nrC = rng.SpecialCells(xlCellTypeConstants)
Set nrF = rng.SpecialCells(xlCellTypeFormulas)
Select Case True
Case Not nrC Is Nothing And Not nrF Is Nothing: Set NewRange = Union(nrC, nrF)
Case Not nrC Is Nothing: Set NewRange = nrC
Case Not nrF Is Nothing: Set NewRange = nrF
End Select
If NewRange Is Nothing Then Set NewRange = rng.Parent.Cells(1)
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.