当某个单元格的值=" 1"时,按键组合键(例如Ctrl + Alt + x)会自动按下。

时间:2017-02-16 12:08:29

标签: excel vba

我希望计算机在excel工作表中某个单元格(例如A1)的值更改为某个值后立即自动按某个组合键(例如Ctrl + Alt + x)(例如& #34; 1&#34)。 我在互联网上搜索了几个小时,但我找不到与我的情况完全相同的信息。请多多帮助和感谢。

1 个答案:

答案 0 :(得分:1)

我很确定你不需要输入一个组合键,但如果你真的想要它是SendKeys。 此代码将执行许多操作,具体取决于使用SendKeys命令完成的单元格A1中输入的值:

'Fires when you change a value on the sheet.
Private Sub Worksheet_Change(ByVal Target As Range)

    'Was only a single cell selected?
    If Target.Cells.Count = 1 Then
        'Was the value changed (Target) in range A1?
        If Not Intersect(Target, Range("A1")) Is Nothing Then
            'Perform an action depending on Targets value.
            Select Case Target.Value
                Case 1
                    'Cut the Target cell (Ctrl+x).
                    Target.Cut
                Case 2
                    'Copy the Target cell (Ctrl+c).
                    Target.Copy
                Case 3
                    'Filldown from the Target 10 rows.
                    Target.Resize(10).FillDown
                Case 4
                    'Change the font colour to red.
                    Target.Font.Color = vbRed
                Case "Hello"
                    'Change the contents of the cell.
                    'This will cause the Change event to fire again, so disable events first.
                    Application.EnableEvents = False
                    Target.Value = "Goodbye"
                    Application.EnableEvents = True
                Case Else
                    'If anything else is press keys Right Arrow, Up Arrow, F2.
                    SendKeys "{RIGHT}{UP}{F2}"
            End Select
        End If
    End If
End Sub