我正在尝试找出一种方法来复制和粘贴已过滤列中的值,然后粘贴到同一索引但不同的列上。看看下面它应该如何工作:
原文:
运行 VBA:
我尝试了 SpecialCells(xlCellTypeVisible) 但无法使其工作,因为我只需要复制一个特定列而不是整个工作表...任何人都可以帮助我吗?谢谢!
最佳答案
请测试下一个代码。它首先从过滤后的可见单元格范围创建一个列切片,然后处理每个区域行:
Sub testCopyFilteredRangeAToB()
Dim sh As Worksheet, rngF As Range, ar As Range, R As Range
Set sh = ActiveSheet
On Error Resume Next 'to avoid an error in case of no any visible cells
Set rngF = sh.UsedRange.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rngF Is Nothing Then Exit Sub 'if not range created, exit the code
Set rngF = Intersect(sh.UsedRange.SpecialCells(xlCellTypeVisible), sh.Range("A:B"))
For Each ar In rngF.Areas 'iterate between the range areas
For Each R In ar.rows 'iterate between the area rows
R.cells(1, 2).value = R.cells(1, 1).value 'copy the values, without using clipboard
Next R
Next ar
End Sub
关于excel - 将过滤列复制到另一列但索引相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71470881/