VBA code: Copy Active Cell and Paste 2 cells/rows directly below

mickyflash

Board Regular
Joined
Jan 29, 2009
Messages
77
Hello All

I need a VBA code that copies the active cell, and pastes values 2 cells directly below the active cell. (skips a row). so if the active cell is A1, the target cell would be A3
This code works, until we hit cells that have been merged. The merged cells will always be the same size, and I am only pasting values but cant get it working without breaking the merged cell.

Does anyone have any ideas on a fix for this please?

VBA Code:
Sub CopyAndPasteTwoCellsBelow()
    ' Copy the value from the active cell
    ActiveCell.Copy
    
    ' Move two cells down
    Dim targetCell As Range
    Set targetCell = ActiveCell.Offset(2, 0)
    
    ' Check if the destination cell is not empty
    If Not IsEmpty(targetCell.Value) Then
        ' Ask the user whether to overwrite the existing value
        Dim overwrite As VbMsgBoxResult
        overwrite = MsgBox("The destination cell is not empty. Do you want to overwrite?", vbYesNo + vbQuestion, "Overwrite Warning")
        
        ' Check the user's response
        If overwrite = vbNo Then
            Exit Sub ' Exit the sub if the user chooses not to overwrite
        End If
    End If
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Are the Merged cells always the same number of cells Merged together?
 
Upvote 0
Actually, I just tested your code and it's working fine for me. At what line is your code breaking?

Edit: You must be on the trailing end of the Merged cells. Then Post #3 is still correct.
 
Upvote 0
Try this.
VBA Code:
Sub CopyAndPasteTwoCellsBelow()
    ' Copy the value from the active cell
    'ActiveCell.Copy
    
    ' Move two cells down
    Dim targetCell As Range
    Set targetCell = MergeAddress(ActiveCell)
    
    ' Check if the destination cell is not empty
    If Not IsEmpty(targetCell.Value) Then
        ' Ask the user whether to overwrite the existing value
        Dim overwrite As VbMsgBoxResult
        overwrite = MsgBox("The destination cell is not empty. Do you want to overwrite?", vbYesNo + vbQuestion, "Overwrite Warning")
        
        ' Check the user's response
        If overwrite = vbNo Then
            Exit Sub ' Exit the sub if the user chooses not to overwrite
        End If
    End If
targetCell.Value = ActiveCell.Value
End Sub
Function MergeAddress(ByVal Target As Range)
Dim targetCell As Range, tAdd
Set targetCell = Target.Offset(2, 0)
If targetCell.MergeCells = True Then
    tAdd = Left(targetCell.MergeArea.Address, InStr(1, targetCell.MergeArea.Address, ":") - 1)
End If
Set MergeAddress = ActiveSheet.Range(tAdd)
End Function
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,976
Members
449,095
Latest member
Mr Hughes

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