比较VBA中的两个数组并添加行

时间:2015-08-13 09:33:43

标签: arrays excel vba

因为我对VBA相当新,所以我需要一些代码的帮助,这应该是比较两个按升序排序的数组,如果某个数组中的值丢失,那么它就是'应该在适当的表中添加行,并将值为零的缺失值填入其旁边的单元格。这是我到目前为止所得到的:

With ActiveSheet
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

For I = 3 To LastRow
    If Cells(I, 1) > Cells(I, 6) Then
        LastRow = LastRow + 1
        With Range("A" & I & ":C" & I)
            .Select
            .Insert Shift:=xlDown
        End With
        Range("A" & I) = Cells(I, 6)
        Cells(I, 2).Value = 0
    ElseIf Cells(I, 1).Value < Cells(I, 6).Value Then
        With Range("F" & I & ":H" & I)
            .Select
            .Insert Shift:=xlDown
        End With
        Range("F" & I) = Cells(I, 1)
        Cells(I, 7).Value = 0
    Else
    End If
 Next i

此代码的问题,除了它的无效(这不是问题,因为两个数组都非常小)是LastRow: a)随每行添加的变化, b)只计算array1中的LastRow,所以如果array2更大,它就不会一直向下, c)如果单元格为空,即使添加

,它似乎也会将具有空单元格的行添加到适当的数组中
If Not IsEmpty (Cells(i, 1)) And IsEmpty(Cells(i, 6)) Then 'next i

我知道解决方案可能在于定义两个数组并将LBound用于Ubound但是我无法理解它。非常感谢您的帮助!

修改 最后一行似乎现在已经解决了,但是我仍然无法以某种方式跳过空白单元格和单元格中的#34; Grand Total&#34;内部的文本因此不像其他范围那样排序。任何人有任何想法如何绕过这个?这就是代码现在的样子:

CurrentRow = 3
Do While CurrentRow <= LastRow
    If Cells(CurrentRow, 1) > Cells(CurrentRow, 6) Then
        If Not Cells(CurrentRow, 6).Value = "Grand Total" Or IsEmpty(Cells(CurrentRow, 6).Value) Then
            With Range("A" & CurrentRow & ":C" & CurrentRow)
                .Select
                .Insert Shift:=xlDown
            End With
            Range("A" & CurrentRow) = Cells(CurrentRow, 6)
            Cells(CurrentRow, 2).Value = 0
        End If
    ElseIf Cells(CurrentRow, 6) > Cells(CurrentRow, 1) Then
        If Not Cells(CurrentRow, 1).Value = "Grand Total" Or IsEmpty(Cells(CurrentRow, 1).Value) Then
            With Range("F" & CurrentRow & ":H" & CurrentRow)
                .Select
                .Insert Shift:=xlDown
            End With
            Range("F" & CurrentRow) = Cells(CurrentRow, 1)
            Cells(CurrentRow, 7).Value = 0
        End If
    Else
    End If
    With ActiveSheet
        LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
    End With
    CurrentRow = CurrentRow + 1
    Debug.Print CurrentRow
Loop

编辑2: 我终于明白了!我刚刚添加了另一个条件,如果找到值&#34; Grand Total&#34;则将行添加到对面的表中。不需要打扰空单元!

CurrentRow = 3
Do While CurrentRow <= LastRow
    If Cells(CurrentRow, 1) > Cells(CurrentRow, 6) Then
        If Cells(CurrentRow, 6).Value = "Grand Total" Then
            With Range("F" & CurrentRow & ":H" & CurrentRow)
                .Select
                .Insert Shift:=xlDown
            End With
            Range("F" & CurrentRow) = Cells(CurrentRow, 1)
            Cells(CurrentRow, 7).Value = 0
        Else
            With Range("A" & CurrentRow & ":C" & CurrentRow)
                .Select
                .Insert Shift:=xlDown
            End With
            Range("A" & CurrentRow) = Cells(CurrentRow, 6)
            Cells(CurrentRow, 2).Value = 0
        End If
    ElseIf Cells(CurrentRow, 6) > Cells(CurrentRow, 1) Then
        If Cells(CurrentRow, 1).Value = "Grand Total" Then
            With Range("A" & CurrentRow & ":C" & CurrentRow)
                .Select
                .Insert Shift:=xlDown
            End With
            Range("A" & CurrentRow) = Cells(CurrentRow, 6)
            Cells(CurrentRow, 2).Value = 0
        Else
            With Range("F" & CurrentRow & ":H" & CurrentRow)
                .Select
                .Insert Shift:=xlDown
            End With
            Range("F" & CurrentRow) = Cells(CurrentRow, 1)
            Cells(CurrentRow, 7).Value = 0
        End If
    Else
    End If
    With ActiveSheet
        LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
    End With
    CurrentRow = CurrentRow + 1
    Debug.Print CurrentRow
Loop

1 个答案:

答案 0 :(得分:1)

改变的一个很好的整洁问题!以下是我对前两部分的看法:

a)使用for循环,初始条件(在这种情况下为Lastrow)仅在启动循环时读取,因此如果在循环期间更改Lastrow,则循环仍将仅运行到原始值。

为了解决这个问题,你可以将它重组为do while循环。使用通用示例:

Sub loop_for()

    Dim intLoop As Integer
    Dim intEnd As Integer

    intEnd = 3

    For intLoop = 1 To intEnd 'this is fixed as soon as is triggered
        Debug.Print intLoop
        If intLoop = 2 Then intEnd = 4 'this has no effect on loop
    Next intLoop

'output is 1,2,3
End Sub

VS

Sub loop_while()

    Dim intLoop As Integer
    Dim intEnd As Integer

    intLoop = 1
    intEnd = 3

    Do While intLoop <= intEnd

        Debug.Print intLoop
        intLoop = intLoop + 1

        If intLoop = 2 Then intEnd = 4

    Loop

'output is 1,2,3,4
End Sub

b)为什么不仅仅评估两者并选择两者中的较大者?

Sub lastrow()

    Dim lastrow As Long
    Dim lastrow1 As Long
    Dim lastrow2 As Long

    lastrow1 = ActiveSheet.Cells(.Rows.Count, "A").End(xlUp).Row
    lastrow2 = ActiveSheet.Cells(.Rows.Count, "F").End(xlUp).Row
    lastrow = Application.Max(lastrow1, lastrow2)

End Sub
c)在这里失去了动力,希望其他人可以提供帮助。