Como hacer que mi macro se ejecute solo en una hoja específica?

Tengo un excel que en la hoja 1 llamada "Ingreso" tiene un formulario que con ayuda de una macro lo pasa a la hoja 2 llamada "Base" para su almacenamiento.

En la hoja "Ingreso" hay celdas específicas para ingresar datos, por lo que con ayuda de macros hice que la tecla Enter y Tab funcionen para despazarse solo por las celdas de ingreso.

El problema es que en la Hoja "Base" la Tecla Enter y Tab ejecutan la macro y no cumplen su funcion normal.

Espero puedan ayudarme.

Esta es la macro que tengo:

Sub tecla()
Application.OnKey "{TAB}", "tabulador"
Application.OnKey "~", "intro"
End Sub

Sub tabulador()
On Error Resume Next
If ActiveCell.Address = "$G$16" Then
ActiveCell.Offset(-3, 0).Select
Exit Sub
End If
If ActiveCell.Address = "$G$13" Then
ActiveCell.Offset(-3, 0).Select
Exit Sub
End If
If ActiveCell.Address = "$G$10" Then
ActiveCell.Offset(-3, 0).Select
Exit Sub
End If
If ActiveCell.Address = "$G$7" Then
ActiveCell.Offset(-3, 0).Select
Exit Sub
End If
If ActiveCell.Address = "$G$4" Then
ActiveCell.Offset(12, -4).Select
Exit Sub
End If
If ActiveCell.Address = "$C$16" Then
ActiveCell.Offset(-3, 0).Select
Exit Sub
End If
If ActiveCell.Address = "$C$13" Then
ActiveCell.Offset(0, 2).Select
Exit Sub
End If
If ActiveCell.Address = "$E$13" Then
ActiveCell.Offset(-3, 0).Select
Exit Sub
End If
If ActiveCell.Address = "$E$10" Then
ActiveCell.Offset(0, -2).Select
Exit Sub
End If
If ActiveCell.Address = "$C$10" Then
ActiveCell.Offset(-3, 0).Select
Exit Sub
End If
If ActiveCell.Address = "$C$7" Then
ActiveCell.Offset(-3, 0).Select
Exit Sub
End If
End Sub

Sub intro()
On Error Resume Next
If ActiveCell.Address = "$C$4" Then
ActiveCell.Offset(3, 0).Select
Exit Sub
End If
If ActiveCell.Address = "$C$7" Then
ActiveCell.Offset(3, 0).Select
Exit Sub
End If
If ActiveCell.Address = "$C$10" Then
ActiveCell.Offset(0, 2).Select
Exit Sub
End If
If ActiveCell.Address = "$E$10" Then
ActiveCell.Offset(3, -2).Select
Exit Sub
End If
If ActiveCell.Address = "$C$13" Then
ActiveCell.Offset(0, 2).Select
Exit Sub
End If
If ActiveCell.Address = "$E$13" Then
ActiveCell.Offset(3, -2).Select
Exit Sub
End If
If ActiveCell.Address = "$C$16" Then
ActiveCell.Offset(-12, 2).Select
Exit Sub
End If
If ActiveCell.Address = "$G$4" Then
ActiveCell.Offset(3, 0).Select
Exit Sub
End If
If ActiveCell.Address = "$G$7" Then
ActiveCell.Offset(3, 0).Select
Exit Sub
End If
If ActiveCell.Address = "$G$10" Then
ActiveCell.Offset(3, 0).Select
Exit Sub
End If
If ActiveCell.Address = "$G$13" Then
ActiveCell.Offset(3, 0).Select
Exit Sub
End If
End Sub

Respuesta
1

Coloca al principio de las macros esta instrucción:

If ActiveSheet.Name <> "Ingreso" Then Exit Sub

Esto hace que cuando se vayan a ejecutar estas macros, si la hoja activa, no es Ingreso, sale de la macro y no hace nada.

Si te ha valido la respuesta.

1 respuesta más de otro experto

Respuesta
1

Agrega la siguiente macro a tu código

Sub tecla2()
    Application.OnKey "{TAB}"
    Application.OnKey "~"
End Sub

Te debe quedar algo así:

Sub tecla()
    Application.OnKey "{TAB}", "tabulador"
    Application.OnKey "~", "intro"
End Sub
Sub tecla2()
    Application.OnKey "{TAB}"
    Application.OnKey "~"
End Sub
Sub tabulador()
    On Error Resume Next
    If ActiveCell.Address = "$G$16" Then
    ActiveCell.Offset(-3, 0).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$G$13" Then
    ActiveCell.Offset(-3, 0).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$G$10" Then
    ActiveCell.Offset(-3, 0).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$G$7" Then
    ActiveCell.Offset(-3, 0).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$G$4" Then
    ActiveCell.Offset(12, -4).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$C$16" Then
    ActiveCell.Offset(-3, 0).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$C$13" Then
    ActiveCell.Offset(0, 2).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$E$13" Then
    ActiveCell.Offset(-3, 0).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$E$10" Then
    ActiveCell.Offset(0, -2).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$C$10" Then
    ActiveCell.Offset(-3, 0).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$C$7" Then
    ActiveCell.Offset(-3, 0).Select
    Exit Sub
    End If
End Sub
Sub intro()
    On Error Resume Next
    If ActiveCell.Address = "$C$4" Then
    ActiveCell.Offset(3, 0).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$C$7" Then
    ActiveCell.Offset(3, 0).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$C$10" Then
    ActiveCell.Offset(0, 2).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$E$10" Then
    ActiveCell.Offset(3, -2).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$C$13" Then
    ActiveCell.Offset(0, 2).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$E$13" Then
    ActiveCell.Offset(3, -2).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$C$16" Then
    ActiveCell.Offset(-12, 2).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$G$4" Then
    ActiveCell.Offset(3, 0).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$G$7" Then
    ActiveCell.Offset(3, 0).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$G$10" Then
    ActiveCell.Offset(3, 0).Select
    Exit Sub
    End If
    If ActiveCell.Address = "$G$13" Then
    ActiveCell.Offset(3, 0).Select
    Exit Sub
    End If
End Sub

Ahora, en los eventos de Thisworkbook agrega este código:

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
'Por.Dante Amor
    If Sh.Name = "Ingreso" Then
        Call tecla
    Else
        Call tecla2
    End If
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas