【发布时间】: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上添加换行符并运行您的代码,检查分配给您的数组的值。如果它们是您期望的值,则逐行遍历循环以查看出错的地方-否则,如果数组缺少预期的数据,则需要修改分配数组的方式。