Macro no se me activa con rango de celdas

Tengo el siguiente código en mi macro:

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("K5").Value = "ERROR" Then Mandar_Correo2
End Sub

El problema es que quiero que actúe en el rango de celdas desde K5 A K34. Al intentar ponerlo no me ejecuta la macro. ¿Se os ocurre a que puede ser debido? Cabe decir que intenté sustituir ("K5) Por ("K5:K34") y ("K5" : "K34")

1 Respuesta

Respuesta
1

H o l a

prueba esto

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("K5:K34")) Is Nothing Then
'
    If UCase(Target.Value) = "ERROR" Then
    call Mandar_Correo2
    End If
'
End If
End Sub

Valora la respuesta para finalizar saludos!

Hola.

Lo he puesto tal cual y no hace nada. De hecho no actúa ni sobre K5. Alguna otra idea?

Gracias.

Un saludo

No logro entender, ¿escribes el ERROR? O es el resultado de una fórmula.

Muéstrame una imagen de tu hoja saludos!

Hola, ya conseguí que funcionase introduciéndolo para todas las celdas.

¿Me surge otra duda que es como puedo hacer para que antes de que se ejecute la función mandar correo se guarde automáticamente?

Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:0:01"), "savebook"
End Sub


Private Sub Worksheet_Change(ByVal Target As Range)
If Range("K5").Value = "ERROR" Then Mandar_Correo2
If Range("K6").Value = "ERROR" Then Mandar_Correo2
If Range("K7").Value = "ERROR" Then Mandar_Correo2
If Range("K8").Value = "ERROR" Then Mandar_Correo2
If Range("K9").Value = "ERROR" Then Mandar_Correo2
If Range("K10").Value = "ERROR" Then Mandar_Correo2
If Range("K11").Value = "ERROR" Then Mandar_Correo2
If Range("K12").Value = "ERROR" Then Mandar_Correo2
If Range("K13").Value = "ERROR" Then Mandar_Correo2
If Range("K14").Value = "ERROR" Then Mandar_Correo2
If Range("K15").Value = "ERROR" Then Mandar_Correo2
If Range("K16").Value = "ERROR" Then Mandar_Correo2
If Range("K17").Value = "ERROR" Then Mandar_Correo2
If Range("K18").Value = "ERROR" Then Mandar_Correo2
If Range("K19").Value = "ERROR" Then Mandar_Correo2
If Range("K20").Value = "ERROR" Then Mandar_Correo2
If Range("K21").Value = "ERROR" Then Mandar_Correo2
If Range("K22").Value = "ERROR" Then Mandar_Correo2
If Range("K23").Value = "ERROR" Then Mandar_Correo2
If Range("K24").Value = "ERROR" Then Mandar_Correo2
If Range("K25").Value = "ERROR" Then Mandar_Correo2
If Range("K26").Value = "ERROR" Then Mandar_Correo2
If Range("K27").Value = "ERROR" Then Mandar_Correo2
If Range("K28").Value = "ERROR" Then Mandar_Correo2
If Range("K29").Value = "ERROR" Then Mandar_Correo2
If Range("K30").Value = "ERROR" Then Mandar_Correo2
If Range("K31").Value = "ERROR" Then Mandar_Correo2
If Range("K32").Value = "ERROR" Then Mandar_Correo2
If Range("K33").Value = "ERROR" Then Mandar_Correo2
If Range("K34").Value = "ERROR" Then Mandar_Correo2
End Sub

Sub Mandar_Correo2()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With OutMail
.To = "[email protected]"
.CC = ""
.BCC = ""
.Subject = "CONTROL PRESENCIA"
.Body = "Ajunto control de presencia del mes"
.Attachments.Add ActiveWorkbook.FullName
.display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub


Private Sub GUARDAR_Click()
ThisWorkbook.Save
End Sub

Sub programarMacro()
Tiempo = Now + TimeValue("00:00:01")
Application.OnTime Tiempo, "GUARDAR_Click", , True
End Sub

Antes de enviar el correo guardar, entonces usa esto en cada linea

If Range("K5").Value = "ERROR" Then ThisWorkbook.Save: mandar_correo2

recuerda valorar saludos!

¡Gracias! 

Buenos días.

He añadido el siguiente código a mi macro para que se pueda bloquear y desbloquear con un botón. Mi problema está en que al pulsar el botón desbloquear no me pide contraseña. Como podría hacer para que estos sucediese? 

Sub PROTECCION()

Sheets("Hoja1").Select

ActiveSheet.Protect (FORMACION1)

End Sub
Sub DESPROTEGER()

Sheets("Hoja1").Select

ActiveSheet.Unprotect (FORMACION1)

End Sub

Gracias

H o l a

Sub DESPROTEGER()
'
Sheets("Hoja1").Select
'
ActiveSheet.Unprotect
End Sub

entonces en DESPROTEGER no le pongas la contraseña

Para terminar cambia la valoración saludos!

Actualmente está así y no me pide contraseña

Sub PROTECCION()

Sheets("Hoja1").Select

ActiveSheet.Protect (FORMACION1)

End Sub
Sub DESPROTEGER()
'
Sheets("Hoja1").Select
'
ActiveSheet.Unprotect
End Sub

Gracias

Es curioso que pase, haber prueba en proteger así

Sub PROTECCION()
Set h1 = Sheets("Hoja1")
'
h1.Protect "clave"
'
End Sub

y desproteger así

Sub DESPROTEGER()
'
Set h1 = Sheets("Hoja1")
'
h1.Unprotect
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas