我创建了几个表(大约200个),这些表是从用户输入的下拉列表中生成的。它有效,但很难看。我虽然根据表的大小应用条件格式。
我想要的是这样的:
如下所示:
知道我也有一些看起来像这样的表:
有没有办法做到这一点?
您可以通过遍历工作表(或工作簿)中的ListObjects并应用选择的边框格式来迭代遍历表(通过ApplyStyle函数中的选择案例格式化不同的方式)。要求您将表设置为可识别为列表对象。
我的代码:
Option Explicit
Public Sub TEST()
Dim edgesArr()
edgesArr = Array(xlEdgeLeft, xlEdgeTop, xlEdgeBottom, xlEdgeRight, xlInsideVertical, xlInsideHorizontal)
With ActiveSheet
Dim tbl As ListObject
For Each tbl In .ListObjects
FormatTable edgesArr, tbl
Next tbl
End With
End Sub
Public Sub FormatTable(ByVal edgesArr As Variant, ByRef tbl As ListObject)
Dim currEdge As Long
With tbl.Range
For currEdge = LBound(edgesArr) To UBound(edgesArr)
ApplyStyle CStr(edgesArr(currEdge)), .Borders(edgesArr(currEdge))
Next currEdge
End With
End Sub
Public Sub ApplyStyle(ByVal currEdgeText As String, ByVal border As Object)
With border
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
Select Case currEdgeText
Case xlInsideVertical, xlInsideHorizontal
.Weight = xlMedium
Case Else
.Weight = xlThick
End Select
End With
End Sub
您可以将当前范围转换为具有语法的表,例如下面的example:
Dim src As Range
Dim ws As Worksheet
Set src = Range("B5").CurrentRegion
Set ws = ActiveSheet
ws.ListObjects.Add( SourceType:=xlSrcRange, Source:=src, _
xlListObjectHasHeaders:=xlYes, tablestyleName:="TableStyleMedium28").Name = "Sales_Table"