Excel - Precionar tecla Enter para commandButton

 
Vista:
Imágen de perfil de JoaoM
Val: 175
Ha disminuido su posición en 2 puestos en Excel (en relación al último mes)
Gráfica de Excel

Precionar tecla Enter para commandButton

Publicado por JoaoM (222 intervenciones) el 27/02/2017 19:52:12
Tengo formulario con varios TextBox (12) que tengo que llenar o editar algún dato y luego pisar botón Insertar Nuevo o Validar Edicion según el caso

Quiero un evento en que después de llenar o editar todo lo que tengo que llenar, estando el foco en determinado Control (TextBox) piso Enter y se ejecuta el botón Insertar Nuevo (CommandButton1) o Validar Edición (CommandButton2) al tratarse de Edición.

El código para el botón Insertar es este
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Private Sub CommandButton1_Click() 'Saving Button
Dim sonsat As Long
 
If TextBox1.Value = "" Then
        MsgBox "Por favor ingrese 1º Nombre.", vbExclamation
        TextBox1.SetFocus
        Exit Sub
    End If
    If TextBox2.Value = "" Then
        MsgBox "Por favor ingrese Nombre de Compañia.", vbExclamation
        TextBox2.SetFocus
        Exit Sub
    End If
    If TextBox3.Value = "" Then
        MsgBox "Please enter an Adress.", vbExclamation
        TextBox3.SetFocus
        Exit Sub
    End If
If TextBox10.Value = "" Then
        MsgBox "Please enter an Email.", vbExclamation
        TextBox10.SetFocus
        Exit Sub
    End If
    If TextBox12.Value = "" Then
        MsgBox "Please enter Estimated Revenue.", vbExclamation
        TextBox12.SetFocus
        Exit Sub
    End If
 
    If Not IsNumeric(TextBox12.Text) Then
        MsgBox "Please enter a Numeric Value.", vbExclamation
        TextBox12.SetFocus
        Exit Sub
    End If
sonsat = Sheets("Data").[a65536].End(3).row + 1
Call Main 'Progress Bar
 
Cells(sonsat, 1) = TextBox1
Cells(sonsat, 2) = TextBox2
Cells(sonsat, 3) = TextBox3
Cells(sonsat, 4) = TextBox4
Cells(sonsat, 5) = TextBox5
Cells(sonsat, 6) = TextBox6
Cells(sonsat, 7) = TextBox7
Cells(sonsat, 8) = TextBox8
Cells(sonsat, 9) = TextBox9
Cells(sonsat, 10) = TextBox10
Cells(sonsat, 11) = TextBox11
Cells(sonsat, 12) = TextBox12
 
MsgBox "Registration is successful"
ListBox1.List = Sheets("Data").Range("a2:l" & [a65536].End(3).row).Value 'For refresh listbox
TextBox14.Value = ListBox1.ListCount
End Sub

Y para la Edición es este
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Private Sub CommandButton2_Click() 'Change Button
Dim sonsat As Long
 
If ListBox1.ListIndex = -1 Then
MsgBox "Choose an item", vbExclamation
Exit Sub
End If
Sheets("Data").Range("A:A").Find(What:=ListBox1.Value, LookIn:=xlValues, LookAt:=xlWhole).Activate
sonsat = ActiveCell.row
 
Cells(sonsat, 1) = TextBox1.Text
Cells(sonsat, 2) = TextBox2.Text
Cells(sonsat, 3) = TextBox3.Text
Cells(sonsat, 4) = TextBox4.Text
Cells(sonsat, 5) = TextBox5.Text
Cells(sonsat, 6) = TextBox6.Text
Cells(sonsat, 7) = TextBox7.Text
Cells(sonsat, 8) = TextBox8.Text
Cells(sonsat, 9) = TextBox9.Text
Cells(sonsat, 10) = TextBox10.Text
Cells(sonsat, 11) = TextBox11.Text
Cells(sonsat, 12) = TextBox12.Text
 
Call Main 'Progress Bar
MsgBox "Item has been updated"
ListBox1.List = Sheets("Data").Range("a2:l" & [a65536].End(3).row).Value 'For refresh listbox
End Sub

Conseguí este código pero realmente no se como hacer referente al CommandButton visto que está con TextBox1
1
2
3
4
5
6
Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        Select Case e.KeyData
            Case Keys.Enter
                'Aqui tu codigo a ejecutar
        End Select
    End Sub

Gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder