-1

我有一个 Excel 工作表,在该工作表上创建了一个包含许多值的列表。我还创建了一个宏,它显示了一个用户表单,其中这些值是硬编码的。

现在我希望表单中的这些值自动/以编程方式/动态添加到我的用户表单列表中,这样将来,如果我想减少列表中的值,那么我不必再次更改宏。

我一直在寻找答案,但一直未能找到我想要的东西。

我已经记录了这个宏,但我不知道如何从中检索值:

Sub Macro7()
'
' Macro7 Macro
'

'
Range("E1").Select
ActiveSheet.Range("$A$1:$AE$175").AutoFilter Field:=5
End Sub
4

2 回答 2

0

您指定的宏将为您的活动工作表打开自动筛选。这将提供允许用户过滤到感兴趣的内容的列标题。假设这种工作表的过滤是你想要的,你可以使用类似的东西:

Dim r As Range
'Note: set r to something useful, such as worksheet.Cells

Dim vis As Range
Set vis = r.SpecialCells(xlCellTypeVisible)

'now vis holds a special "Range" object referring to the visible cells.
'since (auto) filtering hides some cells, this vis range will help show only the cells that remain visible.
'the output of SpecialCells, you should assume holds a complex Range,
'which is composed of multiple Areas that are wrapped in one single Range object
'the separate areas help you distinguish the visible cells from the hidden cells
'fyi, various safety checks you can do: vis Is Range, vis Is Nothing

Dim a as Areas
Set a = r.Areas

Dim cr as Range
For Each cr in a
    'cr refers to a single (i.e. normal and contiguous) area range
    'where you can use cr.Row, cr.Column, cr.Rows.Count, cr.Columns.Count
Next

因此,当您进行过滤时,您可以使用 SpecialCells(xlCellTypeVisible) 来显示非隐藏单元格,这些单元格表示为具有包含表示连续范围的区域的范围。

于 2012-07-31T04:50:45.197 回答
0

使用一个名为 UReports 的用户表单,它有一个名为 lbxReport 的列表框,使用这样的代码用 E 列中的值填充列表框

Sub ShowUf()

    Dim ufReports As UReports
    Dim rCell As Range
    Dim colUnique As Collection
    Dim i As Long

    Set ufReports = New UReports
    Set colUnique = New Collection

    'loop through the cells in column E
    For Each rCell In Sheet1.Range("E2", Sheet1.Cells(Sheet1.Rows.Count, 5).End(xlUp)).Cells
        'Collections can't have duplicate keys, so we try to add all the values.  If there
        'are duplicates, the 'On Error' ignores them and we're left with a collection of
        'only unique values from column E
        On Error Resume Next
            colUnique.Add rCell.Value, CStr(rCell.Value)
        On Error GoTo 0
    Next rCell

    'loop through the collection and add them to the listbox
    For i = 1 To colUnique.Count
        ufReports.lbxReport.AddItem colUnique.Item(i)
    Next i

    'Show the form
    ufReports.Show

End Sub
于 2012-07-31T14:11:13.500 回答