Erreur 1004 avec ActiveCell

Winniechips

XLDnaute Nouveau
Bonjour au Forum,

Ma feuille excel contient un tableau avec des dates. Je souhaite soustraire chaque date contenue dans mon tableau avec la date du jour B14. Si le résultat (nombre de jours) est compris entre certaines valeurs, la macro m'enverra un mail (c'est pour by-passer la mise en forme conditionnelle).


Dans le code VBA, J'ai un message d'erreur "1004 définie par l'application ou par l'objet" lorsque j'arrive sur la ligne resultat=... :

VB:
Dim resultat As Integer

For i = 3 To 12
        For j = 5 To 35
            
            If Not IsEmpty(ActiveCell) Then
                resultat = DateDiff("d", ActiveCell.Value, Range(B14).Value)
                Select Case resultat
                ...

Je me pose la question de savoir si l'erreur ne viendrait pas de ActiveCell.Value qui ne prendrait pas en compte que :

ActiveCell correspond à la cellule (i,j)
ActiveCell.Value est une date
resultat est un nombre de jours

Je vous remercie du temps que vous prendrez à m'aiguiller sur cette problématique.

Cordialement,
Winniechips
 

camarchepas

XLDnaute Barbatruc
Re : Erreur 1004 avec ActiveCell

Bonjour ,

et bien pas de problème avec l'activecell ,

par contre :

Sub ff()
Dim Resultat As String
If Not IsEmpty(ActiveCell) And IsDate(ActiveCell) Then
Resultat = DateDiff("d", ActiveCell.Value, Range("B14").Value)
End If
End Sub
 

Paf

XLDnaute Barbatruc
Re : Erreur 1004 avec ActiveCell

Bonjour Winniechips et bonjour camarchepas


le but étant de comparer chaque cellule de la plage E3:AG12 avec B14, il faudrait que chaque cellule de la plage soit successivement sélectionnée pour pouvoir utiliser ActiveCell.

la première solution, pour modifier le moins possible le code existant, est de rajouter juste après la boucle For j=..., la selection de chaque cellule
Code:
Cells(i,j).select

une autre solution (moins gourmande en temps d'exécution) consiste à ne pas utiliser ActiveCell :
remplacer alors chaque ActiveCell par Cells( i, j )

Bonne soirée
 

Winniechips

XLDnaute Nouveau
Re : Erreur 1004 avec ActiveCell

Bonjour CamarchePas, Bonjour Paf, Bonjour Le Forum,

Merci pour vos solutions.
Le remplacement d'ActiveCell par Cells(i,j), j'avais cette solution auparavant et j'avais erreur sur erreur, c'est pour cela que j'ai utilisé ActiveCell. Merci tout de même pour l'idée.

Du coup, j'ai ajouté IsDate(ActiveCell) et modifié la déclaration de mon résultat en String : plus d'erreur 1004.

Par contre, je me retrouve avec une nouvelle erreur dans la déclinaison de mes 3 cas : j'ai une "erreur 91 variable objet ou variable de bloc With non définie" sur la ligne "Set oBjMail = oBjOl.CreateItem(olMailItem)". J'ai vérifié mon code et pourtant, j'ai bien mes End With.

VB:
Public Sub Envoi_Mail_Alerte_Habilitation()
Dim i As Integer, j As Integer, objOutlook As Outlook.Application
Dim Resultat As String
Dim oBjMail As Outlook.MailItem
Dim oBjOl As Outlook.Application
Dim olMailItem As MailItem

Set objOutlook = CreateObject("Outlook.Application")

.......

 Select Case Resultat
                Case 90 To 180
                         Set oBjMail = oBjOl.CreateItem(olMailItem)
                         With oBjMail
                             .To = "monemail@blablabla.fr"
                             .Subject = "Echéance Habilitations du Personnel"
                             .Body = "Attention la date de validité d'une formation ou habilitation d'un salarié est comprise entre 3 et 6 Mois"
                             .Send
                        
                         End With

                         Set oBjMail = Nothing

          
                Case 0 To 90
                        Set oBjMail = oBjOl.CreateItem(olMailItem)
                        With oBjMail
                            .To = "monemail@blablabla.fr"
                            .Subject = "Echéance Habilitations du Personnel"
                            .Body = "Attention, la date de validité d'une formation ou habilitation d'un salarié est inférieure à 3 Mois."
                            .Send
                       
                        End With

                        Set oBjMail = Nothing

        
                Case Is <= 0
                         Set oBjMail = oBjOl.CreateItem(olMailItem)
                         With oBjMail
                             .To = "monemail@blablabla.fr"
                             .Subject = "Echéance Habilitations du Personnel"
                             .Body = "Attention, la date de validité d'une formation ou habilitation d'un salarié est dépassée."
                             .Send
                        
                         End With

                         Set oBjMail = Nothing
               
                End Select

.....
objOutlook.Quit
Set objOutlook = Nothing
End Sub

Qu'en pensez-vous?
Merci par avance.
 

gosselien

XLDnaute Barbatruc
Re : Erreur 1004 avec ActiveCell

Bonjour,

je n'ai pas testé ton code mais un en dehors de ta question:
case 0 to 90 et 90 to 180 ne doit pas fonctionner ... comme il choisi si c'est justement 90 ?
ce ne serait pas plus juste de mettre 0 to 89 ? ou je me trompe ?

P.
 

camarchepas

XLDnaute Barbatruc
Re : Erreur 1004 avec ActiveCell

Bonjour Gosselien, Winnie,

Voici le code rectifié en fonction des remarque de Gosselien et des autres problèmes :

Code:
Public Sub Envoi_Mail_Alerte_Habilitation()
 Dim i As Integer, j As Integer, objOutlook As Outlook.Application
 Dim Resultat As String
 Dim oBjMail As Outlook.MailItem
 Dim oBjOl As Outlook.Application


Set oBjOl = CreateObject("Outlook.Application")

'.......

 
 Select Case Resultat
                 Case 91 To 180
                          Set oBjMail = oBjOl.CreateItem(0)
                          With oBjMail
                              .To = "monemail@blablabla.fr"
                              .Subject = "Echéance Habilitations du Personnel"
                              .Body = "Attention la date de validité d'une formation ou habilitation d'un salarié est comprise entre 3 et 6 Mois"
                              .Send
                         
                         End With
 
                         Set oBjMail = Nothing
 
          
                Case 1 To 90
                             Set oBjMail = oBjOl.CreateItem(0)
                         With oBjMail
                             .To = "monemail@blablabla.fr"
                             .Subject = "Echéance Habilitations du Personnel"
                             .Body = "Attention, la date de validité d'une formation ou habilitation d'un salarié est inférieure à 3 Mois."
                             .Send
                        
                         End With
 
                        Set oBjMail = Nothing
 
        
                Case Is <= 0
                          Set oBjMail = oBjOl.CreateItem(0)
                          With oBjMail
                              .To = "monemail@blablabla.fr"
                              .Subject = "Echéance Habilitations du Personnel"
                              .Body = "Attention, la date de validité d'une formation ou habilitation d'un salarié est dépassée."
                              .Send
                         
                         End With
 
                         Set oBjMail = Nothing
                
                 End Select
 
'.....
 objOutlook.Quit
 Set objOutlook = Nothing
 End Sub
 

Winniechips

XLDnaute Nouveau
Re : Erreur 1004 avec ActiveCell

Re- bonjour Camarchepas,

J'ai toujours la même erreur sur la ligne Set oBjMail = oBjOl.CreateItem(0) après avoir réalisé les modifications sur oBjOl.CreateItem(0).
Mais c'est sur le dernier cas :
VB:
Case Is <= 0
                         Set oBjMail = oBjOl.CreateItem(0)
                         With oBjMail
                             .To = "monemail@blablabla.fr"
                             .Subject = "Echéance Habilitations du Personnel"
                             .Body = "Attention, la date de validité d'une formation ou habilitation d'un salarié est dépassée."
                             .Send
                        
                         End With
                         Set oBjMail = Nothing
               
                End Select
 

camarchepas

XLDnaute Barbatruc
Re : Erreur 1004 avec ActiveCell

re ,

a nouveau testé comme ceci ,

et pas d'anomalie ???

Code:
Sub hh()
 Dim i As Integer, j As Integer, objOutlook As Outlook.Application
 Dim Resultat As String
 Dim oBjMail As Outlook.MailItem
 Dim oBjOl As Outlook.Application


Set oBjOl = CreateObject("Outlook.Application")

'.......
Resultat = "-4"
 
 Select Case Resultat

Case Is <= 0
                          Set oBjMail = oBjOl.CreateItem(0)
                          With oBjMail
                              .To = "monemail@blablabla.fr"
                              .Subject = "Echéance Habilitations du Personnel"
                              .Body = "Attention, la date de validité d'une formation ou habilitation d'un salarié est dépassée."
                              .Send
                         
                         End With
                          Set oBjMail = Nothing
                
                 End Select

End Sub
 

Winniechips

XLDnaute Nouveau
Re : Erreur 1004 avec ActiveCell

par contre je pense avoir un autre problème également.
Le test avec la msgbox, j'ai pu le faire en sélectionnant manuellement la cellule avec la souris et non en utilisant les boucles For i et For j.


VB:
Public Sub Envoi_Mail_Alerte_Habilitation()
 Dim i As Integer, j As Integer, objOutlook As Outlook.Application
 Dim Resultat As String
 Dim oBjMail As Outlook.MailItem
 Dim oBjOl As Outlook.Application


Set oBjOl = CreateObject("Outlook.Application")

For i = 3 To 12
        For j = 5 To 35
            
            If Not IsEmpty(ActiveCell) And IsDate(ActiveCell) Then
                Resultat = DateDiff("d", ActiveCell.Value, Range("B14").Value)
                End If
                
                MsgBox Resultat
 
 Select Case Resultat
                 Case 91 To 180
                          Set oBjMail = oBjOl.CreateItem(0)
                          With oBjMail
                              .To = "monemail@blablabla.fr"
                              .Subject = "Echéance Habilitations du Personnel"
                              .Body = "Attention la date de validité d'une formation ou habilitation d'un salarié est comprise entre 3 et 6 Mois"
                              .Send
                         
                         End With
 
                         Set oBjMail = Nothing
 
         
                Case 1 To 90
                             Set oBjMail = oBjOl.CreateItem(0)
                         With oBjMail
                             .To = "monemail@blablabla.fr"
                             .Subject = "Echéance Habilitations du Personnel"
                             .Body = "Attention, la date de validité d'une formation ou habilitation d'un salarié est inférieure à 3 Mois."
                             .Send
                       
                         End With
 
                        Set oBjMail = Nothing
 
       
                Case Is <= 0
                          Set oBjMail = oBjOl.CreateItem(0)
                          With oBjMail
                              .To = "monemail@blablabla.fr"
                              .Subject = "Echéance Habilitations du Personnel"
                              .Body = "Attention, la date de validité d'une formation ou habilitation d'un salarié est dépassée."
                              .Send
                         
                         End With
 
                         Set oBjMail = Nothing
               
                 End Select
     
        Next j
Next i
 

objOutlook.Quit
 Set objOutlook = Nothing
 End Sub
 

Discussions similaires

Statistiques des forums

Discussions
312 550
Messages
2 089 514
Membres
104 199
dernier inscrit
SimonDtx