Solved

Reference a cell contents and formatting

Posted on 2014-01-07
11
359 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 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 22

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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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 500 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 22

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
Outlook Free & Paid Tools
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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…

821 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