按单元格值创建命令按钮

时间:2017-10-04 08:59:31

标签: excel vba cell commandbutton

我正在尝试让Excel自动创建一个命令按钮,如果某个单元格中有文本。通过运行宏录制器和谷歌搜索,这就是我被困住的地方。

Private Sub button()

If ActiveSheet.Range("C5") = Text Then

ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
    , DisplayAsIcon:=False, Left:=396.75, Top:=18.75, Width:=64.5, Height _
    :=26.25).Select
Selection.Verb Verb:=xlPrimary

End If

End Sub

1 个答案:

答案 0 :(得分:0)

首先尝试搜索基本syntax, 确保您的单元格C5具有值“Something”

  Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If ActiveSheet.Range("C5").Value = "Something" Then
            Dim button As OLEObject
            Set button = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
                       , DisplayAsIcon:=False, Left:=396.75, Top:=18.75, Width:=64.5, Height:=26.25)
        End If

    button.Object.Caption = "Complete"
    button.Name = "CommandbuttonEL1"

    End Sub