【发布时间】:2019-10-17 09:27:43
【问题描述】:
我有一个很好的表格,里面有一个 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 格式更容易做到这一点,我猜……
我想有一个标准的聪明方法可以做到这一点,但我无法在谷歌上找到任何东西(它只是给了我关于如何格式化表格的结果,但我已经知道了:D)
提前致谢!
【问题讨论】:
-
“我想过创建一个我会调用的函数,但我必须给它所有相同的信息,所以,我想这毫无意义......”这不是真的。您只需编写一次 fromatting 函数,然后使用您需要的参数调用该函数。这是要走的路,更少的代码,更高的效率
-
@Damian 我编辑我的问题以详细说明这个主题;)
-
对于“有时不需要的参数”,您在参数之前有
Optional,使其成为可选的,因此您可以提供或不提供。 -
是的,但是如果我将我的函数定义为 FormatFunction(optional HorizAlign, optional VertAlign, optional InterColour) 并且我只想使用颜色参数,我必须将其编写为
vbaFormatFunction( , , RGB(whatever))或 @987654327 @ ...在我看来,两者都比标准 VBA 格式效率低:/
标签: excel vba optimization formatting