Solved

Reference a cell contents and formatting

Posted on 2014-01-07
11
357 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
  • 5
  • 4
  • 2
11 Comments
 
LVL 80

Expert Comment

by:byundt
Comment Utility
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 21

Expert Comment

by:yo_bee
Comment Utility
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
Comment Utility
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
 
LVL 80

Expert Comment

by:byundt
Comment Utility
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 80

Expert Comment

by:byundt
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Jenedge73
Comment Utility
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 80

Expert Comment

by:byundt
Comment Utility
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
Comment Utility
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 80

Accepted Solution

by:
byundt earned 500 total points
Comment Utility
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
Comment Utility
Thanks for your help.
0
 
LVL 21

Expert Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
My experience with Windows 10 over a one year period and suggestions for smooth operation
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now