比较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)如果单元格是空的,似乎添加与空单元格的行,以适当的arrays,即使我添加

 If Not IsEmpty (Cells(i, 1)) And IsEmpty(Cells(i, 6)) Then 'next i 

我知道解决scheme可能在于定义这两个数组,并使用LBound到Ubound,但我无法得到我的头。 非常感谢您的帮助!

编辑:最后一行似乎现在是固定的,但我仍然无法以某种方式跳过空白单元格和最后有“总计”文本里面的单元格,因此没有sorting不像其他的范围。 任何人有任何想法如何绕过这一点? 这就是代码现在的样子:

 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:我终于明白了! 如果find值“Grand Total”,我只是添加了另一个条件来添加行到对面的表。 无需打扰空单元格!

 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 

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

a)使用for循环时,初始条件(本例中的Lastrow)只在启动循环时才被读取,因此如果在循环中更改了Lastrow,则循环仍然只运行到原始值。

为了解决这个问题,你可以把它重组为while 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)为什么不只是评估两者,而是select两者中的较大者呢?

 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)在这里跑出来,希望别人能帮忙。