أساسيات VBA إكسل : كيفية استعمال الشروط في VBA‍‍‍‍‍‍ | اكسيل 1#

 كيفية استعمال الشروط في الماكرو  VBA‍‍‍‍‍‍ | اكسيل الجزء 1



في هذه التدوينة سنتعلم كيفية استعمال الشروط في الماكرو - اكسيل تعتبر الشروط مفيدة جدًا في الماكرو VBA ، وسيتم استخدامها لتنفيذ الإجراءات وفقًا لمعايير دقيقة  وهي نفس مبدأ وظيفة IF 

التعليمات الرئيسية هي IF،  في المثال أدناه سنرى كيف يعمل:

If [CONDITION] Then '=> SI condition vraie ALORS 'Instructions si vrai Else '=> SINON (facultatif) 'Instructions si faux End If



كيفية استعمال الشروط في الماكرو  VBA‍‍‍‍‍‍ | اكسيل الجزء 1

الان سننتقل الى التطبيق وسنأخذ المثال الذي تم تطويره في الدرس الخاص بالمتغيرات

كان الغرض منه هو عرض صف الجدول المقابل للرقم الموضح في الخلية F5 في مربع حوار.



إذا أدخلنا حرفًا في F5 ، فسيؤدي ذلك إلى حدوث خطأ ونريد تجنب ذلك.

Sub exemple() 'Déclaration des variables Dim nom As String, prenom As String, age As Integer, numeroLigne As Integer 'Valeurs des variables numeroLigne = Range("F5") + 1 nom = Cells(numeroLigne, 1) prenom = Cells(numeroLigne, 2) age = Cells(numeroLigne, 3) 'Boîte de dialogue MsgBox nom & " " & prenom & ", " & age & " ans" End Sub

سنبدأ بإضافة شرط للتحقق مما إذا كانت قيمة الخلية F5 رقمية بالفعل قبل تشغيل الكود.


Sub exemple() 'Si la valeur entre parenthèses (cellule F5) est numérique (donc si la condition est vraie) alors on exécute les instructions placées entre "Then" et "End If" If IsNumeric(Range("F5")) Then 'Déclaration des variables Dim nom As String, prenom As String, age As Integer, numeroLigne As Integer 'Valeurs des variables numeroLigne = Range("F5") + 1 nom = Cells(numeroLigne, 1) prenom = Cells(numeroLigne, 2) age = Cells(numeroLigne, 3) 'Boîte de dialogue MsgBox nom & " " & prenom & ", " & age & " ans" End If End Sub

دعنا نضيف أيضًا تعليمات للحالة التي لم يتم تحقيق الشرط فيها :

Sub exemple() 'Si F5 est numérique If IsNumeric(Range("F5")) Then 'Déclaration des variables Dim nom As String, prenom As String, age As Integer, numeroLigne As Integer 'Valeurs des variables numeroLigne = Range("F5") + 1 nom = Cells(numeroLigne, 1) prenom = Cells(numeroLigne, 2) age = Cells(numeroLigne, 3) 'Boîte de dialogue MsgBox nom & " " & prenom & ", " & age & " ans" 'Si F5 n'est pas numérique Else 'Boîte de dialogue : avertissement MsgBox "L'entrée """ & Range("F5") & """ n'est pas valide !" 'Suppression du contenu de la cellule F5 Range("F5") = "" End If End Sub

بالنسبة للقيم الغير الرقمية لم تعد مشكلة.

يحتوي جدولنا على 16 صفاً من البيانات (من الصف 2 إلى الصف 17) ، لذا سنتحقق الآن مما إذا كان المتغير أم لا رقم. السطر يكون أكبر من أو يساوي 2 و أقل من أو يساوي 17.

لكن أولاً ، إليك عوامل المقارنة:



كيفية استعمال الشروط في الماكرو  VBA‍‍‍‍‍‍ | اكسيل الجزء 1



بالإضافة إلى العوامل الأخرى المفيدة:



كيفية استعمال الشروط في الماكرو  VBA‍‍‍‍‍‍ | اكسيل الجزء 1



الآن سنقوم باستخدام الشروط أعلاه و بالإضافة إلى عوامل المقارنة المفصلة أعلاه:

Sub exemple() 'Si F5 est numérique If IsNumeric(Range("F5")) Then Dim nom As String, prenom As String, age As Integer, numeroLigne As Integer numeroLigne = Range("F5") + 1 'Si le numéro est dans la bonne plage If numeroLigne >= 2 And numeroLigne <= 17 Then nom = Cells(numeroLigne, 1) prenom = Cells(numeroLigne, 2) age = Cells(numeroLigne, 3) MsgBox nom & " " & prenom & ", " & age & " ans" 'Si le numéro est en dehors de la plage Else MsgBox "L'entrée """ & Range("F5") & """ n'est pas un numéro valide !" Range("F5") = "" End If 'Si F5 n'est pas numérique Else MsgBox "L'entrée """ & Range("F5") & """ n'est pas valide !" Range("F5") = "" End If End Sub


لجعل الماكرو الخاص بنا أكثر ملاءمة ، لا يزال بإمكاننا استبدال 17بواسطة متغير يحتوي على عدد الأسطر

سيسمح لنا ذلك بإضافة / إزالة صفوف إلى جدولنا دون الحاجة إلى تغيير هذا الحد في كل مرة في الكود.

للقيام بذلك ، دعونا ننشئ متغيرًا nbLignes وأضف هذه الوظيفة:


كيفية استعمال الشروط في الماكرو  VBA‍‍‍‍‍‍ | اكسيل الجزء 1



WorksheetFunction 

  CountA لا تقول لنا أي شيء ، لكنها في الواقع وظيفة NBVAL التي ربما تعرفها بالفعل 

نطلب من هذه الوظيفة حساب عدد الخلايا غير الفارغة في العمود الأول ثم نستبدلها 17 عبر nbLignes :

Sub exemple() 'Si F5 est numérique If IsNumeric(Range("F5")) Then Dim nom As String, prenom As String, age As Integer, numeroLigne As Integer, nbLignes As Integer numeroLigne = Range("F5") + 1 nbLignes = WorksheetFunction.CountA(Range("A:A")) 'Fonction NBVAL 'Si le numéro est dans la bonne plage If numeroLigne >= 2 And numeroLigne <= nbLignes Then nom = Cells(numeroLigne, 1) prenom = Cells(numeroLigne, 2) age = Cells(numeroLigne, 3) MsgBox nom & " " & prenom & ", " & age & " ans" 'Si le numéro est en dehors de la plage Else MsgBox "L'entrée """ & Range("F5") & """ n'est pas un numéro valide !" Range("F5") = "" End If 'Si F5 n'est pas numérique Else MsgBox "L'entrée """ & Range("F5") & """ n'est pas valide !" Range("F5") = "" End If End Sub

ELSEIF


ElseIf  يسمح لك بإضافة عدة شروط كما يلي:


If [CONDITION 1] Then '=> SI la condition 1 est vraie ALORS 'Instructions 1 ElseIf [CONDITION 2] Then '=> SINON, SI la condition 2 est vraie ALORS 'Instructions 2 Else '=> SINON 'Instructions 3 End If


إذا كان الشرط 1 صحيحًا ، فسيتم تنفيذ التعليمات 1 ، ثم نخرج من التعليمات If (التي تبدأ بـ If وينتهي عند End Ifا). إذا كان الشرط 1 خاطئًا ، ننتقل إلى الشرط 2. إذا كان هذا صحيحًا ، فسيتم تنفيذ التعليمات 2 ، وإذا لم يكن الأمر كذلك ، فسيتم تنفيذ التعليمات 3.

فيما يلي مثال ، مع A1 من 1 إلى 6 وفي B1 تعليق وفقًا للنتيجة:

Sub commentaires() 'Variables Dim note As Single, commentaire As String note = Range("A1") 'Commentaire en fonction de la note If note = 6 Then commentaire = "Excellent résultat !" ElseIf note >= 5 Then commentaire = "Bon résultat" ElseIf note >= 4 Then commentaire = "Résultat satisfaisant" ElseIf note >= 3 Then commentaire = "Résultat insatisfaisant" ElseIf note >= 2 Then commentaire = "Mauvais résultat" ElseIf note >= 1 Then commentaire = "Résultat exécrable" Else commentaire = "Aucun résultat" End If 'Commentaire en B1 Range("B1") = commentaire End Sub

كيفية القيام باختيار في الماكرو VBA


 الماكرو مع وظيفة الاختيار Select

Sub commentaires() 'Variables Dim note As Single, commentaire As String note = Range("A1") 'Commentaire en fonction de la note Select Case note '<= la valeur à tester (ici, la note) Case Is = 6 commentaire = "Excellent résultat !" Case Is >= 5 commentaire = "Bon résultat" Case Is >= 4 commentaire = "Résultat satisfaisant" Case Is >= 3 commentaire = "Résultat insatisfaisant" Case Is >= 2 commentaire = "Mauvais résultat" Case Is >= 1 commentaire = "Résultat exécrable" Case Else commentaire = "Aucun résultat" End Select 'Commentaire en B1 Range("B1") = commentaire End Sub

لاحظ أنه يمكننا أيضًا إدخال قيم متعددة:

Case Is = 6, 7 'Si la valeur = 6 ou 7 Case Is <> 6, 7 'Si la valeur est différente de 6 ou 7

أو مجموعة من القيم:

Case 6 To 10 'Si la valeur = de 6 à 10


Comments
No comments
Post a Comment



    Reading Mode :
    Font Size
    +
    16
    -
    lines height
    +
    2
    -