根据单击按钮为每行着色

时间:2019-04-29 10:48:07

标签: excel vba

每行都有一堆按钮。 (范围A1:A200) 我想单击每个人,然后他将在整行(范围为A:Q)中执行所需的操作(例如,颜色)。 目前,我的代码设置方式可能需要对btn_click函数进行更改并触发。

'This creates buttons for the selected range

    Range("A2:A200").Select
    Dim btn As Button
    Application.ScreenUpdating = False
    ActiveSheet.Buttons.Delete
    Dim t As Range
    ' Find the First & Last Row number of selection
    Dim x As Long, y As Long
    x = Selection.Rows(1).Row
    y = Selection.Rows.Count + x - 1

    For i = x To y ' Loop from first row to last row
       Set t = ActiveSheet.Range(Cells(i, 1), Cells(i, 1))
       Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
       With btn
         .OnAction = "btn_Click"
         .Caption = "LineBreak "
         .Name = "Line Break "
       End With'
    Next i

    Application.ScreenUpdating = True
End Sub

Sub btn_Click()

    '//CODE NEEDS TO GO IN HERE

End Sub

1 个答案:

答案 0 :(得分:1)

使用“表单”按钮非常简单:

Sub btn_Click()
    Intersect(ActiveSheet.Shapes(Application.Caller).TopLeftCell.EntireRow, Range("A:Q")).Interior.Color = vbRed
End Sub