无法将边框信息从单元格转换为自定义样式

时间:2015-06-24 13:37:28

标签: excel vba

以下解决

我一直在编写一个工作簿,根据我在ListObject表中输入的单元格信息加载一堆自定义单元格样式,如下所示: Table

每个单元格都有一个独特的格式配置,希望是当我需要添加更多时,我只需要展开列表。我创建的代码可以处理每个单元格的详细信息,直到它来到Borders部分,然后它似乎根本不读它,或者它自己构成它边界样式应该是什么的定义。这是我的代码(对不起,有点长):

Dim lstStle As ListObject
Dim obj_wbk As Object

For Each obj_wbk In ActiveWorkbook.Styles
    If obj_wbk.Name <> "Normal" Then
        obj_wbk.Delete
    End If
Next obj_wbk
Set obj_wbk = Nothing

Set lstStle = Sheet1.ListObjects(1)
For Each obj_wbk In lstStle.DataBodyRange
    If obj_wbk.Value2 <> vbNullString Then
        ActiveWorkbook.Styles.Add Name:=obj_wbk.Value2
        With ActiveWorkbook.Styles(obj_wbk.Value2)
'!!!!!!!!!!!Number properties
            .IncludeNumber = True
            .NumberFormat = obj_wbk.NumberFormat
'!!!!!!!!!!!Alignment properties
            .IncludeAlignment = True
            .HorizontalAlignment = obj_wbk.HorizontalAlignment
            .IndentLevel = obj_wbk.IndentLevel
            .Orientation = obj_wbk.Orientation
            .ReadingOrder = obj_wbk.ReadingOrder
            .ShrinkToFit = obj_wbk.ShrinkToFit
            .VerticalAlignment = obj_wbk.VerticalAlignment
            .WrapText = obj_wbk.WrapText
'!!!!!!!!!!!Font properties
            .IncludeFont = True
            .Font.Bold = obj_wbk.Font.Bold
            .Font.Color = obj_wbk.Font.Color
            .Font.Italic = obj_wbk.Font.Italic
            .Font.Name = obj_wbk.Font.Name
            .Font.Size = obj_wbk.Font.Size
            .Font.Strikethrough = obj_wbk.Font.Strikethrough
            .Font.Subscript = obj_wbk.Font.Subscript
            .Font.Superscript = obj_wbk.Font.Superscript
            .Font.Underline = obj_wbk.Font.Underline
'!!!!!!!!!!!Borders properties
            .IncludeBorder = True
            .Borders(xlEdgeLeft).Color = obj_wbk.Borders(xlEdgeLeft).Color
            .Borders(xlEdgeTop).Color = obj_wbk.Borders(xlEdgeTop).Color
            .Borders(xlEdgeBottom).Color = obj_wbk.Borders(xlEdgeBottom).Color
            .Borders(xlEdgeRight).Color = obj_wbk.Borders(xlEdgeRight).Color
            .Borders(xlEdgeLeft).LineStyle = obj_wbk.Borders(xlEdgeLeft).LineStyle
            .Borders(xlEdgeTop).LineStyle = obj_wbk.Borders(xlEdgeTop).LineStyle
            .Borders(xlEdgeBottom).LineStyle = obj_wbk.Borders(xlEdgeBottom).LineStyle
            .Borders(xlEdgeRight).LineStyle = obj_wbk.Borders(xlEdgeRight).LineStyle
            .Borders(xlEdgeLeft).Weight = obj_wbk.Borders(xlEdgeLeft).Weight
            .Borders(xlEdgeTop).Weight = obj_wbk.Borders(xlEdgeTop).Weight
            .Borders(xlEdgeBottom).Weight = obj_wbk.Borders(xlEdgeBottom).Weight
            .Borders(xlEdgeRight).Weight = obj_wbk.Borders(xlEdgeRight).Weight
'!!!!!!!!!!!Patterns/Interiors properties
            .IncludePatterns = True
            .Interior.Color = obj_wbk.Interior.Color
            .Interior.Pattern = obj_wbk.Interior.Pattern
            .Interior.PatternColor = obj_wbk.Interior.PatternColor
'!!!!!!!!!!!Protection properties
            .IncludeProtection = False
        End With
    End If
Next obj_wbk

就像我说的那样,除了Borders之外,其他所有内容都会被读取并推送到新创建的样式中。我错过了什么吗?

更新:当我试图进一步分解这个过程时,我发现当我尝试定义.Borders(xlEdgeLeft)属性时,它会接受定义,但会立即忘记/更改所述属性的值在下一行。例如,.Color值可能在指定非零数字后自发地更改为0,或.LineStyle可能会自发地更改为-4142。之后,任何以下.Borders(...)属性都会被取消。我觉得这是一个错误。从给定单元格中检索边框项属性似乎有效,但是将边框段属性分配给自定义单元格样式则不然。任何进一步的见解将不胜感激。

解决:我找到了解决类似问题的VSTO问题的解决方案: Excel range Style: specifying Borders via VSTO doesn't work

(xlEdge...)替换为非Edge枚举(即将Borders(xlEdgeLeft)更改为Borders(xlLeft)),它完全解决了我遇到的问题。在我看来,微软对边界对象的定义需要详细阐述,以包含xlLeft,xlRight,xlTop和xlBottom枚举作为索引选项。 Border Object (Excel)

1 个答案:

答案 0 :(得分:0)

将(xlEdge ...)替换为非Edge枚举(即将Borders(xlEdgeLeft)更改为Borders(xlLeft)),它完全解决了我所遇到的问题。在我看来,微软对边界对象的定义需要详细阐述,以包含xlLeft,xlRight,xlTop和xlBottom枚举作为索引选项。边框对象(Excel)