在表内运行宏

时间:2017-11-09 15:04:05

标签: excel vba excel-vba

enter image description here我想运行一个宏,将表背景颜色设置为绿色,但是如果有两个表彼此相邻,第二个表也会获得绿色行: / p>

enter image description here

有没有办法只在第一张表中获取背景颜色,但没有命名表格。 (例如,不命名“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的

enter image description here

1 个答案:

答案 0 :(得分:0)

请试一试......

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