如何将 Table 样式应用于当前选择

How to apply Table Style to current selection

我在创建宏时得到了 doubt/problem 和 VBA。 基本上我想对我当前的选择应用 Table“样式”,如下所示:

Range("A1:J1").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False

我录制的宏然后使用以下代码应用 Table:

 ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A:$J9"), , xlYes).Name = _
    "Table4"
ActiveSheet.ListObjects("Table4").TableStyle = "TableStyleLight15"
With Selection
    .HorizontalAlignment = xlGeneral
    .VerticalAlignment = xlCenter
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
With Selection
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With

现在我关心的是真正的第一行,Range("$A$1:$J$319"),这显然是我用前三行代码选择的区间。有没有办法让这个范围变成这样?

ActiveSheet.ListObjects.Add(xlSrcRange, Range("Selection"), , xlYes).Name = _
    "Table4"

我想要这样的东西,因为我的间隔将来可能会改变(减少或增加 50 行),我不希望 table 应用于空行或更多行...

希望我解释清楚了!谢谢!

Range.CurrentRegion 可用于引用所有连接的单元格(a.k.a。连续的单元格)。

最好设置对 ListObject 的引用,因为它有许多内置属性,允许我们引用它的不同部分。

Dim ListObject As ListObject
Set ListObject = ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes)
With ListObject
    .Name = "Table4"
    .TableStyle = "TableStyleLight15"
End With

With ListObject.Range
    .HorizontalAlignment = xlGeneral
    .VerticalAlignment = xlCenter
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With

我会使用一个函数来添加列表对象。

您将未来 table 的左上角传递给函数 - 例如activesheet.range("A1").

该函数然后使用 - 作为 TinMan - 解释 CurrentRegion 添加列表对象。

如果您将名称和/或 TableStyle 传递给函数,它们将被应用。 如果 TableName 已经存在 - 将在名称中添加一个计数器。

如果样式不存在,错误将被忽略。

Public Function addListobject(rgTopLeftCorner As Range, _
    Optional TableName As String, Optional TableStyle As String) As ListObject
    
Dim ws As Worksheet
Set ws = rgTopLeftCorner.Parent

Dim lo As ListObject
Set lo = ws.ListObjects.Add(xlSrcRange, rgTopLeftCorner.CurrentRegion)

With lo
    
    If LenB(TableName) > 0 Then
        On Error Resume Next
        lo.Name = TableName
        If Err <> 0 Then
            'this will handle the case that supplied TableName is already in use
            Dim i As Long, TableNameNew As String
            Do
                Err.Clear
                i = i + 1
                TableNameNew = TableName & "_" & i
                lo.Name = TableNameNew
            Loop Until Err = 0
        End If
        On Error GoTo 0
    End If
    

    If LenB(TableStyle) > 0 Then
        On Error Resume Next    'in case style does not exist
        lo.TableStyle = TableStyle
        On Error GoTo 0
    End If

End With

Set addListobject = lo
 
End Function

你可以这样调用这个函数:

Sub test()

Dim lo As ListObject
Set lo = addListobject(Selection, "test")
Debug.Print lo.Name

Set lo = addListobject(Selection.Offset(, 5), "test")
Debug.Print lo.Name 'will return test_1

Set lo = addListobject(Selection.Offset(, 10), "test")
Debug.Print lo.Name 'will return test_2

End Sub

我很确定你不需要

With ListObject.Range
    .HorizontalAlignment = xlGeneral
    ...

部分 - 特别是。如果您设置 table 样式。这大概是宏录制的人工制品......