Excel VBA从所选内容创建新范围
我使用的是一个复杂的用户定义函数,它只能处理单个连续的数据范围。 因此,我想创建一个函数,对于给定的范围,它将返回一个空单元格的新范围,作为初始函数的输入 以下是我迄今为止未成功完成的工作:Excel VBA从所选内容创建新范围,excel,vba,Excel,Vba,我使用的是一个复杂的用户定义函数,它只能处理单个连续的数据范围。 因此,我想创建一个函数,对于给定的范围,它将返回一个空单元格的新范围,作为初始函数的输入 以下是我迄今为止未成功完成的工作: Public Function NewRange(rng) As Range rng.SpecialCells(xlCellTypeConstants).Select rng.SpecialCells(xlCellTypeFormulas).Select rng = Union(r
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
你能帮我解决这个问题吗?rng是一个对象。它必须使用Set关键字。所以:设置rng=。。。。同时删除同一语句末尾的.select。您不能这样做。无法创建不在工作表上的范围。您需要更改UDF以处理不连续的范围或忽略空白单元格,或者实际将数据复制到某个位置。
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