首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从条件格式返回LineStyle

从条件格式返回LineStyle
EN

Stack Overflow用户
提问于 2019-06-13 02:50:58
回答 1查看 83关注 0票数 0

我正在尝试编译一个条件格式的详细列表,并给出一个结果示例。

我需要弄清楚如何在行样式上返回TrueFalse。或者,如果可能的话,返回行样式名称(如xlContinuous)。并将其应用于第11细胞。

注释是工作的一部分,如果它能帮助任何人。

代码语言:javascript
复制
Sub CompileConditionalFormattingList()
    Dim i As Long, cSh As Worksheet, nSh As Worksheet
    Set cSh = ActiveSheet
    Application.ScreenUpdating = False
    Set nSh = Worksheets.Add(After:=cSh)
    With nSh
        .Name = "Format Report"
        .Cells(1, 1).Resize(, 11).Value = _
        Array("Formula", "Interior Color", "Font Color", "Bold", "Italic", "B.Top", "B.Bottom", "B.Left", "B.Right", "Number Format", "Format")

        For i = 1 To cSh.Cells.FormatConditions.Count
            '.Cells(i + 1, 1).Value = "'" & cSh.Cells.FormatConditions(i).Formula1
            '.Cells(i + 1, 2).Value = cSh.Cells.FormatConditions(i).Interior.Color
            '.Cells(i + 1, 3).Value = cSh.Cells.FormatConditions(i).Font.Color
            '.Cells(i + 1, 4).Value = cSh.Cells.FormatConditions(i).Font.Bold
            '.Cells(i + 1, 5).Value = cSh.Cells.FormatConditions(i).Font.Italic
            .Cells(i + 1, 6).Value = cSh.Cells.FormatConditions(i).Borders(xlEdgeTop).LineStyle ' I want this to return the line style
            .Cells(i + 1, 7).Value = cSh.Cells.FormatConditions(i).Borders(xlEdgeBottom).LineStyle ' I want this to return the line style
            .Cells(i + 1, 8).Value = cSh.Cells.FormatConditions(i).Borders(xlEdgeLeft).LineStyle ' I want this to return the line style
            .Cells(i + 1, 9).Value = cSh.Cells.FormatConditions(i).Borders(xlEdgeRight).LineStyle ' I want this to return the line style
            '.Cells(i + 1, 10).Value = cSh.Cells.FormatConditions(i).NumberFormat
            With .Cells(i + 1, 11)
                '.Value = "Abc123"
                '.Interior.Color = cSh.Cells.FormatConditions(i).Interior.Color
                '.Font.Color = cSh.Cells.FormatConditions(i).Font.Color
                '.Font.Bold = cSh.Cells.FormatConditions(i).Font.Bold
                '.Font.Italic = cSh.Cells.FormatConditions(i).Font.Italic
                .Borders(xlEdgeTop).LineStyle = cSh.Cells.FormatConditions(i).Borders(xlEdgeTop).LineStyle 'Here I want the line style to be replicated
                .Borders(xlEdgeBottom).LineStyle = cSh.Cells.FormatConditions(i).Borders(xlEdgeBottom).LineStyle 'Here I want the line style to be replicated
                .Borders(xlEdgeLeft).LineStyle = cSh.Cells.FormatConditions(i).Borders(xlEdgeLeft).LineStyle 'Here I want the line style to be replicated
                .Borders(xlEdgeRight).LineStyle = cSh.Cells.FormatConditions(i).Borders(xlEdgeRight).LineStyle 'Here I want the line style to be replicated
                '.NumberFormat = cSh.Cells.FormatConditions(i).NumberFormat
            End With
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-13 10:48:21

一个正常的单元可以有多达8个边框(从5= xlDiagonalDown到12 = xlInsideHorizontal),

但是格式条件只能有4个边框(1 =左,2=右,3=顶,4=底部)。

我添加了一个Iif条件,将一些值显式地显示为TrueFalse

另外,我将ColorIndex设置为未填充的单元格,否则将显示为黑色内部。

代码语言:javascript
复制
Sub CompileConditionalFormattingList()
    Dim i As Long, cSh As Worksheet, nSh As Worksheet
    Set cSh = ActiveSheet
    Application.ScreenUpdating = False
    Set nSh = Worksheets.Add(After:=cSh)
    With nSh
        .Name = "Format Report"
        .Cells(1, 1).Resize(, 11).Value = _
        Array("Formula", "Interior Color", "Font Color", "Bold", "Italic", _
            "B.Left", "B.Right", "B.Top", "B.Bottom", "Number Format", "Format")

        For i = 1 To cSh.Cells.FormatConditions.Count
            .Cells(i + 1, 1).Value = "'" & cSh.Cells.FormatConditions(i).Formula1
            .Cells(i + 1, 2).Value = cSh.Cells.FormatConditions(i).Interior.Color
            .Cells(i + 1, 3).Value = cSh.Cells.FormatConditions(i).Font.Color
            .Cells(i + 1, 4).Value = IIf(cSh.Cells.FormatConditions(i).Font.Bold, True, False)
            .Cells(i + 1, 5).Value = IIf(cSh.Cells.FormatConditions(i).Font.Italic, True, False)
            .Cells(i + 1, 6).Value = GetLinestyleName(cSh.Cells.FormatConditions(i).Borders(1).LineStyle)
            .Cells(i + 1, 7).Value = GetLinestyleName(cSh.Cells.FormatConditions(i).Borders(2).LineStyle)
            .Cells(i + 1, 8).Value = GetLinestyleName(cSh.Cells.FormatConditions(i).Borders(3).LineStyle)
            .Cells(i + 1, 9).Value = GetLinestyleName(cSh.Cells.FormatConditions(i).Borders(4).LineStyle)
            .Cells(i + 1, 10).Value = cSh.Cells.FormatConditions(i).NumberFormat
            With .Cells(i + 1, 11)
                .Value = "Abc123"
                .Interior.Color = cSh.Cells.FormatConditions(i).Interior.Color
                .Interior.ColorIndex = cSh.Cells.FormatConditions(i).Interior.ColorIndex
                .Font.Color = cSh.Cells.FormatConditions(i).Font.Color
                .Font.Bold = cSh.Cells.FormatConditions(i).Font.Bold
                .Font.Italic = cSh.Cells.FormatConditions(i).Font.Italic
                .Borders(xlEdgeLeft).LineStyle = cSh.Cells.FormatConditions(i).Borders(1).LineStyle
                .Borders(xlEdgeRight).LineStyle = cSh.Cells.FormatConditions(i).Borders(2).LineStyle
                .Borders(xlEdgeTop).LineStyle = cSh.Cells.FormatConditions(i).Borders(3).LineStyle
                .Borders(xlEdgeBottom).LineStyle = cSh.Cells.FormatConditions(i).Borders(4).LineStyle
                .NumberFormat = cSh.Cells.FormatConditions(i).NumberFormat
            End With
        Next i
    End With
    Application.ScreenUpdating = True
End Sub

Private Function GetLinestyleName(i As Long) As String
    Select Case i
    Case Excel.XlLineStyle.xlContinuous     ' 1
        GetLinestyleName = "xlContinuous"
    Case Excel.XlLineStyle.xlDash           ' -4115
        GetLinestyleName = "xlDash"
    Case Excel.XlLineStyle.xlDashDot        ' 4
        GetLinestyleName = "xlDashDot"
    Case Excel.XlLineStyle.xlDashDotDot     ' 5
        GetLinestyleName = "xlDashDotDot"
    Case Excel.XlLineStyle.xlDot            ' -4118
        GetLinestyleName = "xlDot"
    Case Excel.XlLineStyle.xlDouble         ' -4119
        GetLinestyleName = "xlDouble"
    Case Excel.XlLineStyle.xlLineStyleNone  ' -4142
        GetLinestyleName = "xlLineStyleNone"
    Case Excel.XlLineStyle.xlSlantDashDot   ' 13
        GetLinestyleName = "xlSlantDashDot"
    Case Else
        GetLinestyleName = "unknown"
    End Select
End Function

如果希望看到格式条件的更多参数,可以通过以下方式将其赋值给变量:

代码语言:javascript
复制
Dim fc as FormatCondition
...
Set fc = cSh.Cells.FormatConditions(i)
Stop

如果之后停止代码,则可以在本地窗口中检查其参数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56572742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档