[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Reference a cell contents and formatting

Posted on 2014-01-07
11
Medium Priority
?
372 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

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.

Question has a verified solution.

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

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
New style of hardware planning for Microsoft Exchange server.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

649 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