sub en function

Hunter296

XLDnaute Nouveau
Bonjour à tous,

Je suis nouveau sur ce site, et je suis ravi d'être parmi vous.
Voilà, j'ai écris le code ci-dessous sous forme de Sub, et je souhaite le rendre comme étant une fonction:

Sub ConcaténationChaines()

Dim ChaineA As String
Dim ChaineB As String
Dim ChaineC As String

Dim F1 As Integer
Dim F2 As Integer
Dim F3 As Integer
Dim E1 As Integer

ChaineA = Range("A1").Value
ChaineB = Range("B1").Value
ChaineC = Range("C1").Value

F1 = Range("F1").Value
F2 = Range("F2").Value
F3 = Range("F3").Value
E1 = Range("E1").Value

If F1 > E1 Or F1 < E1 And F2 = 0 Then

Range("D1").Value = ChaineA
Else
If F1 < E1 And F1 <> 0 Then
Range("D1").Value = ChaineA & " " & ChaineB
Else
If F1 = 0 And F2 > E1 Then
Range("D1").Value = ChaineB
Else
If F1 = 0 And F2 < E1 And F3 > 0 Then
Range("D1").Value = ChaineB & " " & ChaineC
Else
If F1 = 0 And F2 = 0 Then
Range("D1").Value = ChaineC
End If
End If
End If
End If
End If
End Sub


Qlq un peut m'aider sur ce sujet.
Les conditions du résultat de la fonction sont sur la feuille de calcul, avec un bouton macro pour faire des essais.

Je vous remercie pour votre aide.

Hunter296
 

Pièces jointes

  • classeur3 (version 1)_1.xlsm
    21.6 KB · Affichages: 1
Solution
Bonjour,

Capture.JPG


VB:
Function FonctionLot(ByVal QLot1 As Integer, ByVal QLot2 As Integer, QLot3 As Integer, ByVal QFormule As Integer, ByVal Chaine As String) As String

' SI QTE LOT 1 >= QUANTITE FORMULE alors RESULTAT = LOT 1
' SI QTE LOT 1 < QUANTITE FORMULE alors RESULTAT = LOT 1 LOT 2
' SI QTE LOT 1 = 0 ET QTE LOT 2 >= QUANTITE FORMULE alors RESULTAT =  LOT 2
' SI QTE LOT 2 < QUANTITE FORMULE alors RESULTAT = LOT 2 LOT 3
' SI QTE LOT 1 = 0 ET QTE LOT 2 = 0 ET QTE LOT3 <> QUANTITE FORMULE alors RESULTAT = LOT 3

  FonctionLot = ""
 
  If QLot1 >= QFormule Then FonctionLot = UCase(Chaine) & " 1": Exit Function
  If QLot1 < QFormule And QLot1 > 0 Then FonctionLot = UCase(Chaine) & " 1 " & UCase(Chaine) & " 2": Exit Function...

Eric KERGRESSE

XLDnaute Occasionnel
Bonjour,

Capture.JPG


VB:
Function FonctionLot(ByVal QLot1 As Integer, ByVal QLot2 As Integer, QLot3 As Integer, ByVal QFormule As Integer, ByVal Chaine As String) As String

' SI QTE LOT 1 >= QUANTITE FORMULE alors RESULTAT = LOT 1
' SI QTE LOT 1 < QUANTITE FORMULE alors RESULTAT = LOT 1 LOT 2
' SI QTE LOT 1 = 0 ET QTE LOT 2 >= QUANTITE FORMULE alors RESULTAT =  LOT 2
' SI QTE LOT 2 < QUANTITE FORMULE alors RESULTAT = LOT 2 LOT 3
' SI QTE LOT 1 = 0 ET QTE LOT 2 = 0 ET QTE LOT3 <> QUANTITE FORMULE alors RESULTAT = LOT 3

  FonctionLot = ""
 
  If QLot1 >= QFormule Then FonctionLot = UCase(Chaine) & " 1": Exit Function
  If QLot1 < QFormule And QLot1 > 0 Then FonctionLot = UCase(Chaine) & " 1 " & UCase(Chaine) & " 2": Exit Function
  If QLot1 = 0 And QLot2 >= QFormule Then FonctionLot = UCase(Chaine) & " 2": Exit Function
  If QLot2 < QFormule And QLot2 > 0 Then FonctionLot = UCase(Chaine) & " 2 " & UCase(Chaine) & " 3": Exit Function
  If QLot1 = 0 And QLot2 = 0 And QLot3 <> QFormule Then FonctionLot = UCase(Chaine) & " 3": Exit Function
  If QLot1 = 0 And QLot2 = 0 And QLot3 = QFormule Then FonctionLot = ""

End Function
 

Pièces jointes

  • Capture.JPG
    Capture.JPG
    35.9 KB · Affichages: 3

Hunter296

XLDnaute Nouveau
Bonjour,

Regarde la pièce jointe 1183862

VB:
Function FonctionLot(ByVal QLot1 As Integer, ByVal QLot2 As Integer, QLot3 As Integer, ByVal QFormule As Integer, ByVal Chaine As String) As String

' SI QTE LOT 1 >= QUANTITE FORMULE alors RESULTAT = LOT 1
' SI QTE LOT 1 < QUANTITE FORMULE alors RESULTAT = LOT 1 LOT 2
' SI QTE LOT 1 = 0 ET QTE LOT 2 >= QUANTITE FORMULE alors RESULTAT =  LOT 2
' SI QTE LOT 2 < QUANTITE FORMULE alors RESULTAT = LOT 2 LOT 3
' SI QTE LOT 1 = 0 ET QTE LOT 2 = 0 ET QTE LOT3 <> QUANTITE FORMULE alors RESULTAT = LOT 3

  FonctionLot = ""
 
  If QLot1 >= QFormule Then FonctionLot = UCase(Chaine) & " 1": Exit Function
  If QLot1 < QFormule And QLot1 > 0 Then FonctionLot = UCase(Chaine) & " 1 " & UCase(Chaine) & " 2": Exit Function
  If QLot1 = 0 And QLot2 >= QFormule Then FonctionLot = UCase(Chaine) & " 2": Exit Function
  If QLot2 < QFormule And QLot2 > 0 Then FonctionLot = UCase(Chaine) & " 2 " & UCase(Chaine) & " 3": Exit Function
  If QLot1 = 0 And QLot2 = 0 And QLot3 <> QFormule Then FonctionLot = UCase(Chaine) & " 3": Exit Function
  If QLot1 = 0 And QLot2 = 0 And QLot3 = QFormule Then FonctionLot = ""

End Function
Bonjour Eric,
Merci beaucoup pour votre aide, ça marche impec.
Je vous souhaite une bonne journée
 

Statistiques des forums

Discussions
312 207
Messages
2 086 248
Membres
103 164
dernier inscrit
axelheili2