Maxwell
Maxwell

Reputation: 109

VBA Code to copy and paste active column with merged cells inside

I'm trying to copy the active column and paste it next to it but the code selects the entire worksheet because it has merged cells in.

Sub CopyPaste()

Columns(ActiveCell.Column).Selection
Selection.Copy
ActiveCell.Offset(0,1).PasteSpecial Paste:=xlPasteAll

End Sub 

Could you please help me adding the missing code to ignore merged cells?

Upvotes: 1

Views: 2602

Answers (1)

A.S.H
A.S.H

Reputation: 29352

This is yet another reason to avoid using Select in VBA for Excel. Your selection will expand with the merged cells. You can try this:

ActiveCell.EntireColumn.Copy ActiveCell.Offset(0, 1).EntireColumn

And again, you should find some way to avoid counting on the ActiveCell in your code, and use some fully qualified range.

Upvotes: 5

Related Questions