VBA For Each Loop不能与Next i一起使用

时间:2015-08-10 14:20:41

标签: vba for-loop each

Sub ISIN()
    Dim RNG As Range
    Set RNG = Range("C20").End(xlDown).Select
    Dim i As Variant
    Dim y As Variant    
    Dim t As Range    
    Set t = Range("T:T")    

    For Each i In Range("C20,C500")
        For Each y In Range("DATAS!A6,DATAS!A100")   
            If Cells(i, 1) = Cells(y, 1) Then
                t = "All Good Here"
            Else: Range("RNG") = Cells(y, 1)
            End If
        Next y
    Next i

End Sub

1 个答案:

答案 0 :(得分:0)

如果我猜对了,这就是你要找的东西:

Sub ISIN()

    Dim RNG As Range
    Dim Rg1 As Range
    Dim Rg2 As Range
    Dim t As Range
    Dim Ws As Worksheet
    Dim WsData As Worksheet

    Set Ws = ActiveWorkbook.Sheets("Sheet1")
    Set WsData = ActiveWorkbook.Sheets("DATAS")

    Set RNG = Ws.Range(Ws.Range("C20"), Ws.Range("C" & Ws.Rows.Count).End(xlUp))
    Set t = Ws.Range("T:T")

    For Each Rg1 In RNG
        For Each Rg2 In WsData.Range("A6:A100")
            If Cells(Rg1.Row, 1) = Cells(Rg2.Row, 1) Then
                Application.Intersect(t, Rg1.Row).Value = "All Good Here"
            Else
                Ws.Cells(Rg1.Row, 1) = WsData.Cells(Rg2.Row, 1)
            End If
        Next y
    Next i

    Set Ws = Nothing
    Set WsData = Nothing

    Set RNG = Nothing
    Set t = Nothing

End Sub