jeudi 29 décembre 2016

دورة VBA: الحلقات

دورة VBA: حلقات

في حين

الحلقات تسمح تكرار تصريحات لعدد من المرات، والتي يمكن أن يكون الوقت المدخر.
وفيما يلي أرقام رمز خلايا العمود (خط 1-12):

Sub boucle_while()
Cells(1, 1) = 1
Cells(2, 1) = 2
Cells(3, 1) = 3
Cells(4, 1) = 4
Cells(5, 1) = 5
Cells(6, 1) = 6
Cells(7, 1) = 7
Cells(8, 1) = 8
Cells(9, 1) = 9
Cells(10, 1) = 10
Cells(11, 1) = 11
Cells(12, 1) = 12
End Sub
هذا الرمز هو متكررة جدا ...
الآن تخيل أنه من الضروري أن عددهم عدة مئات من الخطوط ... لذلك على فهم قيمة وخلق الحلقات.
هنا هو حلقة في حين:

Sub boucle_while()
While [condition]
'Instructions Wend
End Sub
طالما كان الشرط صحيحا، يتم تنفيذ تعليمات مرارا وتكرارا (الحرص على عدم خلق حلقة لا نهائية).
ماكرو المتكررة هنا أعلاه مع حلقة في حين:
Sub boucle_while()
Dim numero As Integer
numero = 1 'Numéro de départ (correspond ici au n° de ligne et au n° de numérotation) While numero <= 12 'TANT QUE la variable numero est <= 12, la boucle est répétée Cells(numero, 1) = numero 'Numérotation numero = numero + 1 'Le numéro est augmenté de 1 à chaque boucle Wend
End Sub
مع هذه الحلقة، إذا كان لنا أن الاتصال الهاتفي 500 خطوط، فإنه يكفي لاستبدال 12 من 500 ...

القيام حلقة

هذه الحلقة يعمل بنفس الطريقة التي وند بينما (طالما كان الشرط صحيحا، يتم تنفيذ حلقة):
 
Sub boucle_do_while()
Do While [condition]
'Instructions Loop
End Sub
ويمكن أيضا أن حالة وضعها في نهاية حلقة هل حلقة، وهو ما يعني أن يتم تنفيذ التعليمات مرة واحدة على الأقل:

 
Sub boucle_do_while()
Do
'Instructions Loop While [condition]
End Sub
 
بدلا من تكرار حلقة طالما كان الشرط صحيحا، فمن الممكن للخروج من حلقة عندما كان الشرط صحيحا عن طريق استبدال الوقت حتى:
 
Sub boucle_do_while()
Do Until [condition]
'Instructions
Loop
End Sub

إلى

 

Sub boucle_for()
For i = 1 To 5
'Instructions Next
End Sub
وتتكرر حلقة لهنا 5 مرات.
في كل تكرار للحلقة، وزادت ط متغير تلقائيا بنسبة 1:
 
Sub boucle_for()
For i = 1 To 5
MsgBox i
Next
End Sub

الخروج من حلقة قبل الأوان

فمن الممكن للخروج من لحلقة قبل الأوان مع العبارة التالية:
 
Exit For 'Quitter une boucle For
وهنا مثال على ذلك:
 
Sub boucle_for()
Dim max_boucles As Integer
max_boucles = Range( "A1" ) 'En A1 : une limite de répétitions de la boucle est définie For i = 1 To 7 'Boucles prévues : 7 If i > max_boucles Then 'Si A1 est vide ou contient un nombre < 7, diminution du nb de boucles Exit For 'Si condition vraie, on quitte la boucle For End If
MsgBox i
Next
End Sub
تعليمات الخروج الأخرى:

Exit Do 'Quitter une boucle Do Loop
Exit Sub 'Quitter une procédure
Exit Function 'Quitter une fonction
 
لتطبيق ما تم رأيناه حتى الآن، ونحن خلق خطوة خطوة ماكرو من شأنها أن لون 10X10 الخلايا (متقلب الأحمر والأسود) من الخلية المحددة، المعاينة:

 
هنا هو نقطة انطلاق التمرين:
Sub exercice_boucles()
Const NB_CASES As Integer = 10 'Nombre de cellules à colorer
'...
End Sub
 
أولا، إضافة لحلقة من شأنها أن لون خلايا سوداء في العمود A من 1 إلى 10 (10 كونها ثابتة NB_CASES) المعاينة:

 
نتوقف لحظة لإنشاء هذه الحلقة قبل أن ينتقل إلى الحل
الحل :
Sub exercice_boucles()
Const NB_CASES As Integer = 10 'Nombre de cellules à colorer
For l = 1 To NB_CASES 'l => n° ligne
Cells(l, 1).Interior.Color = RGB(0, 0, 0) 'Noir
Next
End Sub
والخطوة التالية هي اللون الأحمر من خلية 2 مع (اعتمادا على عدد نيف / حتى خط) المعاينة إذا:

 
الحل :
Sub exercice_boucles()
Const NB_CASES As Integer = 10 'Nombre de cellules à colorer
For l = 1 To NB_CASES 'l => n° ligne
If l Mod 2 = 0 Then 'Mod => est le reste d'une division
Cells(l, 1).Interior.Color = RGB(200, 0, 0) 'Rouge
Else
Cells(l, 1).Interior.Color = RGB(0, 0, 0) 'Noir
End If
Next
End Sub
إذا كان الشرط وزارة الدفاع 2 = 0 وسائل: إذا كان باقي القسمة على 2 هي 0 ...
أقرانهم فقط رقم خط لديهم بقية تساوي 0 عندما مقسوما على 2.
الآن خلق حلقة من شأنها أن تعمل الحلقة الأولى من 10 الأعمدة، المعاينة:

 
الحل :
Sub exercice_boucles()
Const NB_CASES As Integer = 10 'Damier de 10x10 cellules
For l = 1 To NB_CASES 'l => n° ligne
For c = 1 To NB_CASES 'c => n° colonne
If l Mod 2 = 0 Then
Cells(l, c).Interior.Color = RGB(200, 0, 0) 'Rouge
Else
Cells(l, c).Interior.Color = RGB(0, 0, 0) 'Noir
End If
Next
Next
End Sub
وتداخل في الحلقة الثانية ضمن أولا.
لتحقيق هذه النتيجة ...

 
استبدال:
If l Mod 2 = 0 Then
من قبل:
If (l + c) Mod 2 = 0 Then
يبقى فقط أن تعديل التعليمات البرمجية لإنشاء مربعات من الخلية النشطة (بدلا من A1) المعاينة:

 
الحل :




Sub exercice_boucles()
Const NB_CASES As Integer = 10 'Damier de 10x10 cellules
Dim lig As Integer , col As Integer ' => ajout de 2 variables
'Décalage (lignes) à partir de la première cellule = n° de ligne de la cellule active - 1
lig = ActiveCell.Row - 1
'Décalage (colonnes) à partir de la première cellule = n° de colonne de la cellule active - 1
col = ActiveCell.Column - 1
For l = 1 To NB_CASES 'N° ligne
For c = 1 To NB_CASES 'N° colonne
If (l + c) Mod 2 = 0 Then
'Cells(n° de ligne + décalage lignes, n° de colonne + décalage colonnes)...
Cells(l + lig, c + col).Interior.Color = RGB(200, 0, 0) 'Rouge
Else
Cells(l + lig, c + col).Interior.Color = RGB(0, 0, 0) 'Noir
End If
Next
Next
End Sub

Aucun commentaire:

Enregistrer un commentaire

مواقيت الصلاة


Propellerads