【问题标题】:Cell border with a varying range具有不同范围的单元格边框
【发布时间】:2016-07-15 17:16:36
【问题描述】:

当我通过 FOR LOOP 时,我试图在单个单元格周围设置边框,其中循环的数量取决于其中包含内容的列数,并且列数会因工作表而异。

看来我不能单独在动态单元格位置周围设置边框,而是必须指定范围或特定单元格。那是对的吗?

这是我用来实现目标的代码...

Sub Sheet_Formatting()

   For Col_Count = 1 To Col_Count_Active_Sheet

    ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Interior.Color = RGB(100, 100, 100)   ' sets to the color ???
    ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Borders.LineStyle.xlContinuous        ' sets to the linestyle
    ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Borders.Weight.xlThick                ' sets to the border thickness
    ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Font.Bold = True                      ' sets to the text format to bold

   Next Col_Count
End Sub

我相信有一种方法可以完成边框规范,但我不能使用“单元格”来指定任何边框。内部颜色和字体根据需要工作。

【问题讨论】:

    标签: vba excel formatting


    【解决方案1】:

    您需要为 Border 分配属性值,就像您为 Color 和 Bold 所做的那样

    Sub Sheet_Formatting()
    
    
    
       For Col_Count = 1 To Col_Count_Active_Sheet
    
        ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Interior.Color = RGB(100, 100, 100)   ' sets to the color ???
        ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Borders.LineStyle = xlContinuous       ' sets to the linestyle
        ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Borders.Weight = xlThick               ' sets to the border thickness
        ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).Font.Bold = True                      ' sets to the text format to bold
    
       Next Col_Count
    End Sub
    

    另外,另一种方法是:

    ThisWorkbook.Worksheets(Active_Sheet).Cells(1, Col_Count).BorderAround (xlThin)

    【讨论】:

    • 为边框添加属性值是什么意思?我以为这就是 xlContinuous 和 xlThick 的用途?
    • 你正在使用这个.Borders.LineStyle.xlContinuous 。它不是一个任务。这是分配.Borders.LineStyle=xlContinuous 另外,当您的查询得到解决时,请始终将其标记为已回答/赞成。
    【解决方案2】:

    Range.Borders 属性和Border Object 之间存在差异。不要混淆两者。

    Sub Sheet_Formatting()
    
       For Col_Count = 1 To Col_Count_Active_Sheet
    
            With ThisWorkbook.Worksheets(Active_Sheet).Cells(1, col_Count)
                .Interior.Color = RGB(100, 100, 100)   ' sets to the color ???
                .Borders.LineStyle = xlContinuous      ' sets to the linestyle
                .Borders.Weight = xlThick              ' sets to the border thickness
                .Font.Bold = True                      ' sets to the text format to bold
            End With
    
       Next Col_Count
    
    End Sub
    

    我用With ... End With statement 清理了一些重复的.Parent 引用。这不仅使代码更简洁,而且运行速度也更快。

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 1970-01-01
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多