?
Solved

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

Posted on 2014-08-24
6
Medium Priority
?
676 Views
Last Modified: 2014-08-24
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.
0
Comment
Question by:peispud
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:Norie
ID: 40281819
Is there a reason you don't want to use Copy/Paste?

You could do this easily with Copy and PasteSpecial, without selecting.
0
 

Author Comment

by:peispud
ID: 40281827
I guess that I don't know how to do it without selecting.
0
 
LVL 4

Accepted Solution

by:
AnthonyHamon earned 1000 total points
ID: 40281837
To use Copy and PasteSpecial (formats) without selecting:

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

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:peispud
ID: 40281887
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
 
LVL 35

Assisted Solution

by:Norie
Norie earned 1000 total points
ID: 40282041
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
 

Author Closing Comment

by:peispud
ID: 40282151
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question