excel - 将过滤列复制到另一列但索引相同

标签 excel vba

我正在尝试找出一种方法来复制和粘贴已过滤列中的值,然后粘贴到同一索引但不同的列上。看看下面它应该如何工作:

原文:

<表类=“s-表”> <标题> <第>行 A 列 B 列 <正文> 1 abcd 4 acdf 9 gfac

运行 VBA:

<表类=“s-表”> <标题> <第>行 A 列 B 列 <正文> 1 abcd abcd 4 acdf acdf 9 gfac gfac

我尝试了 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/

相关文章:

excel - 单元格中包含破折号但编辑栏中不包含破折号的文本 (Excel)

vba - 查找某个值的行位置并使用它在 VBA 中进行计算

Excel:如何计算多个条件的和积?

vba - 强制 VBA 使用英国本地化

javascript - 如何使用javascript存储Excel文件

sql-server - 无法在 Excel 中使用传递查询

VBA 和事件目录 : Windows group visible in manual search dialog but VBA script throws error

excel - 根据部分文件名打开多个 Excel 文件

excel - VBA - 避免循环

excel - 一一添加自动过滤条件