copy value and formatting from one range to another without select / copy / paste

Hi

I am using excel 2013 VBA.  I would like to copy the value and formatting (background color) of one range to another range.

The two ranges are identical in size and shape.  The code below copies the values, but not the cell formatting.  To be specific, I would like to copy the cell value and the background color of the cells in the range.

Also, I do not want the activecell to change.  Except for the target, I wish the screen to be unaffected.

Range("Area1").Value = Range("Area2").Value

Open in new window


Thank you.
peispudAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieVBA ExpertCommented:
Is there a reason you don't want to use Copy/Paste?

You could do this easily with Copy and PasteSpecial, without selecting.
0
peispudAuthor Commented:
I guess that I don't know how to do it without selecting.
0
AnthonyHamonCommented:
To use Copy and PasteSpecial (formats) without selecting:

Range("Area2").Copy
Range("Area1").PasteSpecial xlPasteFormats

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

peispudAuthor Commented:
This code is being used in the worksheet_selection change event. Using the code given, the focus changes.  The range "Area1" becomes selected.    I am trying to avoid that.

The code below behaves properly..... except for the cell coloring.



The exact code is as follows.   The values get copied correctly.  The interior color (formatting)J does not

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static oldRange As Range
    If Not oldRange Is Nothing Then
        Dim intCount As Long
        If oldRange.Column = 1 Then
           Range("Area2").Interior.Color = Range("Area1").Interior.Color
           Range("Area2").Value = Range("Area1").Value
        End If
    End If
    Set oldRange = Target
End Subb

Open in new window

0
NorieVBA ExpertCommented:
How is 'Area1' being selected?

If it just appears that way, for example with the 'marching ants' around the range, try adding this to the code.
Application.CutCopyMode = False

Open in new window

PS You can't apply formatting like that.
0
peispudAuthor Commented:
Problem solved with you help.  Thanks!!!

Static oldRange As Range
    If oldRange Is Nothing Then Set oldRange = Target: Exit Sub
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    If oldRange.Column = 1 Then
        Dim RowRef As Long: RowRef = ActiveCell.Row
        Dim ColRef As Long: ColRef = ActiveCell.Column
        Range("Area1").Copy
        Range("Area2").PasteSpecial xlPasteFormats
        Range("Area2").PasteSpecial xlPasteValues
        Application.CutCopyMode = False
        Cells(RowRef, ColRef).Select
    End If
    Set oldRange = Target
    Application.EnableEvents = True
    Application.ScreenUpdating = True
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.