Vlookup无法在循环内工作

时间:2019-06-10 19:07:28

标签: excel vba

如果该语句似乎不起作用,我尝试调试无济于事。一些见识会很棒。

ProteinmakerCount = SmallSampleCount + 2
 If ThisWorkbook.Sheets("Samples").Range("H" & (ProteinmakerCount - SmallSampleCount)) = "Protein Maker #1" Then
       'MsgBox (ThisWorkbook.Sheets("Samples").Range("H" & (ProteinmakerCount - SmallSampleCount)).Value)
        For X = 0 To 2
            For j = 1 To 6
                For k = 1 To 4
                        Y = 0
                        Sheet2.Cells((3 + k), (2 + j)).Offset((6 * X), 0).Value = Application.WorksheetFunction.VLookup(ThisWorkbook.Sheets("SSP Plate").Range("T" & k + 3).Offset(6 * X, j - 1 + 9 * Y), Range1, 2, False)
                      'MsgBox (Sheet2.Cells((3 + k), (2 + j)).Offset((6 * X), 0).Value)
                        SmallSampleCount = SmallSampleCount - 1
                    If ThisWorkbook.Sheets("Samples").Range("H" & (ProteinmakerCount - SmallSampleCount)) = "Protein Maker #2" Then
                       Exit For
                    ElseIf SmallSampleCount < 1 Then
                       Exit For
                    End If
                Next k
                If ThisWorkbook.Sheets("Samples").Range("H" & (ProteinmakerCount - SmallSampleCount)) = "Protein Maker #2" Then
                   Exit For
                ElseIf SmallSampleCount < 1 Then
                    Exit For
                End If
            Next j
            If ThisWorkbook.Sheets("Samples").Range("H" & (ProteinmakerCount - SmallSampleCount)) = "Protein Maker #2" Then
                  Exit For
            ElseIf SmallSampleCount < 1 Then
             Exit For
            End If
        Next X
ElseIf ThisWorkbook.Sheets("Samples").Range("H" & (ProteinmakerCount - SmallSampleCount)) = "Protein Maker #2" Then
       'MsgBox (ThisWorkbook.Sheets("Samples").Range("H" & (ProteinmakerCount - SmallSampleCount)).Value)
            For z = 0 To 2
                For a = 1 To 6
                    For b = 1 To 4
                        Y = 1
                        Sheet2.Cells((3 + b), (2 + a)).Offset((6 * z), 9).Value = Application.WorksheetFunction.VLookup(ThisWorkbook.Sheets("SSP Plate").Range("T" & b + 3).Offset(6 * z, a - 1 + 9 * Y), Range1, 2, False)
                      ' MsgBox (Sheet2.Cells((3 + b), (2 + a)).Offset((6 * z), 8).Value)
                        SmallSampleCount = SmallSampleCount - 1
                       'MsgBox (SmallSampleCount)
                        If ThisWorkbook.Sheets("Samples").Range("H" & (ProteinmakerCount - SmallSampleCount)) = "Protein Maker #1" Then
                            Exit For
                        ElseIf SmallSampleCount < 1 Then
                             Exit For
                        End If
                    Next b
                    If ThisWorkbook.Sheets("Samples").Range("H" & (ProteinmakerCount - SmallSampleCount)) = "Protein Maker #1" Then
                        Exit For
                    ElseIf SmallSampleCount < 1 Then
                         Exit For
                    End If
                Next a
                    If ThisWorkbook.Sheets("Samples").Range("H" & (ProteinmakerCount - SmallSampleCount)) = "Protein Maker #1" Then
                        Exit For
                    ElseIf SmallSampleCount < 1 Then
                         Exit For
                    End If
            Next z
            If SmallSampleCount < 1 Then
               Exit Sub
            End If
 Else
    End If

1 个答案:

答案 0 :(得分:1)

您的代码很难遵循-我建议使用变量来减少代码量并减少重复。

这可能无法解决您的问题,但更易于调试:

Sub test()

    Const PM1 = "Protein Maker #1"
    Const PM2 = "Protein Maker #2"
    Dim shtSamples As Worksheet, shtSSP As Worksheet, rw As Range, pmVal, otherPMVal, Y

    Set shtSamples = ThisWorkbook.Sheets("Samples")
    Set shtSSP = ThisWorkbook.Sheets("SSP Plate")

    ProteinmakerCount = SmallSampleCount + 2

    Set rw = shtSamples.Rows(ProteinmakerCount - SmallSampleCount)

    pmVal = rw.Cells(1, "H").Value


    If pmVal = PM1 Or pmVal = PM2 Then

        Y = IIf(pmVal = PM1, 0, 1)
        otherPMVal = IIf(pmVal = PM1, PM2, PM1)

        For x = 0 To 2
            For j = 1 To 6
                For k = 1 To 4

                    Sheet2.Cells((3 + k), (2 + j)).Offset((6 * x), 0).Value = _
                        Application.VLookup(shtSSP.Range("T" & k + 3).Offset(6 * x, j - 1 + 9 * Y), Range1, 2, False)

                    SmallSampleCount = SmallSampleCount - 1
                    Set rw = shtSamples.Rows(ProteinmakerCount - SmallSampleCount)
                    'I think GoTo is acceptable for exiting a nested loop...
                    If rw.Cells(1, "H") = otherPMVal Or SmallSampleCount = 0 Then GoTo done

                Next k
            Next j
        Next x
    End If

done:

End Sub