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

Macros et VBA Excel Discussion :

[VB]Problème ouverture de deux fichier excel


Sujet :

Macros et VBA Excel

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    150
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Septembre 2005
    Messages : 150
    Points : 65
    Points
    65
    Par défaut [VB]Problème ouverture de deux fichier excel
    Bonjour
    Je travaille sur un programme qui utilise deux fichiers Excel. Seulement j'ouvre le premier tout va bien et lorsque j'ouvre le deuxième il m'ouvre le même fichier que le deuxième.
    Je ne comprend pas pourquoi.
    voila mon code:
    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
     
    Dim appExcel As Excel.Application 'Application Excel
    Dim wbExcel As Excel.Workbook 'Classeur Excel
    Dim wsExcel As Excel.Worksheet 'Feuille Excel
    Dim wbExcelAna As Excel.Workbook 'Classeur Excel
    Dim wsExcelAna As Excel.Worksheet 'Feuille Excel
    Dim TestBoucle, No_Piece, DatePiece, Code_Journal, DateEche, Ref_Piece, CompteGen, MontantGen, Sens, Libelle, Code_Taxe, No_Plan, Type_Ecr, CompteTiers, MontantAna, SensAna, No_Plan_Ana, No_Section, Type_Ecr_Ana, NumPrim, Num_Ana As String
    Dim LigneGen, LigneAna As String
    Dim j, i As Integer
    Dim TestBool As Boolean
     
    Private Sub cmdValider_Click()
    Close
    j = 2
    i = 2
    'Ouverture de l'application
    Set appExcel = CreateObject("Excel.Application")
    'Ouverture d'un fichier Excel
    Set wbExcel = appExcel.Workbooks.Open(Form1.txtEcrGen)
    'wsExcel correspond à la première feuille du fichier
    Set wsExcel = wbExcel.Worksheets(1)
     
    'Ouverture de l'application
    Set appExcel = CreateObject("Excel.Application")
    'Ouverture d'un fichier Excel
    Set wbExcelAna = appExcel.Workbooks.Open(Form1.txtEcrAna)
    'wsExcel correspond à la première feuille du fichier
    Set wsExcelAna = wbExcel.Worksheets(1)
    Open Form1.txtDest For Output As #2
     
     
     
    While &#40;wsExcel.Cells&#40;i, 1&#41;.Value <> Empty&#41;
        TestBool = False
        Num_Ana = "1"
        'LigneGen = "" & wsExcel.Cells&#40;i, 2&#41;.Value & vbTab & "" & vbTab & wsExcel.Cells&#40;i, 4&#41;.Value & vbTab & wsExcel.Cells&#40;i, 5&#41;.Value & vbTab & wsExcel.Cells&#40;i, 6&#41;.Value & vbTab & wsExcel.Cells&#40;i, 7&#41;.Value & vbTab & wsExcel.Cells&#40;i, 9&#41;.Value & vbTab & wsExcel.Cells&#40;i, 22&#41;.Value & vbTab & wsExcel.Cells&#40;i, 11&#41;.Value & vbtab &
        NumPrim = wsExcel.Cells&#40;i, 1&#41;.Value
        No_Piece = wsExcel.Cells&#40;i, 2&#41;.Value
        DatePiece = wsExcel.Cells&#40;i, 4&#41;.Value
        Code_Journal = wsExcel.Cells&#40;i, 5&#41;.Value
        Ref_Piece = wsExcel.Cells&#40;i, 6&#41;.Value
        CompteGen = Mid&#40;CStr&#40;wsExcel.Cells&#40;i, 7&#41;.Value&#41;, 1, 7&#41;
        MontantGen = wsExcel.Cells&#40;i, 9&#41;.Value
        Sens = wsExcel.Cells&#40;i, 22&#41;.Value
        Libelle = wsExcel.Cells&#40;i, 11&#41;.Value
     
        If Mid&#40;CompteGen, 1, 3&#41; = "401" Then
            Code_Taxe = "D19"
        ElseIf Mid&#40;CompteGen, 1, 3&#41; = "411" Then
            Code_Taxe = "C05"
        End If
     
        No_Plan = "0"
        Type_Ecr = "G"
        CompteTiers = wsExcel.Cells&#40;i, 8&#41;.Value
        DateEche = wsExcel.Cells&#40;i, 23&#41;.Value
     
        LigneGen = "" & No_Piece & vbTab & "" & vbTab & DatePiece & vbTab & Code_Journal & vbTab & Ref_Piece & vbTab & CompteGen & vbTab & MontantGen & vbTab & Sens & vbTab & Libelle & vbTab & Code_Taxe & vbTab & No_Plan & vbTab & Type_Ecr & vbTab & CompteTiers & vbTab & DateEche
        Print #2, LigneGen
     
        j = 2
        While &#40;wsExcelAna.Cells&#40;j, 1&#41;.Value <> Empty Or TestBool = True Or CDbl&#40;Num_Ana&#41; < CDbl&#40;NumPrim&#41;&#41;
            Num_Ana = wsExcelAna.Cells&#40;j, 1&#41;.Value
            If wsExcelAna.Cells&#40;j, 1&#41;.Value = NumPrim Then
     
     
                'Insertion Analytique
                MontantAna = wsExcelAna.Cells&#40;j, 9&#41;.Value
     
                If wsExcelAna.Cells&#40;j, 6&#41;.Value = "-1" Then
                    SensAna = "D"
                ElseIf wsExcelAna.Cells&#40;j, 6&#41;.Value = "1" Then
                    SensAna = "C"
                End If
     
                No_Plan_Ana = "1"
                No_Section = wsExcelAna.Cells&#40;j, 3&#41;.Value
                Type_Ecr_Ana = "A"
                LigneAna = "" & No_Piece & vbTab & "" & vbTab & DatePiece & vbTab & Code_Journal & vbTab & Ref_Piece & vbTab & CompteGen & vbTab & MontantAna & vbTab & SensAna & vbTab & Libelle & vbTab & Code_Taxe & vbTab & No_Plan_Ana & vbTab & No_Section & vbTab & Type_Ecr_Ana & vbTab & CompteTiers & vbTab & DateEche
                Print #2, LigneAna
     
                If wsExcelAna.Cells&#40;j + 1, 1&#41;.Value = NumPrim Then
                    TestBool = True
                End If
            End If
     
            j = j + 1
        Wend
        i = i + 1
    Wend
    End Sub
    Merci

  2. #2
    Membre expérimenté Avatar de Megaxel
    Profil pro
    Inscrit en
    Mai 2003
    Messages
    1 187
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2003
    Messages : 1 187
    Points : 1 405
    Points
    1 405
    Par défaut
    Pourquoi ouvres-tu 2 fois l'application Excel, et en plus avec le même nom (AppExcel)?

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    150
    Détails du profil
    Informations personnelles :
    Âge : 40
    Localisation : France

    Informations forums :
    Inscription : Septembre 2005
    Messages : 150
    Points : 65
    Points
    65
    Par défaut
    C'est vrai déja y avait ce problème et en plus je m'étais trompé dans une des déclarations.
    Mais ce problème est résolu mais j'en n'ai un autre.Dans ma boucle while mon test sur le booléen ne s'effectue pas.Revoila mon code
    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
     
    Dim appExcel As Excel.Application
    Dim appExcel1 As Excel.Application 'Application Excel
    Dim wbExcel As Excel.Workbook 'Classeur Excel
    Dim wsExcel As Excel.Worksheet 'Feuille Excel
    Dim wbExcelAna As Excel.Workbook 'Classeur Excel
    Dim wsExcelAna As Excel.Worksheet 'Feuille Excel
    Dim TestBoucle, No_Piece, DatePiece, Code_Journal, DateEche, Ref_Piece, CompteGen, MontantGen, Sens, Libelle, Code_Taxe, No_Plan, Type_Ecr, CompteTiers, MontantAna, SensAna, No_Plan_Ana, No_Section, Type_Ecr_Ana, NumPrim, Num_Ana As String
    Dim LigneGen, LigneAna As String
    Dim j, i As Integer
    Dim Num_AnaInt, NumPrimInt As Double
    Dim TestBool As Boolean
     
    Private Sub cmdValider_Click&#40;&#41;
    Close
    j = 2
    i = 2
    'Ouverture de l'application
    Set appExcel = CreateObject&#40;"Excel.Application"&#41;
    'Ouverture d'un fichier Excel
    Set wbExcel = appExcel.Workbooks.Open&#40;Form1.txtEcrGen&#41;
    'wsExcel correspond à la première feuille du fichier
    Set wsExcel = wbExcel.Worksheets&#40;1&#41;
     
    'Ouverture de l'application
    Set appExcel1 = CreateObject&#40;"Excel.Application"&#41;
    'Ouverture d'un fichier Excel
    Set wbExcelAna = appExcel1.Workbooks.Open&#40;Form1.txtEcrAna&#41;
    'wsExcel correspond à la première feuille du fichier
    Set wsExcelAna = wbExcelAna.Worksheets&#40;1&#41;
    Open Form1.txtDest For Output As #2
     
     
     
    While &#40;wsExcel.Cells&#40;i, 1&#41;.Value <> Empty&#41;
        TestBool = False
        Num_Ana = "1"
        'LigneGen = "" & wsExcel.Cells&#40;i, 2&#41;.Value & vbTab & "" & vbTab & wsExcel.Cells&#40;i, 4&#41;.Value & vbTab & wsExcel.Cells&#40;i, 5&#41;.Value & vbTab & wsExcel.Cells&#40;i, 6&#41;.Value & vbTab & wsExcel.Cells&#40;i, 7&#41;.Value & vbTab & wsExcel.Cells&#40;i, 9&#41;.Value & vbTab & wsExcel.Cells&#40;i, 22&#41;.Value & vbTab & wsExcel.Cells&#40;i, 11&#41;.Value & vbtab &
        NumPrim = wsExcel.Cells&#40;i, 1&#41;.Value
        No_Piece = wsExcel.Cells&#40;i, 2&#41;.Value
        DatePiece = wsExcel.Cells&#40;i, 4&#41;.Value
        Code_Journal = wsExcel.Cells&#40;i, 5&#41;.Value
        Ref_Piece = wsExcel.Cells&#40;i, 6&#41;.Value
        CompteGen = Mid&#40;CStr&#40;wsExcel.Cells&#40;i, 7&#41;.Value&#41;, 1, 7&#41;
        MontantGen = wsExcel.Cells&#40;i, 9&#41;.Value
        Sens = wsExcel.Cells&#40;i, 22&#41;.Value
        Libelle = wsExcel.Cells&#40;i, 11&#41;.Value
     
        If Mid&#40;CompteGen, 1, 3&#41; = "401" Then
            Code_Taxe = "D19"
        ElseIf Mid&#40;CompteGen, 1, 3&#41; = "411" Then
            Code_Taxe = "C05"
        End If
     
        No_Plan = "0"
        Type_Ecr = "G"
        CompteTiers = wsExcel.Cells&#40;i, 8&#41;.Value
        DateEche = wsExcel.Cells&#40;i, 23&#41;.Value
     
        LigneGen = "" & No_Piece & vbTab & "" & vbTab & DatePiece & vbTab & Code_Journal & vbTab & Ref_Piece & vbTab & CompteGen & vbTab & MontantGen & vbTab & Sens & vbTab & Libelle & vbTab & Code_Taxe & vbTab & No_Plan & vbTab & Type_Ecr & vbTab & CompteTiers & vbTab & DateEche
        Print #2, LigneGen
     
        j = 2
        While &#40;&#40;wsExcelAna.Cells&#40;j, 1&#41;.Value <> Empty&#41; Or &#40;TestBool = True&#41;&#41;
            Num_Ana = wsExcelAna.Cells&#40;j, 1&#41;.Value
            If wsExcelAna.Cells&#40;j, 1&#41;.Value = NumPrim Then
                'Insertion Analytique
                MontantAna = wsExcelAna.Cells&#40;j, 9&#41;.Value
     
                If wsExcelAna.Cells&#40;j, 6&#41;.Value = "-1" Then
                    SensAna = "D"
                ElseIf wsExcelAna.Cells&#40;j, 6&#41;.Value = "1" Then
                    SensAna = "C"
                End If
     
                No_Plan_Ana = "1"
                No_Section = wsExcelAna.Cells&#40;j, 3&#41;.Value
                Type_Ecr_Ana = "A"
                LigneAna = "" & No_Piece & vbTab & "" & vbTab & DatePiece & vbTab & Code_Journal & vbTab & Ref_Piece & vbTab & CompteGen & vbTab & MontantAna & vbTab & SensAna & vbTab & Libelle & vbTab & Code_Taxe & vbTab & No_Plan_Ana & vbTab & No_Section & vbTab & Type_Ecr_Ana & vbTab & CompteTiers & vbTab & DateEche
                Print #2, LigneAna
     
                If wsExcelAna.Cells&#40;j + 1, 1&#41;.Value = NumPrim Then
                    TestBool = True
                End If
            End If
            Num_AnaInt = CDbl&#40;Num_Ana&#41;
            NumPrimInt = CDbl&#40;NumPrim&#41;
            If Num_AnaInt > NumPrimInt Then
                TestBool = False
            End If
            j = j + 1
        Wend
        i = i + 1
    Wend
    End Sub
    Merci

  4. #4
    Membre expérimenté Avatar de Megaxel
    Profil pro
    Inscrit en
    Mai 2003
    Messages
    1 187
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2003
    Messages : 1 187
    Points : 1 405
    Points
    1 405
    Par défaut
    Remplace ton test par celui la:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    While Not IsEmpty&#40;wsExcel.Cells&#40;i, 1&#41;&#41; ...

Discussions similaires

  1. Problème sur VBA : ouverture d'un fichier Excel Réseau
    Par michelsimon dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 13/12/2012, 21h59
  2. Réponses: 4
    Dernier message: 01/08/2012, 10h01
  3. [SP-2010] Problème d'ouverture d'un fichier excel
    Par Gond63 dans le forum SharePoint
    Réponses: 0
    Dernier message: 14/04/2011, 11h53
  4. [EXCEL 2002] Problème ouverture de 2 fichiers Excel
    Par Golork dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 30/06/2010, 20h45
  5. problème d'ouverture d'un fichier EXCEL!!
    Par JauB dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 27/11/2005, 14h37

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