Fonction IsError()

Résolu
Bonjour,

J'ai des problèmes avec la fonction IsError(), auparavant je disposais d'une formule excel contenant ESTERREUR() et j'ai voulu la transformer en macro en utilisant IsError().
Dans la fonction j'exécute un calcul et j'aimerais que quand celui-ci ne peut pas se faire il ne se fasse pas pour qu'il n'y est pas d'erreur mais l'erreur se produit sur la fonction avec ce message : Erreur d'exécution 13 : incompatibilité de type.

If IsError(Worksheets("PIECES USINEES").Cells(i, 9).Value * Worksheets("PIECES USINEES").Cells(i, 10).Value * Worksheets("PIECES USINEES").Cells(i, 11).Value / 1000000000 * PoidsMatiere) = True Then
Worksheets("PIECES USINEES").Cells(i, 12).Value = "Données incorrectes"
Else
Worksheets("PIECES USINEES").Cells(i, 12).Value = Worksheets("PIECES USINEES").Cells(i, 9).Value * Worksheets("PIECES USINEES").Cells(i, 10).Value * Worksheets("PIECES USINEES").Cells(i, 11).Value / 1000000000 * PoidsMatiere
End If

Pouvez-vous m'aider ?

5 réponses

  1. Problème résolu comme ça :
    Sub MAJ_poids()

    ' ***********************************************************************************
    ' ********** SEQUENCE DE MAJ DES DONNEES POIDS PARTIE USINEES **********
    ' ***********************************************************************************

    ' Macro enregistrée le 04/08/2014 par ajannin

    Dim Matiere As String, Format As String
    Dim Poidsmatiere As Integer

    Application.EnableEvents = False

    If Worksheets("PIECES USINEES").Cells(9, 3).Value Like "FR" Then
    a = 2
    ElseIf Worksheets("PIECES USINEES").Cells(9, 3).Value Like "CH" Then
    a = 0
    ElseIf Worksheets("PIECES USINEES").Cells(9, 3).Value Like "CZ" Then
    a = 1
    ElseIf Worksheets("PIECES USINEES").Cells(9, 3).Value Like "EN" Then
    a = 3
    End If

    ' Désactivation de l'écran pendant le calcul
    Application.ScreenUpdating = False
    ' Activation du Sablier
    Application.Cursor = xlWait

    'passer en calcul manuel
    With Application
    .Calculation = xlManual
    .MaxChange = 0.001
    End With

    i = 115
    While Worksheets("PIECES USINEES").Cells(i, 1).Value <> 0

    Matiere = ""
    Format = ""
    Poidsmatiere = 0

    'Matiere
    d = 5 'Colonne de départ pour la recherche de l'entete de la liste
    l = 0 'Numéro de la colonne recherchée
    k = 4 'Ligne de recherche de l'entete de la liste
    If Worksheets("TABLEAU_TRADUCTION").Cells(6, 2).Value <> "" Then
    While l = 0 And (Worksheets("TABLEAU_TRADUCTION").Cells(k, d).Value <> "" Or Worksheets("TABLEAU_TRADUCTION").Cells(k, d + 1).Value <> "")
    If Worksheets("TABLEAU_TRADUCTION").Cells(k, d).Value = Worksheets("TABLEAU_TRADUCTION").Cells(6, 2).Value Then
    l = d
    End If
    d = d + 1
    Wend
    End If

    'Filtre de la liste par ordre croissant pour supprimer les cellules vides
    Worksheets("TABLEAU_TRADUCTION").Activate
    Range(Cells(k, l), Cells(k, l + 3)).Select
    selection.AutoFilter
    ActiveWorkbook.Worksheets("TABLEAU_TRADUCTION").AutoFilter.Sort.SortFields. _
    Clear
    z = Cells(k, l + a).Address
    ActiveWorkbook.Worksheets("TABLEAU_TRADUCTION").AutoFilter.Sort.SortFields.Add _
    Key:=Range(z), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
    :=xlSortNormal
    With ActiveWorkbook.Worksheets("TABLEAU_TRADUCTION").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With

    Worksheets("TABLEAU_TRADUCTION").Range(Cells(k, l), Cells(k, l + 3)).Select
    selection.AutoFilter

    'Traduction de la colonne
    If Worksheets("PIECES USINEES").Cells(i, 4).Value <> "" Then
    'Recherche de la traduction dans fichier de traduction, si pas trouvé, écriture du mot d'origine avec couleur cellule en rouge
    k = 5
    FindTranslate = False
    While (FindTranslate = False) And Worksheets("TABLEAU_TRADUCTION").Cells(k, l + a).Value <> 0
    If Worksheets("PIECES USINEES").Cells(i, 4).Value = Worksheets("TABLEAU_TRADUCTION").Cells(k, l + a).Value And Worksheets("TABLEAU_TRADUCTION").Cells(k, l + 2).Value <> "" Then
    Matiere = Worksheets("TABLEAU_TRADUCTION").Cells(k, l + 2).Value
    FindTranslate = True
    End If
    k = k + 1
    Wend
    End If

    'Format
    d = 5 'Colonne de départ pour la recherche de l'entete de la liste
    l = 0 'Numéro de la colonne recherchée
    k = 4 'Ligne de recherche de l'entete de la liste
    If Worksheets("TABLEAU_TRADUCTION").Cells(11, 2).Value <> "" Then
    While l = 0 And (Worksheets("TABLEAU_TRADUCTION").Cells(k, d).Value <> "" Or Worksheets("TABLEAU_TRADUCTION").Cells(k, d + 1).Value <> "")
    If Worksheets("TABLEAU_TRADUCTION").Cells(k, d).Value = Worksheets("TABLEAU_TRADUCTION").Cells(11, 2).Value Then
    l = d
    End If
    d = d + 1
    Wend
    End If

    'Filtre de la liste par ordre croissant pour supprimer les cellules vides
    Worksheets("TABLEAU_TRADUCTION").Activate
    Range(Cells(k, l), Cells(k, l + 3)).Select
    selection.AutoFilter
    ActiveWorkbook.Worksheets("TABLEAU_TRADUCTION").AutoFilter.Sort.SortFields. _
    Clear
    z = Cells(k, l + a).Address
    ActiveWorkbook.Worksheets("TABLEAU_TRADUCTION").AutoFilter.Sort.SortFields.Add _
    Key:=Range(z), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
    :=xlSortNormal
    With ActiveWorkbook.Worksheets("TABLEAU_TRADUCTION").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With

    Worksheets("TABLEAU_TRADUCTION").Range(Cells(k, l), Cells(k, l + 3)).Select
    selection.AutoFilter

    'Traduction de la colonne
    If Worksheets("PIECES USINEES").Cells(i, 13).Value <> "" Then
    'Recherche de la traduction dans fichier de traduction, si pas trouvé, écriture du mot d'origine avec couleur cellule en rouge
    k = 5
    FindTranslate = False
    While (FindTranslate = False) And Worksheets("TABLEAU_TRADUCTION").Cells(k, l + a).Value <> 0
    If Worksheets("PIECES USINEES").Cells(i, 13).Value = Worksheets("TABLEAU_TRADUCTION").Cells(k, l + a).Value And Worksheets("TABLEAU_TRADUCTION").Cells(k, l + 2).Value <> "" Then
    Format = Worksheets("TABLEAU_TRADUCTION").Cells(k, l + 2).Value
    FindTranslate = True
    End If
    k = k + 1
    Wend
    End If

    'PoidsMatiere
    For m = 5 To 37
    If Matiere = Worksheets("TABLEAU_TRADUCTION").Cells(m, 12).Value Then
    Poidsmatiere = Worksheets("TABLEAU_TRADUCTION").Cells(m, 14).Value
    End If
    Next m

    'Calcul du poids
    If Format = "" Or Format = "LASER" Or Format = "REPRISE" Then
    Worksheets("PIECES USINEES").Cells(i, 12).Value = ""
    ElseIf Format = "FONDERIE" Then
    Worksheets("PIECES USINEES").Cells(i, 12).Value = Worksheets("PIECES USINEES").Cells(i, 12).Value
    Else
    With Worksheets("PIECES USINEES")
    On Error GoTo erreur
    .Cells(i, 12) = .Cells(i, 9) * .Cells(i, 10) * .Cells(i, 11) / 1000000000 * Poidsmatiere
    suivant:
    End With
    End If

    On Error GoTo 0

    i = i + 1
    Wend

    Worksheets("PIECES USINEES").Activate

    'passer en calcul automatique
    With Application
    .Calculation = xlAutomatic
    .MaxChange = 0.001
    End With

    'Désactivation du sablier
    Application.Cursor = xlDefault
    'Activation de l'écran à la fin des calculs
    Application.ScreenUpdating = True

    Application.EnableEvents = True

    Exit Sub
    ' Sous programme de gestion d'erreur
    erreur:
    Worksheets("PIECES USINEES").Cells(i, 12) = "Données incorrectes"
    Resume suivant
    End Sub
    1. Solution trouvée dans un autre forum sur une question similaire à la mienne. Mais je n'ai pas compris pourquoi il y avait un problème avec ce qui était fait avant. Pourquoi doit-on faire un sous-programme de gestion d'erreurs et ne peut-on pas l'intégrer à une boucle ?
  2. Voici mon code :
    Sub MAJ_poids()

    ' ***********************************************************************************
    ' ********** SEQUENCE DE MAJ DES DONNEES POIDS PARTIE USINEES **********
    ' ***********************************************************************************

    ' Macro enregistrée le 04/08/2014 par ajannin

    Dim Matiere As String, Format As String
    Dim Poidsmatiere As Integer

    Application.EnableEvents = False

    If Worksheets("PIECES USINEES").Cells(9, 3).Value Like "FR" Then
    a = 2
    ElseIf Worksheets("PIECES USINEES").Cells(9, 3).Value Like "CH" Then
    a = 0
    ElseIf Worksheets("PIECES USINEES").Cells(9, 3).Value Like "CZ" Then
    a = 1
    ElseIf Worksheets("PIECES USINEES").Cells(9, 3).Value Like "EN" Then
    a = 3
    End If

    ' Désactivation de l'écran pendant le calcul
    Application.ScreenUpdating = False
    ' Activation du Sablier
    Application.Cursor = xlWait

    'passer en calcul manuel
    With Application
    .Calculation = xlManual
    .MaxChange = 0.001
    End With

    i = 115
    While Worksheets("PIECES USINEES").Cells(i, 1).Value <> 0

    Matiere = ""
    Format = ""
    Poidsmatiere = 0

    'Matiere
    d = 5 'Colonne de départ pour la recherche de l'entete de la liste
    l = 0 'Numéro de la colonne recherchée
    k = 4 'Ligne de recherche de l'entete de la liste
    If Worksheets("TABLEAU_TRADUCTION").Cells(6, 2).Value <> "" Then
    While l = 0 And (Worksheets("TABLEAU_TRADUCTION").Cells(k, d).Value <> "" Or Worksheets("TABLEAU_TRADUCTION").Cells(k, d + 1).Value <> "")
    If Worksheets("TABLEAU_TRADUCTION").Cells(k, d).Value = Worksheets("TABLEAU_TRADUCTION").Cells(6, 2).Value Then
    l = d
    End If
    d = d + 1
    Wend
    End If

    'Filtre de la liste par ordre croissant pour supprimer les cellules vides
    Worksheets("TABLEAU_TRADUCTION").Activate
    Range(Cells(k, l), Cells(k, l + 3)).Select
    selection.AutoFilter
    ActiveWorkbook.Worksheets("TABLEAU_TRADUCTION").AutoFilter.Sort.SortFields. _
    Clear
    z = Cells(k, l + a).Address
    ActiveWorkbook.Worksheets("TABLEAU_TRADUCTION").AutoFilter.Sort.SortFields.Add _
    Key:=Range(z), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
    :=xlSortNormal
    With ActiveWorkbook.Worksheets("TABLEAU_TRADUCTION").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With

    Worksheets("TABLEAU_TRADUCTION").Range(Cells(k, l), Cells(k, l + 3)).Select
    selection.AutoFilter

    'Traduction de la colonne
    If Worksheets("PIECES USINEES").Cells(i, 4).Value <> "" Then
    'Recherche de la traduction dans fichier de traduction, si pas trouvé, écriture du mot d'origine avec couleur cellule en rouge
    k = 5
    FindTranslate = False
    While (FindTranslate = False) And Worksheets("TABLEAU_TRADUCTION").Cells(k, l + a).Value <> 0
    If Worksheets("PIECES USINEES").Cells(i, 4).Value = Worksheets("TABLEAU_TRADUCTION").Cells(k, l + a).Value And Worksheets("TABLEAU_TRADUCTION").Cells(k, l + 2).Value <> "" Then
    Matiere = Worksheets("TABLEAU_TRADUCTION").Cells(k, l + 2).Value
    FindTranslate = True
    End If
    k = k + 1
    Wend
    End If

    'Format
    d = 5 'Colonne de départ pour la recherche de l'entete de la liste
    l = 0 'Numéro de la colonne recherchée
    k = 4 'Ligne de recherche de l'entete de la liste
    If Worksheets("TABLEAU_TRADUCTION").Cells(11, 2).Value <> "" Then
    While l = 0 And (Worksheets("TABLEAU_TRADUCTION").Cells(k, d).Value <> "" Or Worksheets("TABLEAU_TRADUCTION").Cells(k, d + 1).Value <> "")
    If Worksheets("TABLEAU_TRADUCTION").Cells(k, d).Value = Worksheets("TABLEAU_TRADUCTION").Cells(11, 2).Value Then
    l = d
    End If
    d = d + 1
    Wend
    End If

    'Filtre de la liste par ordre croissant pour supprimer les cellules vides
    Worksheets("TABLEAU_TRADUCTION").Activate
    Range(Cells(k, l), Cells(k, l + 3)).Select
    selection.AutoFilter
    ActiveWorkbook.Worksheets("TABLEAU_TRADUCTION").AutoFilter.Sort.SortFields. _
    Clear
    z = Cells(k, l + a).Address
    ActiveWorkbook.Worksheets("TABLEAU_TRADUCTION").AutoFilter.Sort.SortFields.Add _
    Key:=Range(z), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
    :=xlSortNormal
    With ActiveWorkbook.Worksheets("TABLEAU_TRADUCTION").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
    End With

    Worksheets("TABLEAU_TRADUCTION").Range(Cells(k, l), Cells(k, l + 3)).Select
    selection.AutoFilter

    'Traduction de la colonne
    If Worksheets("PIECES USINEES").Cells(i, 13).Value <> "" Then
    'Recherche de la traduction dans fichier de traduction, si pas trouvé, écriture du mot d'origine avec couleur cellule en rouge
    k = 5
    FindTranslate = False
    While (FindTranslate = False) And Worksheets("TABLEAU_TRADUCTION").Cells(k, l + a).Value <> 0
    If Worksheets("PIECES USINEES").Cells(i, 13).Value = Worksheets("TABLEAU_TRADUCTION").Cells(k, l + a).Value And Worksheets("TABLEAU_TRADUCTION").Cells(k, l + 2).Value <> "" Then
    Format = Worksheets("TABLEAU_TRADUCTION").Cells(k, l + 2).Value
    FindTranslate = True
    End If
    k = k + 1
    Wend
    End If

    'PoidsMatiere
    For m = 5 To 37
    If Matiere = Worksheets("TABLEAU_TRADUCTION").Cells(m, 12).Value Then
    Poidsmatiere = Worksheets("TABLEAU_TRADUCTION").Cells(m, 14).Value
    End If
    Next m

    'Calcul du poids
    If Format = "" Or Format = "LASER" Or Format = "REPRISE" Then
    Worksheets("PIECES USINEES").Cells(i, 12).Value = ""
    ElseIf Format = "FONDERIE" Then
    Worksheets("PIECES USINEES").Cells(i, 12).Value = Worksheets("PIECES USINEES").Cells(i, 12).Value
    Else
    With Worksheets("PIECES USINEES")
    On Error GoTo erreur
    .Cells(i, 12) = .Cells(i, 9) * .Cells(i, 10) * .Cells(i, 11) / 1000000000 * Poidsmatiere
    GoTo suite
    'gestion erreurs
    erreur:
    .Cells(i, 12) = "Données incorrectes"
    suite:
    End With
    End If

    On Error GoTo 0

    i = i + 1
    Wend

    Worksheets("PIECES USINEES").Activate

    'passer en calcul automatique
    With Application
    .Calculation = xlAutomatic
    .MaxChange = 0.001
    End With

    'Désactivation du sablier
    Application.Cursor = xlDefault
    'Activation de l'écran à la fin des calculs
    Application.ScreenUpdating = True

    Application.EnableEvents = True

    End Sub
    1. tu pourrais essayer ça pour voir ? (il faudra verifier l'emplacement des paranthèses)

      Dim a As Long
      a = 1000000000
      
      With ThisWorkbook.Worksheets("PIECE USINEES")
          Cells(i, 12).Value = "=" & .Cells(i, 9).adress & "*" & .Cells(i, 10).adress & "*" & .Cells(i, 11).adress & "/(" & a & "*" & PoidMatiere & ")"
          If IsError(.Cells(i, 12)) = True Then .Cells(i, 12).Value = "donnés incorrectes"
      End With
      
      1. Aucun changement, le problème se produit sur cette ligne : Cells(i, 12).Value = "=" & .Cells(i, 9).adress & "*" & .Cells(i, 10).adress & "*" & .Cells(i, 11).adress & "/(" & a & "*" & PoidMatiere & ")"
      2. tu as déclaré comment PoidMatière ?
      3. Integer mais le problème vient de la case i,9 qui contient quelque fois un symbole, c'est pour ça qu'il n'y arrive pas
    2. Contributeur
      Bonjour,

      J'aurais plutôt écris ceci:
       Option Explicit
      '-------
      Sub xxxx()
      Dim Poidsmatiere As Double

      With Worksheets("PIECES USINEES")
      On Error GoTo erreur
      .Cells(i, 12) = .Cells(i, 9) * .Cells(i, 10) * .Cells(i, 11) / 1000000000 * Poidsmatiere
      End With
      Exit Sub

      'gestion erreurs
      erreur:
      .Cells(i, 12) = "Données incorrectes"
      End With
      End Sub

      Michel
      1. J'ai testé ta macro et chose incompréhensible, ma fonction réalisant ce calcul est une boucle, la première fois qu'il tombe sur une erreur il écrit bien données incorrectes et tout va bien et la deuxième fois (sur un autre calcul avec une erreur) : incompatibilité de type !!!
      2. Modérateur
        Bonjour,

        Salut Michel,

        Désolé pour l'incruste...

        La procédure donnée par Michel n'est pas en cause. Je suppose qu'il s'agit d'une erreur dans le format des données de la feuille. Il suffit que l'une des trois valeurs contenues dans : .Cells(i, 9), .Cells(i, 10), .Cells(i, 11) ne soit pas numérique, ou que tu tentes de stocker, dans Poidsmatiere, autre chose qu'une valeur numérique et ça plante...
      3. Je sais que certaines de mes valeurs ne sont pas numériques c'est pour ça que je veux qu'il écrive données incorrectes quand ce cas se produit
      4. Modérateur
        Donnes nous ton code avec boucle stp?
    3. Slt, sur quelle ligne l'incompatibilité de type ?
      1. Sur celle-ci : If IsError(Worksheets("PIECES USINEES").Cells(i, 9).Value * Worksheets("PIECES USINEES").Cells(i, 10).Value * Worksheets("PIECES USINEES").Cells(i, 11).Value / 1000000000 * PoidsMatiere) = True Then