我有一张表格,为列表中的每个项目添加一个表格 但是在添加表格后,我无法找到编辑表格标题的代码。 可能有一个简单的解决方案,但我无法找到一个。
Sub getAllProjectsTest()
Dim totalSheet As Worksheet
Set totalSheet = ThisWorkbook.Sheets("Total Overview")
totalSheet.Range("A2:Z4000").Clear
For Each project In Sheets("Projects-Tasks").Range("Projects")
Dim LastRow As Long
LastRow = totalSheet.Cells(Rows.Count, 1).End(xlUp).Row
With totalSheet.Range("A" & LastRow).Offset(3, 0)
.Value = project
.Font.Size = 16
.Font.Bold = True
End With
Dim ListStart As String
ListStart = totalSheet.Range("A" & LastRow).Offset(4, 0).Address
Dim TableWidth As String
TableWidth = totalSheet.Range("A" & LastRow).Offset(4, 3).Address
totalSheet.ListObjects.Add(xlSrcRange, Range(ListStart & ":" & TableWidth), , xlYes).Name = "Table" & project
Range(ListStart & ":" & TableWidth).HorizontalAlignment = xlCenter
Next project
End Sub
答案 0 :(得分:1)
如果使用对象会变得更容易。见这个例子
'
'~~> Rest of the code
'
Dim tbl As ListObject
Set tbl = totalSheet.ListObjects.Add(xlSrcRange, _
Range(ListStart & ":" & TableWidth), , xlYes)
With tbl
.Name = "Table" & project
Debug.Print .HeaderRowRange.Address
End With
'
'~~> Rest of the code
'
答案 1 :(得分:0)
另一种更有趣的方法。
Sub trimHeaders()
Dim wlistobj As ListObject
Dim wlistcol As ListColumn
Set wlistobj = ThisWorkbook.Sheets(1).ListObjects(1)
For Each wlistcol In wlistobj.ListColumns
wlistobj.HeaderRowRange.Cells(wlistcol.DataBodyRange.Column) = Trim(wlistobj.HeaderRowRange.Cells(wlistcol.DataBodyRange.Column))
Next wlistcol
End Sub