簡體   English   中英

Excel VBA僅連接已過濾列的可見單元格。 包含測試代碼

[英]Excel VBA Concatenate only visible cells of filtered column. Test code included

大家好

我正在嘗試將過濾的列連接成一個用逗號分隔的單元格。 我對編碼知之甚少,經過數小時的搜索,其他人提供的代碼也是。

到目前為止,此功能有效,但也可以連接不可見,已濾除的單元格:

Function test1(myRange As Range)
Dim aOutput
For Each entry In myRange
    If Not IsEmpty(entry.Value) Then
        aOutput = aOutput & entry.Value & ", "
    End If
Next
test1 = Left(aOutput, Len(aOutput) - 1)
End Function

這個方法效果很好,它也可以從范圍中刪除重復項,但是存在相同的問題:

Function test2(ByRef rRng As Range, Optional ByVal sDelim As String = ", ") As String
Dim oDict As Object
Dim rCell As Range
Dim sTxt As String
Set oDict = CreateObject("Scripting.Dictionary")
With oDict
    For Each rCell In rRng
        If .Exists(rCell.Text) Then
            'Do nothing
        Else
            .Add rCell.Text, rCell.Text
            sTxt = sTxt & sDelim & rCell.Text
        End If
    Next rCell
End With
    test2 = Mid(sTxt, Len(sDelim) + 1)
End Function

是否可以更改這兩個函數以忽略列中不可見,已濾除的單元格?

謝謝閱讀,

布賴恩

考慮:

Public Function test1(myRange As Range)
    Dim aOutput As String, entry As Range
    For Each entry In myRange
        If entry.EntireRow.Hidden = False Then
            aOutput = aOutput & entry.Value & ", "
        End If
    Next
    test1 = Left(aOutput, Len(aOutput) - 1)
End Function

當然-在函數內部以及任何可執行指令之前,聲明一個新變量myRangeVisible,如下所示:

Dim myRangeVisible as Range

並將其設置為僅包含myRange內部的可見單元格

Set myRangeVisible = myRange.SpecialCells(xlCellTypeVisible)

並將此范圍用作函數代碼中所有操作的源范圍

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM