在表格中运行macros

enter image description here我想运行一个macros,将我的背景颜色设置为表格中的一行的绿色,但是如果有两个相邻的表格,则第二个表格也会以绿色显示该行:

在这里输入图像说明

有没有办法只获取第一个表中的背景颜色,但没有命名表。 (例如,不要命名“Set Table1 row color green”)。 这是我正在使用的代码:

Sub TEST() Set LeftCell = Cells(ActiveCell.Row, 1) Set RightCell = Cells(ActiveCell.Row, Columns.Count) If IsEmpty(LeftCell) Then Set LeftCell = LeftCell.End(xlToRight) If IsEmpty(RightCell) Then Set RightCell = RightCell.End(xlToLeft) If LeftCell.Column = Columns.Count And RightCell.Column = 1 Then ActiveCell.Select Else Range(LeftCell, RightCell).Select End If Selection.Interior.Color = RGB(146, 208, 80) End Sub 

谢谢Roxana

在这里input图像说明

请给这个尝试…

 Sub FillRowColor() Dim tbl As ListObject Dim tblName Dim cell As Range Set cell = ActiveCell On Error Resume Next tblName = cell.ListObject.Name On Error GoTo 0 If tblName <> "" Then Set tbl = ActiveCell.ListObject Set tbl = ActiveSheet.ListObjects(1) tbl.Range.Interior.ColorIndex = xlNone tbl.Range.Rows(ActiveCell.Row).Interior.Color = RGB(146, 208, 80) Else MsgBox "ActiveCell is not in the table.", vbCritical End If End Sub