Do While loop in excel vba | Excel VBA lesson - 12 | excel vba tutorial for beginners in hindi
Do While Loop
A ‘Do While’ loop allows you to check for a condition and run
the loop while that condition is met (or is TRUE).
For Next Loop मे आपने पढ़ा की आपने कितने times या kitne numbers ऑफ times code को रन करना है इसके विपरीत Do while मे ये नहीं बताते की कितनी बार रन करना है, यह condition के आधार पर loop को चलाने के लिए यूज किया जाता है इसके अंतर्गत एक condition को define किया जाता है, जिसके True होने पर loop की स्टेटमेंट execute होती है ओर जैसे ही ये condition false होती है तो ये लूप से बाहर आ जाता है। जैसे आपने बताया नंबर <10 हो या >10 ना हो या जबतक किन्ही संख्याओं का जोड़ 100 ना हो या blank होने तक इस प्रकार, इसको आप इस Diagram से भी समझ सकते हैं:
सबसे पहले do while सुरू करेंगे ओर condition देंगे तो जब condition false होगी तो ये सीधा बाहर आ जाएगा ओर यदि condition True होती है तो ये इसको फिरसे चेक करेगा यानि जबतक condition true होती रहेगी तो ये इसको चेक करता रहेगा ओर जैसे ही condition False होगी ये इससे बाहर आ जाएगा।
|
A |
1 |
CGG |
2 |
MK |
3 |
12 |
4 |
CC |
6 |
|
7 |
|
इस table के अनुसार हम जो code लिखें
वो तब तक रन करे जब तक उसको blank cell ना मिल जाए तो हम
इसके लिए code लिखेंगे।
Sub doloop1 ()
Range(“A1”)।Select
Do While ActiveCell.value
<> “”
ActiveCell.Offset (1, 0).Select
Loop
End Sub
तो ये code तब तक रन करेगा जब तक की इसको blank
cell नहीं मिल जाता अगर हम आगे ओर भी value type कर देते हैं तो ये code वहाँ तक रन करेगा।
आप इसको For Next Loop से भी चला सकते हैं। उसके लिए हमे
लास्ट Row नंबर बताना पड़ेगा जैसे उपर की table मे लास्ट Row हमारे पास 6 नंबर होनी चाहिए तो VBA
को लास्ट row हम कैसे बताएँगे उसके लिए नीचे
हमे code लिखा है किस्से आप समझ सकते हैं
Sub Fornext_do ()
Dim M As Long
Dim Lastrow As long
Range (“A1000000”).End(xlUp).Select
Lastrow = ActiveCell.Row
For M = 1 to Lastrow
Cells(M, 1).Select
Next m
End Sub
एक Example ओर देख लेते हैं। उधारण के लिए A1 सेल से A10
सेल तक Counting लिखी आ जाए तो उसके लिए हम
लिखेंगे।
Sub Dowhile1 ()
Dim M as Integer
M = 1
Do while M <= 10
Sheet1.Cells (R, 1).Value = R
R = R + 1
Loop
There are two types of syntax in the Do While Loop.
Do [While
condition]
[Code block
to Execute]
Loop
and
Do
[Code block
to Execute]
Loop [While
condition]
The difference between these two is that in the first, the While
condition is checked first before any code block is executed, and in the second
case, the code block is executed first and then the While condition is checked.
This means that if the While condition is False is both the
cases, the code will still run at least once in the second case (as the ‘While’
condition is checked after the code has been executed once).
Now let’s see some examples of using Do While loops in VBA.
Example 1 – Add First 10 Positive Integers using VBA
Suppose you want to add the first ten positive integers using
the Do While loop in VBA.
To do this, you can use the Do While loop until the next number
is less than or equal to 10. As soon as the number is greater than 1o, your
loop would stop.
Here is the VBA code that will run this Do While loop and the
show the result in a message box.
Sub
AddFirst10PositiveIntegers()
Dim i As
Integer
i = 1
Do While i
<= 10
Result =
Result + i
i = i + 1
Loop
MsgBox
Result
End Sub
The above loop continues to work until the value of ‘i’ becomes
11. As soon as it becomes 11, the loop ends (as the While condition becomes
False).
Within the loop, we have used a Result variable that holds the
final value Once the loop is completed, a message box shows the value of the
‘Result’ variable.
Example 2 – Enter Dates For the Current Month
Let’s say you want to enter all the dates of the current month
into a worksheet column.
You can do that by using the following Do While loop code:
Sub
EnterCurrentMonthDates()
Dim CMDate
As Date
Dim i As
Integer
i = 0
CMDate =
DateSerial(Year(Date), Month(Date), 1)
Do While
Month(CMDate) = Month(Date)
Range("A1").Offset(i,
0) = CMDate
i = i + 1
CMDate =
CMDate + 1
Loop
End Sub
The above code would enter all the dates in the first column of
the worksheet (starting from A1). The loops continue till the Month value of
the variable ‘CMDate’ matches that of the current month.
Exit Do Statement
You can use the Exit Do statement to come out of the loop. As
soon as the code executes the ‘Exit Do’ line, it comes out of the Do While loop
and passes the control to the next line right after the loop.
For example, if you want to enter the first 10 dates only, then
you can exit the loop as soon as the first 10 dates are entered.
The below code will do this:
Sub
EnterCurrentMonthDates()
Dim CMDate
As Date
Dim i As
Integer
i = 0
CMDate =
DateSerial(Year(Date), Month(Date), 1)
Do While
Month(CMDate) = Month(Date)
Range("A1").Offset(i,
0) = CMDate
i = i + 1
If i >=
10 Then Exit Do
CMDate =
CMDate + 1
Loop
End Sub
In the above code,
the IF statement is used to check if the value of i is
greater than 10 or not. As soon as the value of ‘i’ becomes 10, Exit Do
statement is executed and the loop ends.
Post a Comment