Range.Find()函数找不到数值 - VBA excel

时间:2017-08-16 10:22:05

标签: excel vba excel-vba

我遇到的问题是,如果Range()。Find找不到任何内容我将该值添加到" Total" sheet但是当循环从头开始时,rgFound = Range.Find()仍然没有任何东西,导致无限循环。 问题似乎是那个

Worksheets("Total").Range("A1:A20000").Find(Worksheets("July").Cells(Index, "C").Value, LookIn:=xlValues, Lookat:=xlWhole)

是否使用以下格式查找值:423423_fsd但不是数值,例如:42341。任何想法?

 Sub find()
 Dim rgFound As Range

 Dim Index As Long: Index = 6


 Do While Worksheets("July").Cells(Index, "C").Value > 0

    Set rgFound = Worksheets("Total").Range("A1:A20000").Find(Worksheets("July").Cells(Index, "C").Value, LookIn:=xlValues, Lookat:=xlWhole)


    If Not rgFound Is Nothing Then

        Row = rgFound.Row + 5


        Worksheets("Total").Range("E" & Row).Value = Worksheets("July").Range("Z" & Index).Value
        Worksheets("Total").Range("F" & Row).Value = Worksheets("July").Range("AA" & Index).Value
        Worksheets("Total").Range("G" & Row).Value = Worksheets("July").Range("AB" & Index).Value
        Worksheets("Total").Range("H" & Row).Value = Worksheets("July").Range("M" & Index).Value
        Worksheets("Total").Range("J" & Row).Value = Worksheets("July").Range("AF" & Index).Value
        Worksheets("Total").Range("I" & Row).Value = Worksheets("July").Range("AC" & Index).Value
        Worksheets("Total").Range("K" & Row).Value = Worksheets("July").Range("J" & Index).Value
        Index = Index + 1
    Else

            Dim lastRow As Long
            Dim rng As Range
            Set rng = Sheets("Total").Cells

            lastRow = Last(1, rng)

            Dim Counter As Long: Counter = 1

            Do While Counter < 13

                 lastRow = lastRow + 1
                 Worksheets("Total").Range("A" & lastRow).Value = Worksheets("July").Cells(Index, "C").Value
                 Worksheets("Total").Range("B" & lastRow).Value = Worksheets("July").Cells(Index, "B").Value

                 Counter = Counter + 1

            Loop

            Counter = 1



    End If


        Loop
End Sub

0 个答案:

没有答案