Bonjour,

Je suis en ce moment sur un petit programme VB avec qui manipule 2 classeurs Excel. J'utilise pour ça Visual Basic 2005 Express.

Le projet fait référence à Excel et j'ai en plus importer Excel dans le module.

Comme je suis pas une star de la prog, j'ai décidé de créer des Macros sous Excel dans un premier temps et de voir le code généré dans le Visual Basic Editor d'Excel. Puis j'adapte les lignes de code des Macros dans un module sous VB 2005.

Voilà pour la petite intro

Le code que je "crée" n'est pas optimisé loin de là mais tout ce que je veux c'est un résultat. Et je bloque sur une sélection des lignes utilisées dans la feuille. Voici le code, je pense que vous trouverez facilement le(s) endroit(s) où ça HIC! Je l'ai marqué par un commentaire.

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
 
Sub MakeFile(ByVal sourceFilePath As String, ByVal SourceFileName As String, ByVal DestinationFolderPath As String, ByVal DestinationFileName As String, ByVal column1 As String, ByVal column2 As String, ByVal column3 As String, ByVal NumberLines As Integer)
 
        ' Create an Excel application and add a workbook
        Dim excelApp As New Excel.Application
        excelApp.Visible = True ' True for the test and false to hide the application
 
        Dim wbookSource As Workbook = excelApp.Workbooks.Open(sourceFilePath)
        Dim wbookDestination As Workbook = excelApp.Workbooks.Add
 
        ' Save the destination file
        excelApp.DisplayAlerts = False
        ChDir(DestinationFolderPath)
        wbookDestination.SaveAs(DestinationFolderPath & "\" & DestinationFileName)
        excelApp.DisplayAlerts = True
 
        ' Count the number of sheets in the source file
        Dim numberSheets As Integer = wbookSource.Worksheets.Count
 
        Dim i As Integer
        For i = 1 To numberSheets Step 1
 
            wbookSource.Worksheets(i).Activate()
            Dim sheetName As String = wbookSource.ActiveSheet.Name
            excelApp.Columns(column1).Select()
            excelApp.Selection.Copy() 'copy the column
 
            wbookDestination.Worksheets(i).Activate()
            wbookDestination.Worksheets(i).Name = sheetName 'affect a name to the sheet
            excelApp.Range("A1").Select() 'paste the column previously copied in the destination file's sheet
            excelApp.ActiveSheet.Paste()
 
            wbookSource.Worksheets(i).Activate()
            excelApp.Columns(column2).Select()
            excelApp.Selection.Copy() 'copy the column
 
            wbookDestination.Worksheets(i).Activate()
            excelApp.Range("B1").Select() 'paste the column previously copied in the destination file's sheet
            excelApp.ActiveSheet.Paste()
 
            wbookSource.Worksheets(i).Activate()
            excelApp.Columns(column3).Select()
            excelApp.Selection.Copy() 'copy the column
 
            wbookDestination.Worksheets(i).Activate()
            excelApp.Range("C1").Select() 'paste the column previously copied in the destination file's sheet
            excelApp.ActiveSheet.Paste()
 
            ' Delete eventual extra lines
            Dim LastLineSheetRef As Integer ' First let's count lines of the source file's sheet
            wbookSource.Worksheets(i).Activate()
            excelApp.Range("A1").Select()
            '
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
' HIC!            
'C'est là que je galère le code suivant est celui founit par l'Editeur Excel à adapter
' je veux compter le nombre de lignes utilisées dans la ième feuille du classeur
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
            '
            '
            Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select()
            LastLineSheetRef = Selection.Rows.Count
            '
            '
            ' autres traitements prévus
        Next
 
        ' save the destination file + close the excel files and stop the excel application
        excelApp.DisplayAlerts = False
        excelApp.Workbooks(DestinationFileName).Close(True)
        excelApp.Workbooks(SourceFileName).Close(False)
        excelApp.DisplayAlerts = True
        excelApp.Quit()
    End Sub
Si vous avez une petie idée faites-moi signe.

Merci d'avance et à bientôt!
reun0