The Code below copies from a range, looks for the right number of consecutive blank cells in the desitnation range and pastes cells. (need to retain cell/number formats)
I would like to achieve somehting similar, but the blank cells don't have to be consecutive, there just need to be enough blank cells in the destination range to accomodate the total in SomeRng, and would need to work from top of range down, as the values of SomeRng are in ascending order. Also need the range SomeRng dynamic/variable in length (but always 1 column wide) Any suggestions greatly appreciated...
By the way this board has beeen a lifeline for a VBA newbie like myself - thanks so much for your help and guidance people ....
I would like to achieve somehting similar, but the blank cells don't have to be consecutive, there just need to be enough blank cells in the destination range to accomodate the total in SomeRng, and would need to work from top of range down, as the values of SomeRng are in ascending order. Also need the range SomeRng dynamic/variable in length (but always 1 column wide) Any suggestions greatly appreciated...
Code:
Sub testme()
Dim myRng As Range
Dim myDestRng As Range
Dim myCell As Range
Dim HowMany As Long
Dim DestCell As Range
Dim SomeRng As Range
HowMany = Range("b1").Value
With ActiveSheet
Set SomeRng = .Range("a1:a4") 'something to copy
Set myDestRng = .Range("a8:a35")
Set myRng = Nothing
On Error Resume Next
Set myRng = myDestRng.Cells.SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
End With
If myRng Is Nothing Then
MsgBox "No empty cells in the Destination range: " _
& myDestRng.Address(0, 0)
Else
Set DestCell = Nothing
For Each myCell In myRng.Cells
If Application.CountA(myCell.Resize(HowMany, 1)) = 0 Then
Set DestCell = myCell
Exit For
End If
Next myCell
If DestCell Is Nothing Then
MsgBox HowMany & " consecutive empty cells not available"
Else
If Intersect(DestCell.Resize(HowMany, 1), myDestRng).Address _
<> DestCell.Resize(HowMany, 1).Address Then
MsgBox "Found a blank cell at: " & DestCell.Address(0, 0) _
& vbLf & "but not enough empty cells under it"
Else
SomeRng.Copy _
Destination:=DestCell
MsgBox "Pasted into: " _
& DestCell.Resize(HowMany, 1).Address(0, 0)
End If
End If
End If
End Sub
By the way this board has beeen a lifeline for a VBA newbie like myself - thanks so much for your help and guidance people ....