Link to home
Start Free TrialLog in
Avatar of peispud
peispudFlag for Canada

asked on

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.
Avatar of Norie
Norie

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

You could do this easily with Copy and PasteSpecial, without selecting.
Avatar of peispud

ASKER

I guess that I don't know how to do it without selecting.
ASKER CERTIFIED SOLUTION
Avatar of AnthonyHamon
AnthonyHamon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peispud

ASKER

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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peispud

ASKER

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