Solved

Reference a cell contents and formatting

Posted on 2014-01-07
11
364 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
Independent Software Vendors: 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!

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
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.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

691 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