【问题标题】:Getting values from filtered excel table从过滤后的 Excel 表中获取值
【发布时间】:2020-09-04 08:05:02
【问题描述】:

我目前正在尝试将所有过滤值从一个 Excel 工作表复制到另一个工作表。但它不会复制所有可见/过滤的值。这是我表的屏幕截图(我粘贴值的另一个表是空的):

例如:当我为值“1/3”过滤“Ordnernummer”列时,它只复制以下值:

它应该是这样的:

这是我目前的代码:

    Sub getCellRangeValues()

    'declare Variant array to hold cell range values
    Dim myValuesArray() As Variant

    'declare Long to hold the last cell with data
    Dim LastRow As Long

    'declare variables to hold loop counters used to iterate through the individual values in the cell range
    Dim rowCounter As Long
    Dim columnCounter As Long

    With Worksheets("Konfiguration") 'worksheet with the data

    'get the last row from column a
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    'get values from all filtered rows and assign them to an array
    myValuesArray = .Range("C2:I" & LastRow).SpecialCells(xlCellTypeVisible).Value
    End With

    ActiveWorkbook.Worksheets("GoLabel").Activate
    'loop through each value in array (rows)
    For rowCounter = LBound(myValuesArray, 1) To UBound(myValuesArray, 1)

        'loop through each value in array (columns)
        For columnCounter = LBound(myValuesArray, 2) To UBound(myValuesArray, 2)
            Call setData(myValuesArray, rowCounter, columnCounter)
        Next columnCounter

    Next rowCounter
End Sub
Sub setData(myValuesArray() As Variant, rowCounter As Long, columnCounter As Long)
    With Worksheets("GoLabel")
    .Cells(rowCounter, columnCounter).Select
    ActiveCell.Value = myValuesArray(rowCounter, columnCounter)
    End With
End Sub

【问题讨论】:

  • 所以你过滤到 show 1/3 并复制 1/1?
  • 对不起,我添加了错误的图片。它应该从 1/3 复制所有值,但它只复制前 2 个。当我从不同列过滤其他值时,情况相同。
  • 手动设置过滤器时:是否过滤了所有数据?通常,它会在找到空行时停止
  • 当我手动使用过滤器时,它工作得非常好。它甚至按预期显示它们。 VBA 只是不保护所有值。
  • End With 上添加换行符并运行您的代码,检查分配给您的数组的值。如果它们是您期望的值,则逐行遍历循环以查看出错的地方-否则,如果数组缺少预期的数据,则需要修改分配数组的方式。

标签: excel vba filtering


【解决方案1】:

我找到了解决方法。我没有将值保存在数组中,而是简单地复制值并将它们粘贴到我的新工作表中。这里对应的代码:

    'worksheet with the data
    With Worksheets("Konfiguration")

    'get the desired column range which is declared in the cell
    startRange = "C1"
    endRange = "I101"

    'select values from all filtered rows
    .Range(startRange & ":" & endRange).SpecialCells(xlCellTypeVisible).Select
    Selection.Copy
    End With

    'worksheet which I fill with the copied data
    ActiveWorkbook.Worksheets("GoLabel").Activate

    With Worksheets("GoLabel")
        .Range("A1").Select
        Selection.PasteSpecial Paste:=xlPasteValues
    End With
    Application.CutCopyMode = False
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 2016-11-09
    相关资源
    最近更新 更多