See Format - "Copy and Apply Cell's Formatting Settings
"Bill Roberts" <BillR...@discussions.microsoft.com> wrote in message
news:9FE7CEAD-2ACA-4021...@microsoft.com...
It would require a macro. See below, and run the macro CopyCFFormats,
selecting the ranges as appropriate. As written, the code will transfer
Bold and background color - but you can add as many formatting properties as
your situation requires.
HTH,
Bernie
MS Excel MVP
Sub CopyCFFormats()
Dim R1 As Range
Dim R2 As Range
Dim i As Integer
Dim j As Integer
Dim m As Range
Dim myRet As Variant
Set R1 = Application.InputBox("Select the CF'd range", Type:=8)
Set R2 = Application.InputBox("Select the final range", Type:=8)
If R1.Cells.Count <> R2.Cells.Count Or R1.Rows.Count <> R2.Rows.Count Then
MsgBox "You must select ranges of equal size and shape"
End If
For i = 1 To R1.Rows.Count
For j = 1 To R1.Columns.Count
myRet = CheckFormat(R1.Cells(i, j))
If myRet = False Then GoTo NoCF
If myRet = "None" Then GoTo NoCF
'Copy each desired format, like so:
R2.Cells(i, j).Interior.ColorIndex = _
R1.Cells(i, j).FormatConditions(myRet).Interior.ColorIndex
R2.Cells(i, j).Font.Bold = _
R1.Cells(i, j).FormatConditions(myRet).Font.Bold
NoCF:
Next j
Next i
End Sub
Function CheckFormat(c As Range) As Variant
Dim bCheck As Boolean
Dim i As Integer
If c.FormatConditions.Count = 0 Then
CheckFormat = False
Exit Function
End If
For i = 1 To c.FormatConditions.Count
If c.FormatConditions.Item(i).Type = 1 Then
bCheck = False
Select Case c.FormatConditions.Item(i).Operator
Case 1 ' between
If (c.Value >= CDbl(c.FormatConditions.Item(i).Formula1)) And _
(c.Value <= CDbl(c.FormatConditions.Item(i).Formula2)) Then _
bCheck = True
Case 2 ' not between
If c.Value < CDbl(c.FormatConditions.Item(i).Formula1) Or _
c.Value > CDbl(c.FormatConditions.Item(i).Formula2) Then _
bCheck = True
Case 3 ' equal to
If c.Value = CDbl(c.FormatConditions.Item(i).Formula1) Then _
bCheck = True
Case 4 ' not equal to
If c.Value <> CDbl(c.FormatConditions.Item(i).Formula1) Then _
bCheck = True
Case 5 ' greater then
If c.Value > CDbl(c.FormatConditions.Item(i).Formula1) Then _
bCheck = True
Case 6 ' less then
If c.Value < CDbl(c.FormatConditions.Item(i).Formula1) Then _
bCheck = True
Case 7 ' greater & equal then
If c.Value >= CDbl(c.FormatConditions.Item(i).Formula1) Then _
bCheck = True
Case 8 ' less & equal then
If c.Value <= CDbl(c.FormatConditions.Item(i).Formula1) Then _
bCheck = True
End Select
If bCheck = True Then
CheckFormat = i
bCheck = False
Exit Function
End If
End If
Next i
CheckFormat = "None"
End Function
"Bill Roberts" <BillR...@discussions.microsoft.com> wrote in message
news:9FE7CEAD-2ACA-4021...@microsoft.com...
"Bernie Deitrick" wrote:
> .
>
> Function CheckFormat(c As Range) As Variant
rest of code
>End Function
Did you copy it and the macro to a general module?
Gord Dibben MS Excel MVP
"Bernie Deitrick" wrote:
> .
>
Case xlBetween
If (c.Value >= CDbl(c.FormatConditions.Item(i).Formula1)) And _
(c.Value <= CDbl(c.FormatConditions.Item(i).Formula2)) Then
bCheck = True
Case xlNotBetween
If c.Value < CDbl(c.FormatConditions.Item(i).Formula1) Or _
c.Value > CDbl(c.FormatConditions.Item(i).Formula2) Then
bCheck = True
Case xlEqual
If c.Value = CDbl(c.FormatConditions.Item(i).Formula1) Then
bCheck = True
Case xlNotEqual
If c.Value <> CDbl(c.FormatConditions.Item(i).Formula1) Then
bCheck = True
Case xlGreater
If c.Value > CDbl(c.FormatConditions.Item(i).Formula1) Then
bCheck = True
Case xlLess
If c.Value < CDbl(c.FormatConditions.Item(i).Formula1) Then
bCheck = True
Case xlGreaterEqual
If c.Value >= CDbl(c.FormatConditions.Item(i).Formula1) Then
bCheck = True
Case xlLessEqual
If c.Value <= CDbl(c.FormatConditions.Item(i).Formula1) Then
bCheck = True
End Select
If bCheck = True Then
CheckFormat = i
bCheck = False
Exit Function
End If
End If
Next i
CheckFormat = "None"
End Function
--
Bill Roberts
Every one of your "Then's" needs to be followed by a space and an
underscore (the continuation character).
For example:
Case xlLess
If c.Value < CDbl(c.FormatConditions.Item(i).Formula1) Then
bCheck = True
Should be
Case xlLess
If c.Value < CDbl(c.FormatConditions.Item(i).Formula1) Then _
bCheck = True
Other wise, you could use
Case xlLess
If c.Value < CDbl(c.FormatConditions.Item(i).Formula1) Then
bCheck = True
End If
But you need to do that for every one...
Bernie
> Select Case c.FormatConditions.Item(i).Operator
>
> Case xlBetween
> If (c.Value >= CDbl(c.FormatConditions.Item(i).Formula1)) And _
> (c.Value <= CDbl(c.FormatConditions.Item(i).Formula2)) Then
> bCheck = True
>
> Case xlNotBetween
> If c.Value < CDbl(c.FormatConditions.Item(i).Formula1) Or _
> c.Value > CDbl(c.FormatConditions.Item(i).Formula2) Then
> bCheck = True
>
> Case xlEqual
> If c.Value = CDbl(c.FormatConditions.Item(i).Formula1) Then
> bCheck = True
>
> Case xlNotEqual
> If c.Value <> CDbl(c.FormatConditions.Item(i).Formula1) Then
> bCheck = True
>
> Case xlGreater
> If c.Value > CDbl(c.FormatConditions.Item(i).Formula1) Then
> bCheck = True
>
> Case xlLess
> If c.Value < CDbl(c.FormatConditions.Item(i).Formula1) Then
> bCheck = True
>
> Case xlGreaterEqual
> If c.Value >= CDbl(c.FormatConditions.Item(i).Formula1) Then
> bCheck = True
>> > Case 1 ' between
"Bernie Deitrick" wrote:
> .
>
What are the values that the cell can have? What CF are you actually using?
It would help if you select one of the cells with the CF that you want, and start the macro
recorder. Make a minor change to one condition of the CF and then stop the recorder. (You can then
change the CF back.)
Post that code when you are done.
HTH,
Bernie
MS Excel MVP
"Bill Roberts" <BillR...@discussions.microsoft.com> wrote in message
news:79E169C6-0E42-4A76...@microsoft.com...
The code will only work if you are not using the Formula is... option.
HTH,
Bernie
MS Excel MVP
"Bill Roberts" <BillR...@discussions.microsoft.com> wrote in message
news:A6169F26-7774-49BF...@microsoft.com...
> Bernie, here is the code as recorded. I created a macro with the same steps
> as I conditionally formatted the column. I did not do it one cell at a time
> with a "for..each" statement. Would that be better?? Hope this helps.
Below is code that will work with the Formula is.. option. If you are only using Formula Is.. then
you can cut out a lot of the code. Note that the code now must select the cell with the CF to
properly evaluate the formulas, since Excel uses the activecell as the basis for the relative cell
locations in the CF formulas.
HTH,
Bernie
MS Excel MVP
Sub CopyCFFormats()
Dim R1 As Range
Dim R2 As Range
Dim i As Integer
Dim j As Integer
Dim Sel As Range
Set Sel = Selection
Dim m As Range
Dim myRet As Variant
Set R1 = Application.InputBox("Select the CF'd range", Type:=8)
Set R2 = Application.InputBox("Select the final range", Type:=8)
If R1.Cells.Count <> R2.Cells.Count Or R1.Rows.Count <> R2.Rows.Count Then
MsgBox "You must select ranges of equal size and shape"
End If
Application.EnableEvents = False
For i = 1 To R1.Rows.Count
For j = 1 To R1.Columns.Count
R1.Cells(i, j).Select
myRet = CheckFormat(R1.Cells(i, j))
If myRet = False Then GoTo NoCF
If myRet = "None" Then GoTo NoCF
'Copy each desired format, like so:
R2.Cells(i, j).Interior.ColorIndex = _
R1.Cells(i, j).FormatConditions(myRet).Interior.ColorIndex
R2.Cells(i, j).Font.Bold = _
R1.Cells(i, j).FormatConditions(myRet).Font.Bold
NoCF:
Next j
Next i
Sel.Select
Application.EnableEvents = True
End Sub
Function CheckFormat(c As Range) As Variant
Dim bCheck As Boolean
Dim i As Integer
CheckFormat = "None"
If c.FormatConditions.Count = 0 Then
CheckFormat = False
Exit Function
End If
For i = 1 To c.FormatConditions.Count
If c.FormatConditions.Item(i).Type = 1 Then
bCheck = False
Select Case c.FormatConditions.Item(i).Operator
End Select
If bCheck = True Then
CheckFormat = i
bCheck = False
Exit Function
End If
End If
If c.FormatConditions.Item(i).Type = 2 Then
bCheck = Application.Evaluate(c.FormatConditions.Item(i).Formula1)
If bCheck Then
CheckFormat = i
bCheck = False
Exit Function
End If
End If
Next i
CheckFormat = "None"
End Function
"Bernie Deitrick" <deitbe @ consumer dot org> wrote in message
news:OIbRQpEd...@TK2MSFTNGP02.phx.gbl...
"Bernie Deitrick" wrote:
> .
>
--
Bill Roberts
Did you leave in the line?
R1.Cells(i, j).Select
Excel evaluated CF formulas as if it were copied into the currently selected
cell - so if the selection doesn't change, the formula evaluates to only one
value.
Bernie
"Bill Roberts" <BillR...@discussions.microsoft.com> wrote in message
news:92681948-D690-4365...@microsoft.com...
Option Explicit
Dim R1 As Range
Dim R2 As Range
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim Sel As Range
Dim myRet As Variant
Dim bCheck As Boolean
Sub CopyCFFormatsA()
Set Sel = Selection
Set R1 = Range("c1:c10")
Set R2 = Range("d1:d10")
j = 1
Application.EnableEvents = False
For i = 1 To R1.Rows.Count
R1.Cells(i, j).Select
myRet = CheckFormat(R1.Cells(i, j))
If myRet = False Then GoTo NoCF
If myRet = "None" Then GoTo NoCF
R2.Cells(i, j).Interior.colorindex = _
R1.Cells(i, j).FormatConditions(myRet).Interior.colorindex
NoCF:
Next i
Sel.Select
Application.EnableEvents = True
End Sub
Function CheckFormat(c As Range) As Variant
CheckFormat = "None"
For k = 1 To c.FormatConditions.Count
bCheck = Application.Evaluate(c.FormatConditions.Item(k).Formula1)
If bCheck Then
CheckFormat = k
bCheck = False
Exit Function
End If
Next k
CheckFormat = "None"
End Function--
Your version of the code worked perfectly, but I'm testing it in XL 2003. I
will have to try to locate a machine with XL2007 to try it out on, and I
will post back in the morning.
Bernie
"Bill Roberts" <BillR...@discussions.microsoft.com> wrote in message
news:751EB0E4-4C64-4699...@microsoft.com...