peispud
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.
Thank you.
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
Thank you.
ASKER
I guess that I don't know how to do it without selecting.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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").PasteSpecia l xlPasteFormats
Range("Area2").PasteSpecia l xlPasteValues
Application.CutCopyMode = False
Cells(RowRef, ColRef).Select
End If
Set oldRange = Target
Application.EnableEvents = True
Application.ScreenUpdating = True
Static oldRange As Range
If oldRange Is Nothing Then Set oldRange = Target: Exit Sub
Application.ScreenUpdating
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").PasteSpecia
Range("Area2").PasteSpecia
Application.CutCopyMode = False
Cells(RowRef, ColRef).Select
End If
Set oldRange = Target
Application.EnableEvents = True
Application.ScreenUpdating
You could do this easily with Copy and PasteSpecial, without selecting.