I have the following code that should insert 2 rows in between each group based on the third column in 2d array. The code is working but I need some adjustments
Code: Select all
Sub MyTest()
Dim a
a = InsertEmptyRows(Range("A2").CurrentRegion)
Range("A18").Resize(UBound(a, 1), UBound(a, 2)).Value = a
End Sub
Function InsertEmptyRows(inputRange As Range)
Dim inputArray(), outputArray()
Dim numRows As Long, numCols As Long
Dim i As Long, j As Long, lastGroupEnd As Long, outputIndex As Long
Dim emptyRowCount As Long, k As Long
inputArray = inputRange.Value
numRows = UBound(inputArray, 1)
numCols = UBound(inputArray, 2)
emptyRowCount = 2
ReDim outputArray(1 To numRows + (numRows - 2) * emptyRowCount + 1, 1 To numCols)
For j = 1 To numCols
outputArray(1, j) = inputArray(1, j)
Next j
outputIndex = 1
For i = 2 To numRows
If i > 2 And i <= numRows And inputArray(i, 3) <> inputArray(i - 1, 3) Then
For k = 1 To emptyRowCount
outputIndex = outputIndex + 1
For j = 1 To numCols
outputArray(outputIndex, j) = ""
Next j
Next k
End If
outputIndex = outputIndex + 1
For j = 1 To numCols
outputArray(outputIndex, j) = inputArray(i, j)
Next j
Next i
InsertEmptyRows = outputArray
End Function