Mostrar control al seleccionar celda

Hola expertos,

Tengo inconvenientes con una macro, quiero que al elegir la celda I4, me muestre el calendario, al elegir la celda H22 me muestre el label 1, el label 2, el commandbutton 1 y el commandbutton 2 y por ultimo al elegir la celda O22 el label 3 y el label 4.

Cuando no este elegidas de manera independiente las celdas I4, H22 y O22, quiero que las propiedades visible de los controles este false.

Tengo la siguiente sentencia, pero me genera error:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Target, [I4]) Is Nothing Then
With Calendar1
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Today
.Visible = True

If Not Intersect(Target, [H22]) Is Nothing Then
With Label1
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True

With Label2
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True
With CommandButton1
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True
With CommandButton2
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True

If Not Intersect(Target, [O22]) Is Nothing Then
With Label3
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True
With Label4
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True
Exit With
Exit Sub
Exit If
Calendar1.Visible = False
Label1.Visible = False
Label2.Visible = False
CommandButton1.Visible = False
CommandButton2.Visible = False

Label3.Visible = False
Label4.Visible = False
End Sub

Gracias por la atención prestada

1 respuesta

Respuesta
1

Cada vez que dejes una consulta 'que da error' debieras indicar en qué linea dá el error o qué tipo de error se presenta al ejecutarla, para que sea más rápida la revisión.

Viéndolo a simple vista, noto errores de escritura de los bucles With e If:

Un With se inicia y termina para un objeto. Por ej:

If Not Intersect(Target, [I4]) Is Nothing Then
With Calendar1
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Today
.Visible = True

END WITH

END IF 'Aquí debiera terminar también el IF para la celda I4 (*)
If Not Intersect(Target, [H22]) Is Nothing Then

'sigue

(*) Un IF puede contener otros IF .... pero en este caso la idea es que si es I4 muestre el calendar y Punto. Si es H22 muestre otro control .... es decir que cada IF debierta cerrarse con un END IF.

REalizá estos ajustes, y si luego queda algo que dé error, déjamela escrita nuevamente para controlarla.

Muchas gracias por tus aclaraciones, pero realizando los ajustes que me indicaste, presenta el siguiente error, "Error de compilación. End If sin bloque If", en las lineas subrayadas y en negrita:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, [I4]) Is Nothing Then
With Calendar1
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Today
.Visible = True
End With
End If
If Not Intersect(Target, [H22]) Is Nothing Then
With Label1
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True
With Label2
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True
With CommandButton1
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True
With CommandButton2
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True
End With
End If
If Not Intersect(Target, [O22]) Is Nothing Then
With Label3
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True
With Label4
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True
End With
End If
Exit Sub
Calendar1.Visible = False
Label1.Visible = False
Label2.Visible = False
CommandButton1.Visible = False
CommandButton2.Visible = False
Label3.Visible = False
Label4.Visible = False
End Sub

Gracias

Reitero: Un With se inicia y termina para un objeto.

If Not Intersect(Target, [H22]) Is Nothing Then
With Label1
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True

End With
With Label2
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True

End With
With CommandButton1
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True

End With
With CommandButton2
.Top = Target.Top - 1
.Left = Target.Left + Target.Width + 3
.Visible = True
End With
End If


Y así con el otro Target... revisa que esté todo bien escrito y pruébalo nuevamente.


Añade tu respuesta

Haz clic para o

Más respuestas relacionadas