since I'm fairly new to VBA I need a help with part of my code, that is supposed to compare two arrays that are sorted ascendingly and if a value in either of the arrays is missing then it's supposed to add row to appropriate table and fill in missing value with value zero to the cell next to it. This is what I've got so far:
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
The issue with this code, besides it's ineffectiveness (which is not the issue since both arrays are very small) is that LastRow: a) changes with each row that is added, b) only counts LastRow in array1 so if array2 is bigger it doesn't go all the way down, c) if cell is empty it seems to add row with empty cell to appropriate array even if I add
If Not IsEmpty (Cells(i, 1)) And IsEmpty(Cells(i, 6)) Then 'next i
I know the solution is probably lies in defining both arrays and using LBound to Ubound however I couldn't get my head around it. Thanks a lot for help!
EDIT: The last row thing seems to be fixed now, however I'm still unable to somehow skip blank cells and cell in the last that has "Grand Total" text inside and thus is not sorted unlike rest of the range. Anyone has any ideas how to bypass this? This is what the code looks like now:
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
EDIT 2: I figured it out at last! I just added another condition to add row to opposite table if it finds value "Grand Total". No need to bother with empty cells!
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