Copy and Paste within Range with selection criteria

singcbl

Well-known Member
Joined
Feb 8, 2006
Messages
518
I have been trying to look for a macro that allows me to copy and paste down a column but the catch is this macro must allows the selection of a range to paste and it must be able to skip over blank rows which is based on a reference range which is either to the right or left of the target column.
For Example if cell A2 is the cell I want to copy from, the range of cells I am reference to is D2 to D10, the macro will then paste the formula or data from cell A2 down until cell A10. If there is any blank cell in D2 to D10 it should be smart enough to skip over them.
The previous macro that I were suggested are very specific with the position where the active cell and the reference range and this limit it flexibility.
Thanks in advance of your suggestion and help.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi singcbl,

Here's one way that firstly copies the formula in cell A2 down to the last cell found in Column D and then clears any of these copied formulas (from Column A only) where the adjacent cell in Column D is blank.

HTH

Robert

Code:
Sub Macro1()

Dim lngLastRow As Long
Dim rngCell As Range

lngLastRow = Cells(Rows.Count, "D").End(xlUp).Row

Range("A2").Copy Range("A3:A" & lngLastRow)

For Each rngCell In Range("A2:A" & lngLastRow)
    
    If IsEmpty(rngCell.Offset(0, 3)) = True Then
        rngCell.ClearContents
    End If
    
Next rngCell

End Sub
 
Upvote 0
Thanks for the codes. I am not too sure if this codes have the flexibility to allow me to select the range I want to match which can either be on the right or left of the target range. I think the codes should allow for an input box to select the reference range.
 
Upvote 0
Run the following while the the cursor is on the cell with first formula (A2 in your example):

Code:
Sub Macro2()

Dim rngSelection, rngCell As Range
Dim strColActive As String
Dim lngRowFirst, lngRowLast As Long

strColActive = Mid(ActiveCell.Address, 2, _
               (InStr(2, ActiveCell.Address, "$")) - 2)

    On Error Resume Next
        Application.DisplayAlerts = False
            Set rngSelection = Application.InputBox(Prompt:= _
                "Use your mouse to select the reference range.", _
                    Title:="SPECIFY RANGE", Type:=8)
    On Error GoTo 0
        Application.DisplayAlerts = True

        If IsEmpty(rngSelection) = True Then
            Exit Sub
        Else
            For Each rngCell In rngSelection
                If lngRowFirst = 0 Then
                    lngRowFirst = rngCell.Row
                End If
            lngRowLast = rngCell.Row
            Next rngCell
        End If
        
Range(strColActive & lngRowFirst).Copy _
    Range(strColActive & lngRowFirst + 1 & ":" & strColActive & lngRowLast)
    
    For Each rngCell In Range(strColActive & lngRowFirst & ":" & strColActive & lngRowLast)
        If rngCell.Value = 0 Then
            rngCell.ClearContents
        End If
    Next rngCell

End Sub
 
Upvote 0
Thanks for the feedback and I'm glad it worked :)
 
Upvote 0
Trebo76,

Just one little nickpick I discovered if can be overcome will be great but if not I can live with what is possible now. I notice that if any of cells being referred to in the formula is empty the macro will skip the target range. That is to say if reference range cell A3 is with data but target cell C3 is suppose to have a formula that is referring to D3+E3 if either is empty, the formula will be copied to C3. Any suggestio?
 
Upvote 0
Hi singcbl,

The macro initially copies down the range selected via the input box then goes back and clears all cells that return a zero, which is what is returned if the cell a formula is referencing to is blank. Maybe you need to build something in your formula that will return a certain unique value if the cell being referenced is blank, then tweak my macro to clear all the cells that return this value.

HTH

Robert
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,535
Members
449,037
Latest member
tmmotairi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top