Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 从条件格式返回线型_Excel_Vba_Conditional Formatting - Fatal编程技术网

Excel 从条件格式返回线型

Excel 从条件格式返回线型,excel,vba,conditional-formatting,Excel,Vba,Conditional Formatting,我正试图用一个结果示例编译一个条件格式的详细列表 我需要弄清楚如何在行样式中返回True或False。或者如果可能,返回线条样式名称(例如xlContinuous)。并将其应用于第11个单元格 如果可以帮助任何人,注释掉就是工作部分 Sub CompileConditionalFormattingList() Dim i As Long, cSh As Worksheet, nSh As Worksheet Set cSh = ActiveSheet Applicatio

我正试图用一个结果示例编译一个条件格式的详细列表

我需要弄清楚如何在行样式中返回
True
False
。或者如果可能,返回线条样式名称(例如xlContinuous)。并将其应用于第11个单元格

如果可以帮助任何人,注释掉就是工作部分

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

一个普通单元格最多可以有8个边框(从5=xl对角线向下到12=xl水平内),
但格式条件只能有4个边框(1=左,2=右,3=顶,4=底)

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

此外,我还设置了
颜色索引
,因为未填充的单元格将显示黑色内部

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
如果要查看格式条件的更多参数,可以通过以下方式将其分配给变量:

Dim fc as FormatCondition
...
Set fc = cSh.Cells.FormatConditions(i)
Stop

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

此代码有什么问题?运行此代码时会发生什么?你有什么要说的吗?Mikku粘贴了错误的版本,是吗tired@TimWilliams粘贴了错误的版本,找出了部分内容,并试图在编辑的版本中进行澄清。注释掉是现在起作用的部分。