?
Solved

Reference a cell contents and formatting

Posted on 2014-01-07
11
Medium Priority
?
366 Views
Last Modified: 2014-01-08
Is there a way I can reference not only a cells contents but also copy its formatting in at the same time?
Thanks experts
0
Comment
Question by:Jenedge73
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 81

Expert Comment

by:byundt
ID: 39764078
You can paste formulas & formatting with the Paste method, but it won't transfer changes within a cells contents (e.g. if one word in a cell has red font, but the rest are black). If you want the values returned by the original formulas instead of the results of the formulas in the new cells, then do a PasteSpecial xlValues.
Sub CopyRange()
Dim rgCopy As Range, rgDest As Range
With Worksheets("Sheet1")
    Set rgCopy = .Range("A1:D5")
    Set rgDest = .Range("M10")
End With
rgCopy.Copy
rgDest.PasteSpecial
rgDest.PasteSpecial xlPasteValues
End Sub

Open in new window

0
 
LVL 23

Expert Comment

by:yo_bee
ID: 39764095
Can you expand on your goal?

Example
You can use Conditional formatting.

If A3 > 4 then  Format B3 Blue and Bold font
0
 

Author Comment

by:Jenedge73
ID: 39764269
I have two worksheets WS1 is completely formatted and had values
WS2 has no formatting
If WS1!A1 is Red formatted as a date DD-MMM-YY  (01-Jan-13)
in WS2!A1 I want to =WS1!A1 and I want WS2!A1 to see a red date 01-Jan-13
Can it be done?
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 81

Expert Comment

by:byundt
ID: 39764298
The answer to your question depends on the version of Excel that you are using and how you are setting the font color on WS1.

If you are using regular formatting, the code could be as simple as:
Sub RedFont()
With Worksheets("Sheet1").Range("A1")
    If .Font.ColorIndex = 3 And IsDate(.Value) Then
        .Copy Worksheets("Sheet2").Range("A1")
    End If
End With
End Sub

Open in new window

0
 
LVL 81

Expert Comment

by:byundt
ID: 39764302
If I need to match the date formatting as well as color, then use:
Sub RedFont()
With Worksheets("Sheet1").Range("A1")
    If .Font.ColorIndex = 3 And .NumberFormat = "dd-mm-yy" Then
        .Copy Worksheets("Sheet2").Range("A1")
    End If
End With
End Sub

Open in new window

0
 

Author Comment

by:Jenedge73
ID: 39764310
Im using excel 2013
I don't know what you mean by formatting since I want to replicate the formatting from WS1
Sorry if im not very clear
0
 
LVL 81

Expert Comment

by:byundt
ID: 39764319
Conditional formatting can be used to set the color of the date in WS1 cell A1. If so, the code needs to be different. Since you are using Excel 2013, I can use the range.DisplayFormat to trap the format (both font color and number format) as the value of the cell is displayed (i.e. taking into account both regular formatting and conditional formatting).
Sub RedFont()
Dim rg As Range
With Worksheets("Sheet1").Range("A1")
    If IsDate(.Value) And .DisplayFormat.Font.ColorIndex = 3 And .DisplayFormat.NumberFormat = "dd-mm-yy" Then
        .Copy Worksheets("Sheet2").Range("A1")
    End If
End With
End Sub

Open in new window

0
 

Author Comment

by:Jenedge73
ID: 39764325
basically what I want to do is replicate exactly what is in WS1 into WS2 using (=).  Also, I need it to be dynamic so that WS2 will show any change formatting or otherwise in WS1.
0
 
LVL 81

Accepted Solution

by:
byundt earned 1000 total points
ID: 39764338
You cannot have what you asked for. It is not possible, because formatting changes do not trigger events. It is therefore possible that WS2 formats may not match those in WS1. The best that we can do is to get the formats to match after the user does something else (like change the value of a cell, select a cell or activate a worksheet).

Cell value changes initiated by the user can be trapped as can calculations. But given how often a calculation occurs, I recommend relying on trapping a manual change in cell value instead.

Could you please get very specific (such as by posting a workbook), so we can see how the value of WS1 cell A1 is changing. Is it a formula or a user entry? Can we defer the updating of WS2 cell A1 until the user selects worksheet WS2?
0
 

Author Closing Comment

by:Jenedge73
ID: 39765630
Thanks for your help.
0
 
LVL 23

Expert Comment

by:yo_bee
ID: 39765967
I know this is closed, but can you attach the excel sheets.
I would like to take a look.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

741 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