Excel宏边框问题

huangapple go评论88阅读模式
英文:

Excel Macro Border Issue

问题

我有一个看起来很简单的要求,需要在Excel宏中添加一个功能,但是我无论如何都无法使其工作。我想在这个边框宏中添加一个额外的循环,用于为财务小计添加双下边框和单上边框。有人可以帮我将其添加到这个循环中吗?

谢谢!

Sub CycleBd()
    With Selection
        If .Borders.LineStyle = xlNone Then
            .Borders(xlEdgeBottom).Weight = xlThin
        ElseIf .Borders(xlEdgeBottom).Weight = xlThin And _
            .Borders(xlEdgeTop).LineStyle = xlNone Then
            .Borders.Weight = xlThin
        ElseIf .Borders.Weight = xlThin Then
            .Borders.LineStyle = xlNone '清除所有边框
            .BorderAround colorIndex:=xlAutomatic, Weight:=xlThick
        Else
            .Borders.LineStyle = xlNone '清除所有边框
        End If
    End With
End Sub

我尝试的每一种方法都没有结果,并且默认情况下会清除边框。

英文:

I have what seems to be a simple addition to an excel macro, but I can't get it to work to save my life. I'd like to add one additional cycle to this borders macro that adds a double bottom border and a single top border for financial subtotals. Can anyone help me add it as rotation in this cycle?

Thank you!

Sub CycleBd()
With Selection
If .Borders.LineStyle = xlNone Then
.Borders(xlEdgeBottom).Weight = xlThin
ElseIf .Borders(xlEdgeBottom).Weight = xlThin And _
.Borders(xlEdgeTop).LineStyle = xlNone Then
.Borders.Weight = xlThin
ElseIf .Borders.Weight = xlThin Then
.Borders.LineStyle = xlNone 'Clear All
.BorderAround colorIndex:=xlAutomatic, Weight:=xlThick
Else
.Borders.LineStyle = xlNone 'Clear All
End If
End With
End Sub

Every attempt I've tried has no result and it defeaults to clearing borders.

答案1

得分: 1

我相信这是你想要做的事情,添加第四个“cycle”。为了简洁起见,它只检查顶部边框是否为xlThick,但如果将来添加更多的“cycles”,你可能需要更改这一点。

Sub CycleBd()
    With Selection
        If .Borders.LineStyle = xlNone Then
            .Borders(xlEdgeBottom).Weight = xlThin
        ElseIf .Borders(xlEdgeBottom).Weight = xlThin And _
            .Borders(xlEdgeTop).LineStyle = xlNone Then
            .Borders.Weight = xlThin
        ElseIf .Borders.Weight = xlThin Then
            .Borders.LineStyle = xlNone 'Clear All
            .BorderAround ColorIndex:=xlAutomatic, Weight:=xlThick
        ElseIf .Borders(xlEdgeTop).Weight = xlThick Then
            .Borders.LineStyle = xlNone 'Clear All
            .Borders(xlEdgeBottom).LineStyle = xlDouble
            .Borders(xlEdgeTop).Weight = xlThin
        Else
            .Borders.LineStyle = xlNone 'Clear All
        End If
    End With
End Sub

我还要补充一点,你可以将这些特定样式添加到快速访问工具栏中,这样你就不必循环遍历它们了。

Excel宏边框问题

英文:

I believe this is what you're trying to do, add a 4th 'cycle'. It only checks if the top border is xlThick for conciseness, but you might need to change that if you add more 'cycles' in the future.

Sub CycleBd()
With Selection
If .Borders.LineStyle = xlNone Then
.Borders(xlEdgeBottom).Weight = xlThin
ElseIf .Borders(xlEdgeBottom).Weight = xlThin And _
.Borders(xlEdgeTop).LineStyle = xlNone Then
.Borders.Weight = xlThin
ElseIf .Borders.Weight = xlThin Then
.Borders.LineStyle = xlNone 'Clear All
.BorderAround ColorIndex:=xlAutomatic, Weight:=xlThick
ElseIf .Borders(xlEdgeTop).Weight = xlThick Then
.Borders.LineStyle = xlNone 'Clear All
.Borders(xlEdgeBottom).LineStyle = xlDouble
.Borders(xlEdgeTop).Weight = xlThin
Else
.Borders.LineStyle = xlNone 'Clear All
End If
End With
End Sub

I'd also add that you can add those specific styles to the quick access toolbar, so you wouldn't have to cycle through them.

Excel宏边框问题

答案2

得分: 0

工作表的数据结构不清楚。提供的代码展示了如何在总行的顶部边框上配置双线。

Sub SetBorders()
    Dim rng As Range
    Set rng = ActiveSheet.UsedRange
    rng.Borders.LineStyle = xlNone
    rng.Borders.LineStyle = xlContinuous
    rng.BorderAround ColorIndex:=xlAutomatic, Weight:=xlThick
    rng.Rows(rng.Rows.Count - 1).Borders(xlEdgeBottom).LineStyle = xlDouble
End Sub

Excel宏边框问题

英文:

The worksheet data structure is unclear. The provided code shows how to configure a double line on top border of total line.

Sub SetBorders()
    Dim rng As Range
    Set rng = ActiveSheet.UsedRange
    rng.Borders.LineStyle = xlNone
    rng.Borders.LineStyle = xlContinuous
    rng.BorderAround ColorIndex:=xlAutomatic, Weight:=xlThick
    rng.Rows(rng.Rows.Count - 1).Borders(xlEdgeBottom).LineStyle = xlDouble
End Sub

Excel宏边框问题

huangapple
  • 本文由 发表于 2023年8月9日 03:03:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定