如何遍历指定列中的相同单元格

时间:2018-06-05 10:42:24

标签: vba excel-vba excel

我写了下面的VBA来循环遍历第2列到1002的每一列中的相同单元格。也就是说,在编码中,当i = 2时,我有范围(" B3",&# 34; B1298&#34);当i = 3时,我需要有Range(" C3"," C1298"),依此类推。怎么实现这个,拜托?谢谢。

Sub ForwardRScen()

    Application.ScreenUpdating = False

    For i = 2 To 102

        Range("ChgBP").Value = Sheets("sth").Cells(2, i).Value
        Sheets("sth").Range("B3", "B1298").Value = Range("DResults").Value

    Next i

    Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:2)

分配范围并在循环中更改它是一个很好的方法:

Public Sub TestMe()

    Dim i As Long
    For i = 2 To 12
        Dim myRange As Range
        Set myRange = Range(Cells(2, i), Cells(1298, i))
        myRange = i
    Next i

End Sub

这是你将得到的:

enter image description here