Excel VBA变量未正确递增

时间:2018-09-27 18:02:24

标签: excel vba excel-vba

我具有下面的Excel 2016 VBA函数,可在其中将数据输出到各种工作表。我的“错误”工作表上的数据似乎有问题,在其他行与数据之间我得到很多空白行。

仅在“错误”工作表中出现此问题。我尝试将k = k + 1变量放在一些周围,但没有帮助。我应该(应该不应该)增加k的明显位置来解决问题吗?还是需要更改lj

Function PPDdate()

    Dim PPD_1_Date As Date
    Dim PPD_2_Date As Date
    Dim TSpot_Date As Variant
    Dim i As Long, j As Long, k As Long

    j = Worksheets("PPDCI").Range("A" & Rows.Count).End(xlUp).Row + 1
    l = Worksheets("CI").Range("A" & Rows.Count).End(xlUp).Row + 1
    k = Worksheets("Error").Range("A" & Rows.Count).End(xlUp).Row + 1

    For i = 2 To lstrow

        PPD_1_Date = Worksheets("Data").Range("AW" & i)
        PPD_2_Date = Worksheets("Data").Range("BA" & i)
        Entity = Worksheets("Data").Range("J" & i)
        Dept = Worksheets("Data").Range("M" & i)
        TSpot_Date = Worksheets("Data").Range("AS" & i)


        If PPD_1_Date > PPD_2_Date Then
            Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
            Worksheets("PPDCI").Range("F" & j).Value = PPD_1_Date
            Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("AX" & i).Value
            Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("AZ" & i).Value
            Worksheets("PPDCI").Range("I" & j).Value = Worksheets("Data").Range("AY" & i).Value
            Worksheets("PPDCI").Range("J" & j).Value = "1st IF STATEMENT"
            j = j + 1
        Else
            If PPD_1_Date < PPD_2_Date Then
                Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
                Worksheets("PPDCI").Range("F" & j).Value = PPD_2_Date
                'Worksheets("PPDCI").Range("G" & j).Value = "ELSE IF CONDITION"
                Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("BB" & i).Value
                Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("BD" & i).Value
                Worksheets("PPDCI").Range("I" & j).Value = Worksheets("Data").Range("BC" & i).Value
                Worksheets("PPDCI").Range("J" & j).Value = "2nd IF STATEMENT"
                j = j + 1

                'If IsEmpty(Worksheets("Data").Range(PPD_1_Date & i).Value) = True And IsEmpty(Worksheets("Data").Range(PPD_2_Date & i).Value) = True Then
                'GoTo EmptyRange
                'Else

            Else
                If Len(TSpot_Date) <> 0 And InStr(1, UCase(Worksheets("Data").Range("AT" & i).Value), "NEG") > 0 Then
                    Worksheets("CI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
                    Worksheets("CI").Range("D" & j).Value = "TSNG"
                    Worksheets("CI").Range("E" & j).Value = TSpot_Date
                    Worksheets("CI").Range("F" & j).Value = "3rd IF STATEMENT"
                    'j = j + 1

                Else
                    If Len(TSpot_Date) <> 0 And InStr(1, UCase(Worksheets("Data").Range("AT" & i).Value), "POS") > 0 Then
                        Worksheets("CI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
                        Worksheets("CI").Range("D" & j).Value = "TSPS"
                        Worksheets("CI").Range("E" & j).Value = TSpot_Date
                        Worksheets("CI").Range("F" & j).Value = "4th IF STATEMENT"
                        l = l + 1

                    Else
                        If (InStr(1, Entity, "CNG Hospital") > 0 Or InStr(1, Entity, "Home Health") > 0 Or InStr(1, Entity, "Hospice") > 0 Or InStr(1, Dept, "Volunteers") > 0 And (PPD_1_Date = 0 And PPD_2_Date = 0)) And TSpot_Date = 0 Then
                            Worksheets("Error").Range("A" & k & ":H" & k).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
                            Worksheets("Error").Range("F" & k).Value = "REVIEW PPD DATA"
                            Worksheets("Error").Range("G" & k).Value = "5th IF STATEMENT"
                            k = k + 1

                        Else
                            Worksheets("Error").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
                            Worksheets("Error").Range("D" & j).Value = TSpot_Date
                            Worksheets("Error").Range("E" & j).Value = Worksheets("Data").Range("AT" & i).Value
                            Worksheets("Error").Range("F" & j).Value = "REVIEW PPD/TSPOT DATA"
                            Worksheets("Error").Range("G" & j).Value = "6th IF STATEMENT"
                            'k = k + 1

                        End If

                    End If

                End If

            End If

        End If

'EmptyRange:
        'k = k + 1

    Next i

End Function

示例输出:

enter image description here

谢谢!

4 个答案:

答案 0 :(得分:3)

您正在将打印品的else语句中的范围设置为J时为ERROR表,范围应为K

                Else
                    If (InStr(1, Entity, "CNG Hospital") > 0 Or InStr(1, Entity, "Home Health") > 0 Or InStr(1, Entity, "Hospice") > 0 Or InStr(1, Dept, "Volunteers") > 0 And (PPD_1_Date = 0 And PPD_2_Date = 0)) And TSpot_Date = 0 Then
                        Worksheets("Error").Range("A" & k & ":H" & k).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
                        Worksheets("Error").Range("F" & k).Value = "REVIEW PPD DATA"
                        Worksheets("Error").Range("G" & k).Value = "5th IF STATEMENT"
                        k = k + 1

                    Else
                        Worksheets("Error").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
                        Worksheets("Error").Range("D" & j).Value = TSpot_Date
                        Worksheets("Error").Range("E" & j).Value = Worksheets("Data").Range("AT" & i).Value
                        Worksheets("Error").Range("F" & j).Value = "REVIEW PPD/TSPOT DATA"
                        Worksheets("Error").Range("G" & j).Value = "6th IF STATEMENT"
                        k = k + 1

                    End If

应该是

            Else
                If (InStr(1, Entity, "CNG Hospital") > 0 Or InStr(1, Entity, "Home Health") > 0 Or InStr(1, Entity, "Hospice") > 0 Or InStr(1, Dept, "Volunteers") > 0 And (PPD_1_Date = 0 And PPD_2_Date = 0)) And TSpot_Date = 0 Then
                    Worksheets("Error").Range("A" & k & ":H" & k).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
                    Worksheets("Error").Range("F" & k).Value = "REVIEW PPD DATA"
                    Worksheets("Error").Range("G" & k).Value = "5th IF STATEMENT"
                    k = k + 1

                Else
                    Worksheets("Error").Range("A" & k & ":C" & k).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
                    Worksheets("Error").Range("D" & k).Value = TSpot_Date
                    Worksheets("Error").Range("E" & k).Value = Worksheets("Data").Range("AT" & i).Value
                    Worksheets("Error").Range("F" & k).Value = "REVIEW PPD/TSPOT DATA"
                    Worksheets("Error").Range("G" & k).Value = "6th IF STATEMENT"
                    k = k + 1
                End If

我认为可以在打印给CI的if语句中找到相同的问题。

此外,正如评论之一所述,您如何设置最后一行?

您应该考虑声明您的工作表,这样才能使这个IMO更干净

答案 1 :(得分:1)

如其他答案所述,& j应该是& k。但这不是“根本问题”。

“根本问题”是不良的变量命名。 花点时间正确命名事物是值得的。

  • i重命名为currentRowData
  • j重命名为currentRowPPDCI
  • k重命名为currentRowError
  • l重命名为currentRowCI

写作代码占工作的5%。 阅读代码是另外95%。因此,应将代码编写为读取,而不只是执行。是的,使用有意义的标识符会花费-哦,还要输入整整 second 的时间。但是它们也使代码更容易 read ,这使得此类错误更容易捕获。

在查看琐碎的i循环时,

For...Next是很好的标识符。还有其他应有的真实姓名。

l可以说是最可怕的单个字母标识符,因为它一眼就很容易被误认为是1

答案 2 :(得分:0)

在我看来,您最后的“ else”子句使用的是“ PPDCI”工作表的最后一行而不是“错误”工作表,即变量j代替了k。

答案 3 :(得分:0)

与工作表(jk)相关的行计数器(lPPDCICI)的使用与您不一致和Error

尝试:

Function PPDdate()

    Dim PPD_1_Date As Date
    Dim PPD_2_Date As Date
    Dim TSpot_Date As Variant
    Dim i As Long, j As Long, k As Long

    j = Worksheets("PPDCI").Range("A" & Rows.Count).End(xlUp).Row + 1
    l = Worksheets("CI").Range("A" & Rows.Count).End(xlUp).Row + 1
    k = Worksheets("Error").Range("A" & Rows.Count).End(xlUp).Row + 1

    For i = 2 To lstrow

        PPD_1_Date = Worksheets("Data").Range("AW" & i)
        PPD_2_Date = Worksheets("Data").Range("BA" & i)
        Entity = Worksheets("Data").Range("J" & i)
        Dept = Worksheets("Data").Range("M" & i)
        TSpot_Date = Worksheets("Data").Range("AS" & i)


        If PPD_1_Date > PPD_2_Date Then
            Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
            Worksheets("PPDCI").Range("F" & j).Value = PPD_1_Date
            Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("AX" & i).Value
            Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("AZ" & i).Value
            Worksheets("PPDCI").Range("I" & j).Value = Worksheets("Data").Range("AY" & i).Value
            Worksheets("PPDCI").Range("J" & j).Value = "1st IF STATEMENT"
            j = j + 1
        Else
            If PPD_1_Date < PPD_2_Date Then
                Worksheets("PPDCI").Range("A" & j & ":C" & j).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
                Worksheets("PPDCI").Range("F" & j).Value = PPD_2_Date
                'Worksheets("PPDCI").Range("G" & j).Value = "ELSE IF CONDITION"
                Worksheets("PPDCI").Range("G" & j).Value = Worksheets("Data").Range("BB" & i).Value
                Worksheets("PPDCI").Range("H" & j).Value = Worksheets("Data").Range("BD" & i).Value
                Worksheets("PPDCI").Range("I" & j).Value = Worksheets("Data").Range("BC" & i).Value
                Worksheets("PPDCI").Range("J" & j).Value = "2nd IF STATEMENT"
                j = j + 1
            Else
                If Len(TSpot_Date) <> 0 And InStr(1, UCase(Worksheets("Data").Range("AT" & i).Value), "NEG") > 0 Then
                    Worksheets("CI").Range("A" & l & ":C" & l).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
                    Worksheets("CI").Range("D" & l).Value = "TSNG"
                    Worksheets("CI").Range("E" & l).Value = TSpot_Date
                    Worksheets("CI").Range("F" & l).Value = "3rd IF STATEMENT"
                    l = l + 1
                Else
                    If Len(TSpot_Date) <> 0 And InStr(1, UCase(Worksheets("Data").Range("AT" & i).Value), "POS") > 0 Then
                        Worksheets("CI").Range("A" & l & ":C" & l).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
                        Worksheets("CI").Range("D" & l).Value = "TSPS"
                        Worksheets("CI").Range("E" & l).Value = TSpot_Date
                        Worksheets("CI").Range("F" & l).Value = "4th IF STATEMENT"
                        l = l + 1
                    Else
                        If (InStr(1, Entity, "CNG Hospital") > 0 Or InStr(1, Entity, "Home Health") > 0 Or InStr(1, Entity, "Hospice") > 0 Or InStr(1, Dept, "Volunteers") > 0 And (PPD_1_Date = 0 And PPD_2_Date = 0)) And TSpot_Date = 0 Then
                            Worksheets("Error").Range("A" & k & ":H" & k).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
                            Worksheets("Error").Range("F" & k).Value = "REVIEW PPD DATA"
                            Worksheets("Error").Range("G" & k).Value = "5th IF STATEMENT"
                            k = k + 1
                        Else
                            Worksheets("Error").Range("A" & k & ":C" & k).Value = Worksheets("Data").Range("A" & i & ":C" & i).Value
                            Worksheets("Error").Range("D" & k).Value = TSpot_Date
                            Worksheets("Error").Range("E" & k).Value = Worksheets("Data").Range("AT" & i).Value
                            Worksheets("Error").Range("F" & k).Value = "REVIEW PPD/TSPOT DATA"
                            Worksheets("Error").Range("G" & k).Value = "6th IF STATEMENT"

                        End If
                    End If
                End If
            End If
        End If
    Next i

End Function