excel 仅适用于可见单元格的特殊循环

pinkon5k  于 2023-02-25  发布在  其他
关注(0)|答案(2)|浏览(157)

我有以下问题;
对于筛选的数据,我希望应用规则A2=B2、A4=B4等
我尝试了下面的代码,但它不起作用,它只接受B2的第一个值

Sub SpecialLoop()
    Dim cl As Range, rng As Range
    x = 1
    
    Set rng = Range("A2", Range("A2").End(xlDown)).Cells.SpecialCells(xlCellTypeVisible)
    
    For Each cl In rng.SpecialCells(xlCellTypeVisible)
        cl = Range("B2", Range("B2").End(xlDown)).Cells.SpecialCells(xlCellTypeVisible)
    Next cl
End Sub
bvuwiixz

bvuwiixz1#

代替
cl = Range("B2", Range("B2").End(xlDown)).Cells.SpecialCells(xlCellTypeVisible)
试试这个:
cl.Value = cl.Offset(0, 1).Value

yiytaume

yiytaume2#

如果只是将B列复制到A列,则根本不需要循环:

Sub copy_visible_cells_from_B_to_A()
    Dim rng As Range
    Set rng = Range("A2", Range("A2").End(xlDown)).Cells.SpecialCells(xlCellTypeVisible)
    rng.Value = rng.Offset(, 1).Value 
End Sub

相关问题