条件格式和文本:基于我想要复制另一个单元格格式的条件

时间:2016-03-07 02:02:42

标签: excel excel-vba conditional-formatting vba

在excel中

我正在尝试根据某些条件复制其他单元格的元素和单元格格式。

G12 =(如果C10 = C11,*留空,如果(C10> C11,*复制B10的元素和格式,*复制B11的元素和格式))

我已经尝试过这个功能,并且在复制元素方面取得了成功,但也无法改变格式。我用过:

= IF($ C $ 10 = $ C $ 11,“”,IF($ C $ 10> $ C $ 11,B10,B11))

我了解了条件格式,但没有看到根据某种条件复制另一个单元格格式的方法。必须指定颜色。

感谢您的帮助!

TL:博士

March Madness Example

弗吉尼亚击败UNC 76到75

我想将Copy(1)Virginia复制到带有浅绿色背景的括号中的下一个位置。如果UNC获胜,我会希望(16)UNC和玫瑰色复制到下一个支架点

1 个答案:

答案 0 :(得分:0)

转到vb-editor(Alt + F11),插入新模块并粘贴此代码:

Function RangeSelectionPrompt(txt As String) As Range
    Dim rng As Range
    Set rng = Application.InputBox(txt, "Select a range", Type:=8)

    Set RangeSelectionPrompt = rng
End Function

Sub FormatAs()

    Dim Sh As Worksheet
    Dim r1 As Range, r2 As Range
    Dim txt1 As String, txt2 As String
    Dim cel As Range, Col As Range
    Dim Val, check As Long, pos As Long

    Set Sh = ActiveSheet

    txt1 = "Enter Range #1 - sourse"
    txt2 = "Enter Range #2 - to paste formats"

    Set r1 = Intersect(RangeSelectionPrompt(txt1), Sh.UsedRange)
    Set r2 = Intersect(RangeSelectionPrompt(txt1), Sh.UsedRange)


    For Each cel In r2.Cells

        Val = cel.Value

        If Val <> "" Then

            For Each Col In r1.Columns

                check = WorksheetFunction.CountIf(Col, Val)

                If check > 0 Then
                    pos = WorksheetFunction.Match(Val, Col, 0)

                    Col.Cells(pos, 1).Copy
                    cel.PasteSpecial Paste:=xlPasteFormats

                End If

            Next Col

        End If

    Next cel

End Sub

运行一次以复制格式。您需要定义两个范围: enter image description here

来源范围(A:B)和粘贴范围(F:G)