处理vba中的可见细胞

时间:2015-04-27 18:02:55

标签: excel vba excel-vba

我有一张名为'Abschluss'的工作表。在这个工作表中,我使用filter来获取我想用vba脚本处理的数据范围。所以我只想处理可见的行。 我的vba脚本看起来像

For Each i In Worksheets("Abschluss").SpecialCells(xlCellTypeVisible).Rows
    If (WorksheetFunction.CountIf(Range("B2:B" & i), Cells(i, 2)) = 1) Then _
        Umsetzung_Kapitel_1.AddItem Cells(i, 2)
Next

这不起作用,我得到运行时错误438.你知道问题在哪里吗? 我假设Worksheets("Abschluss").SpecialCells(xlCellTypeVisible).Rows返回错误的数据类型,但我无法修复它。

2 个答案:

答案 0 :(得分:2)

看起来您正在尝试使用可能在UserForm中的唯一项目填充组合框或列表框?

试试这个

Private Sub UserForm_Initialize()
    Dim cUnique As Collection
    Dim Rng As Range
    Dim Cell As Range
    Dim sh As Worksheet
    Dim vNum As Variant

    Set sh = ThisWorkbook.Sheets("Abschluss")
    Rws = sh.Cells(Rows.Count, "B").End(xlUp).Row
    Set Rng = sh.Range(sh.Cells(2, 2), sh.Cells(Rws, 2)).SpecialCells(xlCellTypeVisible)

    Set cUnique = New Collection

    On Error Resume Next
    For Each Cell In Rng.Cells
        cUnique.Add Cell.Value, CStr(Cell.Value)
    Next Cell
    On Error GoTo 0

    For Each vNum In cUnique
        ComboBox1.AddItem vNum
    Next vNum


End Sub

答案 1 :(得分:0)

更改为

Dim i As Long
Dim colCount As Long: colCount = Worksheets("Abschluss").SpecialCells(xlCellTypeVisible).Rows.Count
For i = 1 to colCount
....