宏循环跳过相同的行

时间:2019-10-16 20:15:12

标签: excel vba excel-2010

我是Excel宏的初学者。我拼凑了这个宏,直到到达第37-209行和第307-444行为止,它似乎运行良好。我试图弄清楚为什么它跳过这些特定的行。

宏应按ID号对第一个数据块排序,然后按ID号对同一页中的第二个数据块排序。这部分运行完美。完成此操作后,假设要移动数据行以匹配ID号。生成的报告应该在同一行上具有匹配的ID号。

我尝试确保数字的格式相同,结果也相同。

第一块数据可能具有:

DUMMY DATA
"Account Number"  "First Name" "Last Name" "Current" "Etc."
==========================================================
1001            |   A         |    A      |    1     | X
1002            |   B         |    B      |    1     | X
1003            |   C         |    C      |    1     | X
1004            |   D         |    D      |    1     | X
1005            |   E         |    E      |    1     | X
1006            |   F         |    F      |    1     | X
1007            |   G         |    G      |    1     | X
1008            |   H         |    H      |    1     | X
1009            |   I         |    I      |    1     | X
1010            |   J         |    J      |    1     | X
1011            |   K         |    K      |    1     | X
1012            |   L         |    L      |    1     | X
1013            |   M         |    M      |    1     | X
1014            |   N         |    N      |    1     | X
1015            |   O         |    O      |    1     | X
1016            |   P         |    P      |    1     | X
1017            |   Q         |    Q      |    1     | X
1018            |   R         |    R      |    1     | X

第二个数据块可能具有:

DUMMY DATA
"Account Number"  "Balance"    "Principle" "Last Payment" "Etc."
===============================================================
1001            |   $1.00     |    $0.50  |    $1.00     |   X
1002            |   $1.00     |    $0.50  |    $1.00     |   X
1003            |   $1.00     |    $0.50  |    $1.00     |   X
1004            |   $1.00     |    $0.50  |    $1.00     |   X
1008            |   $1.00     |    $0.50  |    $1.00     |   X
1009            |   $1.00     |    $0.50  |    $1.00     |   X
1010            |   $1.00     |    $0.50  |    $1.00     |   X
1011            |   $1.00     |    $0.50  |    $1.00     |   X
1012            |   $1.00     |    $0.50  |    $1.00     |   X
1016            |   $1.00     |    $0.50  |    $1.00     |   X
1017            |   $1.00     |    $0.50  |    $1.00     |   X
1018            |   $1.00     |    $0.50  |    $1.00     |   X

这些宏似乎与将帐号匹配起来有问题。

Sub MANIP_TEST_Sort()

    'sorts the left most set of data in numerical order by ID
    Columns("A:H").Select
        ActiveSheet.Sort.SortFields.Clear
        ActiveSheet.Sort.SortFields.Add Key:=Range("A1"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("A:H")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    'sorts the right most set of data in numerical order by ID
    Columns("K:V").Select
        ActiveSheet.Sort.SortFields.Clear
        ActiveSheet.Sort.SortFields.Add Key:=Range("K1"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveSheet.Sort
        .SetRange Range("K:V")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    'Shifts cells to line up matching ID
    lr1 = Range("A" & Rows.Count).End(xlUp).Row
    lr2 = Range("K" & Rows.Count).End(xlUp).Row
    LR = WorksheetFunction.Min(lr1, lr2)
    i = 0

    Do While (i <= LR)
        a = Range("A2" & i)
        k = Range("K2" & i)

        If Not (a = b) Then
            If (Range("A2" & i) > Range("K2" & i)) Then
                Range("A2" & i, "H2" & i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            Else
                If (Range("A2" & i) < Range("K2" & i)) Then
                    Range("K2" & i, "V2" & i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                End If
            End If

        End If
        i = i + 1
        lr1 = Range("A" & Rows.Count).End(xlUp).Row
        lr2 = Range("K" & Rows.Count).End(xlUp).Row
        LR = WorksheetFunction.Min(lr1, lr2)
    Loop
End Sub

0 个答案:

没有答案