Arrays 在VBA中比较两个数组并添加行
由于我是VBA新手,我需要一部分代码的帮助,这是为了比较两个按升序排序的数组,如果其中任何一个数组中缺少一个值,那么它应该将行添加到相应的表中,并在其旁边的单元格中使用值0填充缺少的值。到目前为止,我得到的是:Arrays 在VBA中比较两个数组并添加行,arrays,excel,vba,Arrays,Excel,Vba,由于我是VBA新手,我需要一部分代码的帮助,这是为了比较两个按升序排序的数组,如果其中任何一个数组中缺少一个值,那么它应该将行添加到相应的表中,并在其旁边的单元格中使用值0填充缺少的值。到目前为止,我得到的是: 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
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
我知道解决方案可能在于定义两个数组并使用LBound进行Ubound,但是我无法理解它。非常感谢你的帮助
编辑:
最后一行的事情现在似乎已经解决了,但是我仍然无法跳过空白单元格和最后一行中包含“总计”文本的单元格,因此不像其他单元格那样排序。有人知道如何绕过这个吗?这就是代码现在的样子:
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
CurrentRow=3
当CurrentRow单元格(CurrentRow,6)时执行此操作,然后
如果不是单元格(CurrentRow,6).Value=“总计”或IsEmpty(单元格(CurrentRow,6).Value),则
带范围(“A”&CurrentRow&“:C”&CurrentRow)
.选择
.插入移位:=xlDown
以
范围(“A”和CurrentRow)=单元格(CurrentRow,6)
单元格(CurrentRow,2)。值=0
如果结束
ElseIf Cells(CurrentRow,6)>Cells(CurrentRow,1)然后
如果不是单元格(CurrentRow,1).Value=“总计”或IsEmpty(单元格(CurrentRow,1).Value),则
带范围(“F”&CurrentRow&“:H”&CurrentRow)
.选择
.插入移位:=xlDown
以
范围(“F”和CurrentRow)=单元格(CurrentRow,1)
单元格(当前行,7)。值=0
如果结束
其他的
如果结束
使用ActiveSheet
LastRow=.UsedRange.Rows(.UsedRange.Rows.Count).Row
以
CurrentRow=CurrentRow+1
调试。打印当前行
环
编辑2:
我终于明白了!我刚刚添加了另一个条件,若它找到值“grandtotal”,那个么将行添加到相反的表中。不必为空电池烦恼
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
CurrentRow=3
当CurrentRow单元格(CurrentRow,6)时执行此操作,然后
如果单元格(CurrentRow,6).Value=“总计”,则
带范围(“F”&CurrentRow&“:H”&CurrentRow)
.选择
.插入移位:=xlDown
以
范围(“F”和CurrentRow)=单元格(CurrentRow,1)
单元格(当前行,7)。值=0
其他的
带范围(“A”&CurrentRow&“:C”&CurrentRow)
.选择
.插入移位:=xlDown
以
范围(“A”和CurrentRow)=单元格(CurrentRow,6)
单元格(CurrentRow,2)。值=0
如果结束
ElseIf Cells(CurrentRow,6)>Cells(CurrentRow,1)然后
如果单元格(CurrentRow,1).Value=“总计”,则
带范围(“A”&CurrentRow&“:C”&CurrentRow)
.选择
.插入移位:=xlDown
以
范围(“A”和CurrentRow)=单元格(CurrentRow,6)
单元格(CurrentRow,2)。值=0
其他的
带范围(“F”&CurrentRow&“:H”&CurrentRow)
.选择
.插入移位:=xlDown
以
范围(“F”和CurrentRow)=单元格(CurrentRow,1)
单元格(当前行,7)。值=0
如果结束
其他的
如果结束
使用ActiveSheet
LastRow=.UsedRange.Rows(.UsedRange.Rows.Count).Row
以
CurrentRow=CurrentRow+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
c) 这里没有动力了,希望其他人能帮上忙。而数组,你指的是excel范围?因为我在Lastrow的代码中找不到任何VBA数组,所以您可以使用另一种方法:可以找到不同Lastrow尝试的摘要我建议在一次测试之后再尝试使用的范围iteration@EngJon是的,我指的是excel范围,很抱歉给您带来了混乱。也谢谢你的回答。我去看看。谢谢,你的评论很有帮助。我已经采纳了您的建议,并将For…Next循环替换为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
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
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