admin管理员组文章数量:1125619
I simply need to change the font of a row after the table last row + 2rows for cells from ("A: M") but I don't know how to write that in VBA code ... I have wrote the last section in the below code but unfortunately wrong
any sugesstion please
Sub Addnewrow()
Dim lo As ListObject
Dim newRow As ListRow
Dim cpyRng As Range
Set lo = Range("Data").ListObject
Set newRow = lo.ListRows.Add
With newRow
.Range(12).FillDown
With Range(LastRow + 2)
.Font.Size = 12
.Font.Name = "Tahoma"
.Font.Bold = True
End With
End Sub
I simply need to change the font of a row after the table last row + 2rows for cells from ("A: M") but I don't know how to write that in VBA code ... I have wrote the last section in the below code but unfortunately wrong
any sugesstion please
Sub Addnewrow()
Dim lo As ListObject
Dim newRow As ListRow
Dim cpyRng As Range
Set lo = Range("Data").ListObject
Set newRow = lo.ListRows.Add
With newRow
.Range(12).FillDown
With Range(LastRow + 2)
.Font.Size = 12
.Font.Name = "Tahoma"
.Font.Bold = True
End With
End Sub
Share
Improve this question
edited 2 days ago
Mayukh Bhattacharya
27k8 gold badges29 silver badges42 bronze badges
asked 2 days ago
Ramadan MoussaRamadan Moussa
657 bronze badges
3
|
3 Answers
Reset to default 2Rather than reference the table using "whatever listobject is covering this range" (Set lo = Range("Data").ListObject
) refer to the table by name which can be found in the Table Design tab that appears when you select a cell within the table.
I'm not sure what you're doing with the .Range(12).FillDown
. If a table contains a formula it should automatically copy it down when you add a new row.
The part of the table where the data is held is called the DataBodyRange
so you can find the end of that and then offset by two more rows to get two rows beneath the table.
Public Sub AddNewRow()
'Reference the table by name rather than location.
'You can then move the table if needed without breaking the code.
Dim lo As ListObject
Set lo = ThisWorkbook.Worksheets("Sheet1").ListObjects("Table1")
'Add a new row to the table and add some data to the 12th column in the new row.
With lo.ListRows.Add
.Range(12).Value = "ABC"
End With
'Format 2 rows beneath the table.
With lo.DataBodyRange
With .Rows(.Rows.Count).Offset(2)
.Font.Size = 12
.Font.Name = "Tahoma"
.Font.Bold = True
End With
End With
End Sub
I am not sure what you are trying to achieve, but adds a row to table and then formats the cells from A to M below the table.
Dim lo As ListObject
Dim newRow As ListRow
Dim cpyRng As Range
'Add Dim lastRow
Dim lastRow As Long
Set lo = Range("Data").ListObject
Set newRow = lo.ListRows.Add
'lastRow = table's forst row + rowcount
lastRow = lo.Range.Row + lo.ListRows.Count
newRow.Range(lastRow).FillDown
With Range("A" & lastRow + 1 & ":M" & lastRow + 1)
.Font.Size = 12
.Font.Name = "Tahoma"
.Font.Bold = True
End With
Do not use nested With-clause. Also there is a missing End With in your code.
You need to specify full cell reference. LastRow only indicating row number but not cell. Try-
With Range("A"&LastRow + 2)
.Font.Size = 12
.Font.Name = "Tahoma"
.Font.Bold = True
End With
Replace column A
with your actual column.
本文标签:
版权声明:本文标题:excel - How to Define the row after Lastrow in VBA - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736663375a1946529.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
LastRow
contains a numeric value thenRange(LastRow + 2)
will equate to something likeRange(5)
, so you're trying to writeRange(5).Font.Bold = TRUE
which will return a Method 'Range' of object '_Global' failed error. UsingRows(LastRow + 2)
would work - it will apply to the whole row on whichever sheet is currently active though. – Darren Bartrup-Cook Commented 2 days agoRows
property of a Range/Worksheet (learn.microsoft.com/en-us/office/vba/api/excel.worksheet.rows) – Shrotter Commented 2 days ago