Pasting data into merged cells

2022

Board Regular
Joined
Jun 5, 2022
Messages
74
Office Version
  1. 2016
Platform
  1. Windows
I have some data which I need to paste into column A of a file, using code.

For simplicity, let's assume that

cells A1 & B1 are merged

cells A2 and B2 are merged and

cells A3 and B3 are merged.

And let's say we have the value 1 in cell D2 and the value 2 in cell D3.

When I try top copy cells D2 and D3 then paste values only, into the merged A2&B2 and A3&B3 using code, I get an error!

If I do it manually, and try to paste values only, I get an error saying 'to do this, all merged cells need to be the same size'

But the source of data I'm copying the cells from is not merged data.

And I just want to copy the source data (which is a list of numbers in column A in a separate tab, but, in this case is the numbers 1 and 2 in cells D2 and D3, respectively, then paste it into column A, which has been merged with column B.

The merged columns are for ease of data input in other cells in those columns.

Here's some simple code I recorded, but the recording stopped at the point where it says 'to do this, all merged cells need to be the same size'

Sub MergedCells()
'
' MergedCells Macro
''
Range("D2:D3").Select
Selection.Copy
Range("A2:B2").Select

End Sub

Any thoughts would be greatly appreciated.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Upvote 0
If not, this works

dim strr as string: strr="a2:b2"
Range("strr").Value = ActiveCell.Value & " " & ActiveCell.Offset(1, 0).Value

Make sure the activecell is on the top value that you want put in first. Also, select one cell only.
 
Upvote 0
This could be a solution but avoiding merged cells would be less painful.
VBA Code:
Option Explicit
Sub MergedCells()
    '
    ' MergedCells Macro
    '
    Range("D2").Copy Range("A2").MergeArea
    Range("D3").Copy Range("A3").MergeArea
   
End Sub
PS. Hi to all.
 
Upvote 0
Solution
If not, this works

dim strr as string: strr="a2:b2"
Range("strr").Value = ActiveCell.Value & " " & ActiveCell.Offset(1, 0).Value

Make sure the activecell is on the top value that you want put in first. Also, select one cell only.
Thanks for your input - I tried this, but it didn't work. But the solution @rollis13 posted below did.....

Thanks for responding, though.
 
Upvote 0
Upvote 0
Thanks for the positive feedback(y), glad we were able to help.
 
Upvote 0

Forum statistics

Threads
1,214,621
Messages
6,120,568
Members
448,972
Latest member
Shantanu2024

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