簡體   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