Outils logiciels pour les cours Paris II

Cours Paris II

edit SideBar

Cours4bis

  • Instructions de base
    • Boucles
    • Test
    • Divers: goto abc
  • Structure de données: Programme= Algorithme + structure de données
    • Strings
    • Integer, Real
    • matrices A(100), B(20,20),...
    • files
      • écrire un fichier
  Sub wfile()

  Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    Dim Fileout As Object
    Set Fileout = fso.CreateTextFile("C:\Documents and Settings\mdr\Mes documents\VBA\ES.txt", True, True)

  M = 20
  i = 1
  Do While i < M + 1
   Cells(i + 1, 2) = "a" & Str(i)
  ' print in a file
   Fileout.Write "a" & Str(i) & Chr(13) & Chr(10)
   i = i + 1
  Loop
   ' close the file
    Fileout.Close
  End Sub
  • lire un fichier existant
  Sub rfile()
  Dim myFile As String
  Dim textline As String

 ' preparing the file 
  myFile = "C:\Documents and Settings\mdr\Mes documents\VBA\ES.txt"

  Open myFile For Input As #1

  i = 1
  Do Until EOF(1)
    Line Input #1, textline
    Cells(i+1, 5) = textline
    i = i + 1
  Loop

   ' close the file
  Close #1
  End Sub
  • Passages de paramètres
  Sub TestPassing1()
  Dim y As Integer
  y = 50
  AddNo1 y
  MsgBox y
  End Sub
  Sub AddNo1(ByRef x As Integer)
  x = x + 10
  End Sub

Passage par valeur
  Sub TestPassing2()
  Dim y As Integer
  y = 50
  AddNo2 y
  MsgBox y
  End Sub
  Sub AddNo2(ByVal x As Integer)
  x = x + 10
  End Sub
  • Fonctions

Function Area(x As Double, y As Double) As Double

Area = x * y

End Function

Sub a() Cells(1, 1) = Area(1.5, 3.4) End Sub

  • Objets: Worksheets
    Sub ShowWorkSheets() Dim mySheet As Worksheet For Each mySheet In Worksheets MsgBox mySheet.Name Next mySheet End Sub

Fichier Excel Entrée/sortie et function

UP2