Besoin d'aide pour Tableau en Visual Basic

Utilisateur anonyme - 10 mai 2004 à 16:47
HFanny Messages postés 699 Date d'inscription mercredi 19 février 2003 Statut Membre Dernière intervention 13 mai 2011 - 11 mai 2004 à 09:30
bonjour tout le monde

je dois faire un tableau, je dois remplir chaque case de ce tableau grace au clavier, je dois lancer le programme ou il me donne la main me demandant les noms que je dois entrer, ensuite il tri les noms que je lui donne par ordre alphabétique, ensuite il faut que j'enregistre se tableau dans un fichier text ...
ce fichier sera lu par un otre ordi en réseau ...
je ne sais pas du tout comment faire ..
pouvez vous m'aider ??

Merci d'avance !!!!

2 réponses

HFanny Messages postés 699 Date d'inscription mercredi 19 février 2003 Statut Membre Dernière intervention 13 mai 2011 20
11 mai 2004 à 09:26
Bonjour,

voilà comment tu peux faire (en tout cas, t'inspirer de ça) :

Sub tableau()
Dim tableau() As String
Dim rep As String
Dim cpt, i As Integer

rep = "toto"
cpt = 0
ReDim tableau(cpt)
While Not rep = ""
rep = InputBox("Veuillez entrer un nom")
tableau(cpt) = rep
cpt = cpt + 1
ReDim Preserve tableau(cpt)
Wend
cpt = cpt - 2
ReDim Preserve tableau(cpt)

'For i = 0 To cpt
'MsgBox ("Voici le nom " & i & " : " & tableau(i))
'Next i

Call trier(tableau)

'For i = 0 To cpt
'MsgBox ("Voici le nom " & i & " : " & tableau(i))
'Next i

Call ecrireFichier(tableau)

End Sub

Sub trier(tableau() As String)
  'Cette fonction je l'ai trouvé sur le site (elle vient de Bubble44)
  Dim first As Long
  Dim last As Long
  Dim i As Long
  Dim noswap As Boolean
  Dim v As Variant
  
  first = LBound(tableau)
  last = UBound(tableau)
  
  last = last - 1
  Do While first <= last
     noswap = True
     For i = first To last
        If tableau(i) > tableau(i + 1) Then
           v = tableau(i)
           tableau(i) = tableau(i + 1)
           tableau(i + 1) = v
           noswap = False
        End If
     Next i
     last = last - 1
     If noswap Then Exit Do
  Loop
End Sub

Sub ecrireFichier(tableau() As String)
Dim fso, fichier
Dim i, cpt As Integer
Dim nomFichier As String
Set fso = CreateObject("Scripting.FileSystemObject")
nomFichier = "toto"
i = 0

'on vérifie que l'on écrit pas dans un fichier existant
While fso.FileExists("D:\USER" & nomFichier & i & ".txt")
i = i + 1
Wend
nomFichier = "D:\USER" & nomFichier & i & ".txt"

Set fichier = fso.CreateTextFile(nomFichier, True)
For cpt = 0 To UBound(tableau)
fichier.WriteLine (tableau(cpt))
Next cpt
fichier.Close

Set fso = Nothing
Set fichier = Nothing

MsgBox "Le fichier créé se nomme : " & nomFichier

End Sub



Le tri vient donc du site, j'ai cherché sur tri alphabétique et le code de Bubble44 m'a semblé rapide et efficace donc voilà ...

Fanny
0
HFanny Messages postés 699 Date d'inscription mercredi 19 février 2003 Statut Membre Dernière intervention 13 mai 2011 20
11 mai 2004 à 09:30
Bonjour,

voilà comment tu peux faire (en tout cas, t'inspirer de ça) :

Sub tableau()
Dim tableau() As String
Dim rep As String
Dim cpt, i As Integer

rep = "toto"
cpt = 0
ReDim tableau(cpt)
While Not rep = ""
rep = InputBox("Veuillez entrer un nom")
tableau(cpt) = rep
cpt = cpt + 1
ReDim Preserve tableau(cpt)
Wend
cpt = cpt - 2
ReDim Preserve tableau(cpt)

'For i = 0 To cpt
'MsgBox ("Voici le nom " & i & " : " & tableau(i))
'Next i

Call trier(tableau)

'For i = 0 To cpt
'MsgBox ("Voici le nom " & i & " : " & tableau(i))
'Next i

Call ecrireFichier(tableau)

End Sub

Sub trier(tableau() As String)
  'Cette fonction je l'ai trouvé sur le site (elle vient de Bubble44)
  Dim first As Long
  Dim last As Long
  Dim i As Long
  Dim noswap As Boolean
  Dim v As Variant
  
  first = LBound(tableau)
  last = UBound(tableau)
  
  last = last - 1
  Do While first <= last
     noswap = True
     For i = first To last
        If tableau(i) > tableau(i + 1) Then
           v = tableau(i)
           tableau(i) = tableau(i + 1)
           tableau(i + 1) = v
           noswap = False
        End If
     Next i
     last = last - 1
     If noswap Then Exit Do
  Loop
End Sub

Sub ecrireFichier(tableau() As String)
Dim fso, fichier
Dim i, cpt As Integer
Dim nomFichier As String
Set fso = CreateObject("Scripting.FileSystemObject")
nomFichier = "toto"
i = 0

'on vérifie que l'on écrit pas dans un fichier existant
While fso.FileExists("D:\USER" & nomFichier & i & ".txt")
i = i + 1
Wend
nomFichier = "D:\USER" & nomFichier & i & ".txt"

Set fichier = fso.CreateTextFile(nomFichier, True)
For cpt = 0 To UBound(tableau)
fichier.WriteLine (tableau(cpt))
Next cpt
fichier.Close

Set fso = Nothing
Set fichier = Nothing

MsgBox "Le fichier créé se nomme : " & nomFichier

End Sub



Le tri vient donc du site, j'ai cherché sur tri alphabétique et le code de Bubble44 m'a semblé rapide et efficace donc voilà ...

Fanny
0
Rejoignez-nous