繁体   English   中英

Excel VBA可供选择的新范围

[英]Excel VBA New range from selection

我正在使用复杂的UserDefined函数,该函数仅适用于单个连续的数据范围。 因此,我想创建一个函数,对于给定的范围,该函数将为我返回清空空白单元格的新范围,该范围将用作初始函数的输入。

到目前为止,这是我未成功完成的操作:

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

您能帮我解决一下吗?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM