Microsoft 365 Prendre un tableau en paramètre dans une fonction

Excellearning

XLDnaute Nouveau
Bonjour à tous et à toutes.
Je vous espère bien
Je souhaiterais savoir si possible comment faire pour passer un tableau en paramètre d'une fonction
Ci-joint un fichier
Merci
 

Pièces jointes

  • formationbosse.xlsm
    22.9 KB · Affichages: 7
Solution
C
Bonjour le fil

@Excellearning pour transmettre un tableau dans une fonction il faut que ce soit définit correctement ;)
VB:
Public Function Test_Per(Tableau) As Integer
  Dim Entven As Integer
  Dim Pdd As Integer
  Dim PerEVen As Double
  Dim PerOdd As Double
  Even = 0
  Pdd = 0
  I = 0
  Do While Tableau(I) <> -1
    If Tableau(I) Mod 2 = 1 Then Pdd = Pdd + 1
    If Tableau(I) Mod 2 = 0 Then Entven = Entven + 1
    I = I + 1
  Loop
  MsgBox "Il y a respectivement " & Entven & " nombres pairs de pourcentage " & Entven / I & " et " & Pdd & " nombres impairs de pourcentage " & Pdd / I
End Function

Sub test_function()
  T = Array(1, 8, 20, 50, 51, 47, 6, -6, -7, 9, 3, -1)
  Test_Per (T)
End Sub

A+
C

Compte Supprimé 979

Guest
Bonjour le fil

@Excellearning pour transmettre un tableau dans une fonction il faut que ce soit définit correctement ;)
VB:
Public Function Test_Per(Tableau) As Integer
  Dim Entven As Integer
  Dim Pdd As Integer
  Dim PerEVen As Double
  Dim PerOdd As Double
  Even = 0
  Pdd = 0
  I = 0
  Do While Tableau(I) <> -1
    If Tableau(I) Mod 2 = 1 Then Pdd = Pdd + 1
    If Tableau(I) Mod 2 = 0 Then Entven = Entven + 1
    I = I + 1
  Loop
  MsgBox "Il y a respectivement " & Entven & " nombres pairs de pourcentage " & Entven / I & " et " & Pdd & " nombres impairs de pourcentage " & Pdd / I
End Function

Sub test_function()
  T = Array(1, 8, 20, 50, 51, 47, 6, -6, -7, 9, 3, -1)
  Test_Per (T)
End Sub

A+
 

Excellearning

XLDnaute Nouveau
Bonjour le fil

@Excellearning pour transmettre un tableau dans une fonction il faut que ce soit définit correctement ;)
VB:
Public Function Test_Per(Tableau) As Integer
  Dim Entven As Integer
  Dim Pdd As Integer
  Dim PerEVen As Double
  Dim PerOdd As Double
  Even = 0
  Pdd = 0
  I = 0
  Do While Tableau(I) <> -1
    If Tableau(I) Mod 2 = 1 Then Pdd = Pdd + 1
    If Tableau(I) Mod 2 = 0 Then Entven = Entven + 1
    I = I + 1
  Loop
  MsgBox "Il y a respectivement " & Entven & " nombres pairs de pourcentage " & Entven / I & " et " & Pdd & " nombres impairs de pourcentage " & Pdd / I
End Function

Sub test_function()
  T = Array(1, 8, 20, 50, 51, 47, 6, -6, -7, 9, 3, -1)
  Test_Per (T)
End Sub

A+
Merci beaucoup
 

mapomme

XLDnaute Barbatruc
Supporter XLD
Bonjour à tous,

Pour le fun, le code à ma sauce 😀.

La fonction Test_Per ne sert à rien puisqu'elle ne renvoie rien. Autant la transformer en Sub(). Cela ne change en rien la manière de passer un tableau dans une fonction ou sub.

Le code:
Code:
Option Explicit

Sub somme()
Dim c As Long, s As Long, i As Long
   c = 2: s = 0
   For i = c To 99 Step 3
      s = s + i
      MsgBox "(" & s & " , " & i & ")"
   Next i
   MsgBox "La somme est :" & s
End Sub

Sub Palindrome()
   Dim s As String
   s = LCase(Trim(InputBox("Entrer un texte :")))
   If s = "" Then Exit Sub
   If s = StrReverse(s) Then MsgBox "Le texte " & s & " est un palindrome." Else MsgBox "Le texte " & s & " n'est un palindrome."
End Sub

Public Sub Test_Per(Tableau)
Dim PairS As Long, Impairs As Long, x
   For Each x In Tableau
      If x = -1 Then Exit For
      If x Mod 2 = 0 Then PairS = PairS + 1 Else Impairs = Impairs + 1
   Next x
   MsgBox "Il y a " & PairS & " nombres pairs (" & Format(PairS / (PairS + Impairs), ".0%") & ")" & vbLf & _
          "Il y a " & Impairs & " nombres impairs (" & Format(Impairs / (PairS + Impairs), ".0%") & ")"
End Sub

Sub test_function()
Dim T
  T = Array(1, 8, 20, 50, 51, 47, 6, -6, -7, 9, 3, -1)
  Test_Per T
End Sub
 
Dernière édition:

Excellearning

XLDnaute Nouveau
Bonjour à tous,

Pour le fun, le code à ma sauce 😀.

La fonction Test_Per ne sert à rien puisqu'elle ne renvoie rien. Autant la transformer en Sub(). Cela ne change en rien la manière de passer un tableau dans une fonction ou sub.

Le code:
Code:
Option Explicit

Sub somme()
Dim c As Long, s As Long, i As Long
   c = 2: s = 0
   For i = c To 99 Step 3
      s = s + i
      MsgBox "(" & s & " , " & i & ")"
   Next i
   MsgBox "La somme est :" & s
End Sub

Sub Palindrome()
   Dim s As String
   s = LCase(Trim(InputBox("Entrer un texte :")))
   If s = "" Then Exit Sub
   If s = StrReverse(s) Then MsgBox "Le texte " & s & " est un palindrome." Else MsgBox "Le texte " & s & " n'est un palindrome."
End Sub

Public Sub Test_Per(Tableau)
Dim PairS As Long, Impairs As Long, x
   For Each x In Tableau
      If x = -1 Then Exit For
      If x Mod 2 = 0 Then PairS = PairS + 1 Else Impairs = Impairs + 1
   Next x
   MsgBox "Il y a " & PairS & " nombres pairs (" & Format(PairS / (PairS + Impairs), ".0%") & ")" & vbLf & _
          "Il y a " & Impairs & " nombres impairs (" & Format(Impairs / (PairS + Impairs), ".0%") & ")"
End Sub

Sub test_function()
Dim T
  T = Array(1, 8, 20, 50, 51, 47, 6, -6, -7, 9, 3, -1)
  Test_Per T
End Sub
Merci beaucoup
 

Discussions similaires

Réponses
6
Affichages
310

Statistiques des forums

Discussions
312 156
Messages
2 085 813
Membres
102 989
dernier inscrit
Denver76