如何减少用于格式化许多表格的重复次数?

问题描述 投票:1回答:1

我有一个漂亮的表,上面有一个VBA代码,可以填充它并自动对其进行格式化(我已经花了很长时间了。

非常好,快速,运行良好,但是当我看一下代码时,有数十行关于单元格的格式...

是否有一种方法可以对此进行优化,使其更适合人眼/大脑?

我让你看看:

Application.DisplayAlerts = False                                           'Deactivate the alerts in case the cell is filled
    With Range("A" & PosStartLine + (TPICode * 3) - 3 & ":A" & PosStartLine + (TPICode * 3) - 1)
        .Select
        .Value = FullTPICode
        .Interior.Color = RGB(220, 230, 241)
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Borders.LineStyle = xlContinuous
        .Font.Size = 11
        .Font.Bold = True
        .Merge       'Merging the 3 cells
        .EntireRow.Borders(xlEdgeTop).Weight = xlMedium
        .EntireRow.Borders(xlEdgeBottom).Weight = xlMedium
    End With
    Application.DisplayAlerts = True

    'Writes down the details on the next column (as we're there... why not?)
    With Worksheets(RealData).Range("B" & PosStartLine + (TPICode * 3) - 3)
        .Select
        .Value = "Nb cars done"
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlCenter
        .Interior.Color = RGB(216, 228, 188)
        .Borders.LineStyle = xlContinuous
        .Font.Size = 11
    End With

    With ActiveCell.Offset(1, 0)
        .Value = "Nb cars left"
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlCenter
        .Interior.Color = RGB(217, 217, 217)
        .Borders.LineStyle = xlContinuous
        .Font.Size = 11
    End With

    With ActiveCell.Offset(2, 0)
        .Value = "Price"
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlCenter
        .Interior.Color = RGB(252, 213, 180)
        .Borders.LineStyle = xlContinuous
        .EntireColumn.AutoFit
        .Font.Size = 11
    End With

其他地方:

    Worksheets(RealDataReg).Activate
    With Worksheets(RealDataReg).Range(Split(Cells(1, PosStartColumn).Address, "$")(1) & PosStartLine + (TPICode * 3) - 3 & ":" & _
    Split(Cells(1, PosStartColumn + 61).Address, "$")(1) & PosStartLine + (TPICode * 3) - 3)
        .HorizontalAlignment = xlRight
        .VerticalAlignment = xlCenter
        .Interior.Color = RGB(235, 241, 222)
        .Borders.LineStyle = xlContinuous
        .Font.Size = 11
    End With

    With Worksheets(RealDataReg).Range(Split(Cells(1, PosStartColumn).Address, "$")(1) & PosStartLine + (TPICode * 3) - 2 & ":" & _
    Split(Cells(1, PosStartColumn + 61).Address, "$")(1) & PosStartLine + (TPICode * 3) - 2)
        .HorizontalAlignment = xlRight
        .VerticalAlignment = xlCenter
        .Interior.Color = RGB(242, 242, 242)
        .Borders.LineStyle = xlContinuous
        .Font.Size = 11
    End With

    With Worksheets(RealDataReg).Range(Split(Cells(1, PosStartColumn).Address, "$")(1) & PosStartLine + (TPICode * 3) - 1 & ":" & _
    Split(Cells(1, PosStartColumn + 61).Address, "$")(1) & PosStartLine + (TPICode * 3) - 1)
        .HorizontalAlignment = xlRight
        .VerticalAlignment = xlCenter
        .Interior.Color = RGB(253, 233, 217)
        .Borders.LineStyle = xlContinuous
        .Font.Size = 11
    End With

'Putting the calculation in the last cell of the table
    With Worksheets(RealDataReg)
            .Cells(PosStartLine + (TPICode * 3) - 3, PosStartColumn + 61).Formula = "=SUM(" & _
            .Cells(PosStartLine + (TPICode * 3) - 3, PosStartColumn + 1).Address(False, False) & ":" & _
            .Cells(PosStartLine + (TPICode * 3) - 3, Split(Cells(1, PosStartColumn + 60).Address, "$")(1)).Address(False, False) & ")"
    End With
'Formatting the cells with the calculations
    With Worksheets(RealDataReg).Range(Split(Cells(1, PosStartColumn + 61).Address, "$")(1) & PosStartLine + (TPICode * 3) - 3)
            .HorizontalAlignment = xlRight
            .VerticalAlignment = xlCenter
            .Interior.Color = RGB(216, 228, 188)
            .Borders.LineStyle = xlContinuous
            .Font.Size = 11
            .Select
    End With

依此类推,依此类推...

有人知道使所有这些格式更有效的聪明方法吗?如您所见,每次格式实际上都是相似的,但是会出现细微的差异(颜色,边框,范围等)

我曾考虑过要创建一个我将要调用的函数,但是我必须给它提供所有相同的信息,所以,我想这是毫无意义的...例如,如果我使用此格式化块:

With Worksheets(RealDataReg).Range(Split(Cells(1, PosStartColumn).Address, "$")(1) & PosStartLine + (TPICode * 3) - 3 & ":" & _
Split(Cells(1, PosStartColumn + 61).Address, "$")(1) & PosStartLine + (TPICode * 3) - 3)
    .HorizontalAlignment = xlRight
    .VerticalAlignment = xlCenter
    .Interior.Color = RGB(235, 241, 222)
    .Borders.LineStyle = xlContinuous
    .Font.Size = 11
End With

它将变成这样:

Call FormatFunction( Worksheets(RealDataReg), Range(Split(Cells(1, PosStartColumn).Address, "$")(1) & PosStartLine + (TPICode * 3) - 3 & ":" & _
Split(Cells(1, PosStartColumn + 61).Address, "$")(1) & PosStartLine + (TPICode * 3) - 3), xlRight, xlCenter, (235, 241, 222), xlContinuous, 11))

但是,当然,在某些情况下某些参数是不需要的,因此在传递参数时,我必须“瞄准好参数”。使用标准VBA格式更容易做到这一点,我想...

我想有一种标准的聪明方法,但是我无法在google上找到任何东西(它只是为我提供了如何格式化表格的结果,但我已经知道了:D)

非常感谢!

excel vba optimization formatting
1个回答
0
投票

也许有人会证明我错了(如果这样做的话,那很好,但是我不相信您的问题有任何“正确”的答案。

也许最简单,最不多余的方法是确定两种或多种经常一起出现的格式。然后定义一个自定义数据类型以通过一个变量传递格式。

或者,您可以创建一个设置相同格式的子例程,然后将要格式化的对象作为参数传递。

我的个人选择实际上会稍微复杂一些,但是我觉得它并不优雅,并且易于维护和修改。长话短说:常量和枚举器用于由类设置的类别。

例如,我有一个按钮状态:它可以打开,关闭,空闲或加载;每个州的费用都不相同。我首先将所有基本属性定义为常量(例如颜色,字体大小等)。然后,我定义一个属性数据类型来保存这些常量,但是我还没有签署任何值。相反,我使用它们来创建另一个数据类型,原因是如果保存所有属性,则表示特定状态(打开,关闭等)。所有按钮共有(所有按钮都有底色,前色等)。我为每个状态创建该类型的变量。然后,我使用一个类将所有这些与状态一起暴露为属性。所有这些基础工作使我可以执行mybutton.State.isOn myButton.State isIdle

之类的操作

进行设置需要做很多工作,但这并不是重复性的,它使使用按钮变得很愉快。

© www.soinside.com 2019 - 2024. All rights reserved.