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
| Public Sub SortTable()
Const xlGuess = 0, ForWriting = 2
Dim fso, Fich, Cnt, Table()
Dim XL, WB, Sht, I, S
S = ""
Set XL = CreateObject("Excel.Application")
XL.Visible = True
XL.DisplayAlerts = False
Set WB = XL.Workbooks.Open("C:\Test.xls")
Set Sht = WB.Worksheets(1)
'Syntaxe :
'expression.Sort(Key1, Order1, Key2, Type, Order2, Key3, Order3, Header, OrderCustom, MatchCase, Orientation, SortMethod, DataOption1, DataOption2, DataOption3)
Sht.Range("B1").Sort Sht.Columns("B"), , , , , , , xlGuess
Set fso = CreateObject("Scripting.FileSystemObject")
Set Fich = fso.OpenTextFile("Sortie.txt", ForWriting, True)
Cnt = Sht.UsedRange.Rows.Count
ReDim Table(0)
For I = 2 To Cnt ' On commenc à 2 pour préserver l'entête de colonne
With Sht
Fich.WriteLine .Cells(I, 1) & " => " & .Cells(I, 2)
ReDim Preserve Table(I-2)
Table(I-2) = .Cells(I, 2)
End With
Next
Fich.Close
'Enregistrement ou non ?
If MsgBox("Enregistrer les modification ?",vbYesNo+vbInformation,"Enregistrer " & WB.Name) = vbYes Then WB.Save
XL.Quit
Set XL = Nothing
End Sub |
Partager