对于For Each循环内的每个循环

时间:2015-08-19 11:01:30

标签: excel excel-vba for-loop foreach runtime-error vba

我有2张,所以我想loop两个范围内的供应商(它们应该始终匹配,但是ccc-name后面会添加一些额外的文字,因此replace

我的问题是,我显然无法同时运行cc和ccc,但我需要比较cc和ccc。我怎样才能做出类似的事情?

    For Each cc In rngMyRange
        For Each ccc In Worksheets(10).Range("F3:F" & lastrow2).Cells

            Dim ContactID As String, ContactID2 As String
            ContactID = Sheets(10).Range("F" & ccc.Row).Value
            ContactID = Replace(ContactID, ContactID, cc)

            Debug.Print ContactID

            If cc Like ContactID Then
                If Sheets(1).Range("M" & cc.Row).Value Like "AN*" Then
                    Sheets(1).Range("M" & cc.Row).Copy Destination:=Sheets(10).Range("G" & ccc.Row)
                End If
            End If

        Next ccc
    Next cc

1 个答案:

答案 0 :(得分:0)

我最终做的略有不同:)

     For Each cc In rngMyRange
        For Each ccc In Worksheets(10).Range("F3:F" & lastrow2).Cells

            Dim FoundCell As Range
            Dim ContactID As String

                ContactID = Sheets(10).Range("F" & ccc.Row).Value

                If ContactID Like cc & "*" Then
                    ContactID = Replace(ContactID, ContactID, cc)
                End If

            Set FoundCell = Sheets(10).Columns(6).Find(what:=ContactID & "*", _
                                                             LookIn:=xlValues, _
                                                             LookAt:=xlWhole)
            If Not FoundCell Is Nothing Then
                If cc Like ContactID Then
                    If Sheets(1).Range("M" & cc.Row).Value Like "AN*" Then
                        Sheets(1).Range("M" & cc.Row).Copy Destination:=Sheets(10).Range("G" & ccc.Row)
                    Else
                        ' Do nothing
                    End If
                End If
            Else
                ' Not found
            End If
        Next ccc
    Next cc