IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB 6 et antérieur Discussion :

Erreur de compilation : Variable non définie


Sujet :

VB 6 et antérieur

  1. #1
    Membre à l'essai
    Homme Profil pro
    Lycéen
    Inscrit en
    Mai 2011
    Messages
    29
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Lycéen

    Informations forums :
    Inscription : Mai 2011
    Messages : 29
    Points : 19
    Points
    19
    Par défaut Erreur de compilation : Variable non définie
    Bonjour,

    Désolé mais je débute. Pouvez vous m 'aider. Merci.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    Option Explicit
     
    'Déclaration des variables globales
     
    'Déclaration des fichiers
     
    Private appExcel As Excel.Application 'Application Excel
    Private Affectation As Workbook
    Private Goracc As Workbook
    Private Correspondance As Workbook
    Private R13 As Workbook
    Private nomFichier As String
     
     
     
     
    Sub main()
     
    Set appExcel = CreateObject("Excel.Application")
     
     
    Form1.Show
     
    End Sub
     
     
     
    Sub SelectionFichier(nomFichier As String)
     
       Dim fd As Application
     
     
      ' Créer un objet FileDialog
       Set fd = Application.FileDialog(msoFileDialogOpen)
     
      ' Titre de la boîte de dialogue
       fd.Title = "Sélectionnez un fichier..."
     
      ' Ne pas autoriser la sélection multiple
       ' (donc 1 seul fichier est sélectionnable à la fois)
       fd.AllowMultiSelect = False
     
      ' Afficher la boîte de dialogue
       If fd.Show() Then
         MsgBox "Vous avez sélectionné le fichier : " _
           & vbCrLf & fd.SelectedItems(1), vbInformation
       End If
     
      'Ouverture du fichier en fonction du nom passé en paramètre
     
       Select Case nomFichier
     
            Case "FileAffectation"
                Set Affectation = Workbooks.Open(fd.SelectedItems(1))
     
            Case "FileGoracc"
                Set Goracc = Workbooks.Open(fd.SelectedItems(1))
     
            Case "FileCorrespondance"
                Set Correspondance = Workbooks.Open(fd.SelectedItems(1))
     
            Case "FileR13"
                 Set R13 = Workbooks.Open(fd.SelectedItems(1))
     
            Case Else
     
                 MsgBox ("Erreur de selection. Contactez l'assistance technique.")
     
        End Select
     
    End Sub
     
    'Fonction permettant de supprimer les lignes dont l'Horizontal est à 0 dans le fichier R13
     
    Function supprimerLignesInutilesR13()
     
        Dim nombreDeLigneR13 As Integer
        Dim compteur As Integer
        Dim nombreDeZero As Integer
     
     
        'Récupère le nombre de ligne du fichier R13
     
        nombreDeLigneR13 = R13.Sheets(3).UsedRange.Rows.Count
     
     
        'Récupère le nombre de ligne dont l'horizontal a la valeur zero
        nombreDeZero = calculNombreDeLignesASuppR13()
     
        'Suppression des lignes dont l'horizontal est à zero, en supprimant la première ligne de données(= ligne 2)
     
        For compteur = 2 To nombreDeZero + 2
            R13.Worksheets(3).Rows(2).Delete
        Next compteur
     
        'Sauvegarde du fichier modifié
     
        R13.Save
     
     
     
    End Function
     
    'calcul le nombre de lignes dont l'horizontal est à zero
    Function calculNombreDeLignesASuppR13()
     
        'Saute l'entête de la feuille Excel. Compteur commence à 2
     
        For compteur = 2 To nombreDeLigneR13
     
            If ((R13.Worksheets(3).Cells(compteur, 11).Value) = 0) Then
     
                nombreDeZero = nombreDeZero + 1
            End If
     
        Next compteur
     
        calculNombreDeLignesASuppR13 = nombreDeZero
     
    End Function
     
    'Tri du fichier sur la valeur de l'Horizontal
     
    Sub TriFichierR13SurValHorizontal()
     
         'Active le Worsheets numéro 3
     
        R13.Worksheets(3).Activate
     
        'Tri du fichier
        R13.Worksheets(3).Range("A2:N" & nombreDeLigneR13).Select
        Selection.Sort Key1:=Range("K1"), Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
     
        'Sauvegarde du fichier
        R13.Save
     
    End Sub
     
     
     
    'Fonction principale
     
    Sub execute()
     
     
      'Tri du fichier R13
     
      Call TriFichierR13SurValHorizontal
     
      'Suppression des lignes inutiles dans R13
     
     Call supprimerLignesInutilesR13
     
     
     
    MsgBox ("Fin du traitement")
     
    End Sub

    Form :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    Option Explicit
     
     
     
    Private Sub Exec_Click()
        Call execute
    End Sub
     
    Private Sub FileR13_Click()
        Call SelectionFichier("FileR13")
    End Sub
     
    Private Sub FileAffectation_Click()
        Call SelectionFichier("FileAffectation")
    End Sub
     
    Private Sub FileCorres_Click()
        Call SelectionFichier("FileCorrespondance")
    End Sub
     
    Private Sub FileGoracc_Click()
        Call SelectionFichier("FileGoracc")
    End Sub
    La fonction Sub SelectionFichier(nomFichier As String) pose problème.

    Message d'erreur : Erreur de compilation : Variable non définie

    L'execution s'arrete sur l'entête de la fonction.

    Cependant msoFileDialogOpen est en surbrillance dans :
    Set fd = Application.FileDialog(msoFileDialogOpen)

    Merci

  2. #2
    Membre actif Avatar de petit rabot
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Août 2010
    Messages
    236
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2010
    Messages : 236
    Points : 226
    Points
    226
    Par défaut
    As-tu déclaré la référence Excel???
    Projet -----> Référence -----> Microsoft Excel 12.0 Object Library

  3. #3
    Membre à l'essai
    Homme Profil pro
    Lycéen
    Inscrit en
    Mai 2011
    Messages
    29
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Lycéen

    Informations forums :
    Inscription : Mai 2011
    Messages : 29
    Points : 19
    Points
    19
    Par défaut
    Oui je l'ai bien déclarée

  4. #4
    Membre actif Avatar de petit rabot
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Août 2010
    Messages
    236
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2010
    Messages : 236
    Points : 226
    Points
    226
    Par défaut
    Pourquoi les parenthèses dans la ligne 44 ???
    Peux tu essayer sans les parenthèses.

  5. #5
    Membre à l'essai
    Homme Profil pro
    Lycéen
    Inscrit en
    Mai 2011
    Messages
    29
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Lycéen

    Informations forums :
    Inscription : Mai 2011
    Messages : 29
    Points : 19
    Points
    19
    Par défaut
    Ca ne change rien.

  6. #6
    Membre actif Avatar de petit rabot
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Août 2010
    Messages
    236
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Technicien maintenance
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2010
    Messages : 236
    Points : 226
    Points
    226
    Par défaut
    Essaie de mettre les lignes une à une en commentaire pour voir sur laquelle ça bloque.

  7. #7
    Expert éminent sénior
    Avatar de ProgElecT
    Homme Profil pro
    Retraité
    Inscrit en
    Décembre 2004
    Messages
    6 097
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 68
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Retraité
    Secteur : Communication - Médias

    Informations forums :
    Inscription : Décembre 2004
    Messages : 6 097
    Points : 16 606
    Points
    16 606
    Par défaut
    Salut
    Dans ton select case de la fonction SelectionFichier, lignes 54, 57 et 60
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    ... = Workbooks.Open (..... '<------------------ NON
    ... = appExcel.Workbooks.Open (.....'<---------- MIEUX

  8. #8
    Modérateur
    Avatar de l_autodidacte
    Homme Profil pro
    Retraité : Directeur de lycée/Professeur de sciences physiques
    Inscrit en
    Juillet 2009
    Messages
    2 416
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 69
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Retraité : Directeur de lycée/Professeur de sciences physiques
    Secteur : Enseignement

    Informations forums :
    Inscription : Juillet 2009
    Messages : 2 416
    Points : 5 808
    Points
    5 808
    Par défaut
    Il te manque tout simplement la référence : Microsoft Office 12.0 Object Library

  9. #9
    Rédacteur
    Avatar de DarkVader
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2002
    Messages
    2 130
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2002
    Messages : 2 130
    Points : 3 118
    Points
    3 118
    Par défaut
    Bonjour,
    Il est temps de prendre des wouacances ...

    Citation Envoyé par Smallstone Voir le message
    La fonction Sub SelectionFichier(nomFichier As String) pose problème.

    Message d'erreur : Erreur de compilation : Variable non définie

    L'execution s'arrete sur l'entête de la fonction.

    Cependant msoFileDialogOpen est en surbrillance dans :
    Set fd = Application.FileDialog(msoFileDialogOpen)

    Merci
    En remplaçant Application par appExcel, ça ne serait pas plus fonctionnel ?

    D'autre part, les «Select/Selection,Activate» => à bannir

Discussions similaires

  1. [AC-2007] Erreur de compilation: Variable non définie
    Par m1koo dans le forum VBA Access
    Réponses: 7
    Dernier message: 11/01/2014, 18h21
  2. [XL-2010] Erreur de compilation : variable non définie sur un textbox
    Par Bricomachin dans le forum Excel
    Réponses: 4
    Dernier message: 12/04/2013, 11h56
  3. erreur compilation: variable non défini
    Par ngouagme dans le forum Langage
    Réponses: 8
    Dernier message: 07/07/2010, 15h38
  4. [AC-2003] Erreur de variable non définie
    Par piercleo dans le forum VBA Access
    Réponses: 1
    Dernier message: 12/01/2010, 16h02
  5. erreur de compilation variable non defini
    Par nabelou1 dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 14/01/2009, 16h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo