gpt4 book ai didi

excel - 如何在表格中添加一行并替换行颜色?

转载 作者:行者123 更新时间:2023-12-04 21:31:15 26 4
gpt4 key购买 nike

我想在表格末尾添加一行并自动交替行颜色。我在这里有代码,但是行没有添加到表中,而是添加到了事件表中。我不知道如何正确遍历表中的行。

Private Sub CommandButton2_Click()

Dim lr As Long
lr = Cells(Rows.Count, 1).End(xlUp).row

Rows(lr + 1).Insert

Dim lrow As Excel.ListRow
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("WorkZone")

For Each lrow In tbl.ListRows

If Cells.Interior.Color = RGB(0, 0, 0) And Cells.row Mod 2 = 0 Then

Cells.Interior.Color = RGB(242, 242, 242)

End If

Next lrow

End Sub

最佳答案

Sub AlternateRows()
Dim lst As ListObject, lstRow As ListRow
Set lst = Sheets("Sheet1").ListObjects("Table1")
lst.ListRows.Add '//Add row
For Each lstRow In lst.ListRows
lstRow.Range.Interior.Color = _
IIf(lstRow.Index Mod 2 = 0, vbRed, vbGreen) '//Color rows
Next
End Sub

关于excel - 如何在表格中添加一行并替换行颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52040371/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com