Fills in Destination with Matching Blank Cells
There's a couple ways you can do this. As @Cyril pointed out, you can either loop or filter your ranges. A loop allows you to specify how you want to treat specific cases (i.e. the cell is blank), whereas a filter (through Excel or VBA) lets you specify which cells you want prior to copying them over.
A loop in which you do not copy blank cells would look something like this:
Dim curRange as Range 'Source values
Dim destRange as Range 'Target location
Set curRange = Sheets("Source").Range("F1:G20")
Set destRange = Sheets("Destination").Range("A1:B20")
For i = 1 to curRange.Cells.Count
If Not curRange.Cells(i).Value = vbNullString Then
destRange.Cells(i).Value = curRange.Cells(i).Value
End If
Next i
The loop can be nested as appropriate if you are iterating over a large or unknown number of ranges.
If you have a default value (as opposed to vbNullString
), simply replace vbNullString
with the default value.
Fills in Destination in Continuous Manner, Jumping Over Blank Cells
After re-reading, I think you may have meant that you are trying to have no blank cells in your destination sheet, which means that you want to continue pasting from the previous cell.
Adjust the loop as so:
Dim curRange As Range 'Source values
Dim destRange As Range 'Target location
Dim i As Long
Set curRange = Sheets("Source").Range("F1:G20")
Set destRange = Sheets("Destination").Range("A1:B20")
i = 1
For Each cell In destRange.Cells
Do Until i > curRange.Cells.Count
If Not curRange.Cells(i).Value = vbNullString Then
cell.Value = curRange.Cells(i).Value
i = i + 1
Exit Do
Else
i = i + 1
End If
Loop
Next cell