Kurniawan Blogs

Just share about knowladge

  • Full Screen
  • Wide Screen
  • Narrow Screen
  • Increase font size
  • Default font size
  • Decrease font size

Perulangan atau Loop pada Macro Excel

Mau cetak print area pada excel sekaligus 47 halaman dengan data yang berbeda berdasarkan lookup data, dengan sedikit record macro print (maklum gak bisa VB :D) dan tambahi sedikit combinasi lookup berikut jadi deh... sekali klik aja nyetaknya...

Syntax

Do [{While | Until} condition]
[statements]
[Exit Do]
[statements]

Loop

Or

Do
[statements]
[Exit Do]
[statements]

Loop [{While | Until} condition]

And

While condition
[statements]

Wend

While … wend is the same with do while … loop, and do while … loop is more structured and flexible compare to while … wend statement.

Examples:

' example of do while ... loop
Sub ChkFirstWhile()
    counter = 0
    myNum = 20
    ' do while myNum > 10
    ' we perform condition check before we do loop
    ' if the first check fail to meet the criteria
    ' no loop will be perform
    Do While myNum > 10
        myNum = myNum - 1
        counter = counter + 1
    Loop
    MsgBox "The loop made " & counter & " repetitions."
End Sub

' example of do ... loop while Sub ChkLastWhile() counter = 0 myNum = 9 ' do ... loop while myNum > 10 ' we perform condition check after the loop ' we do at least 1 loop Do myNum = myNum - 1 counter = counter + 1 Loop While myNum > 10 MsgBox "The loop made " & counter & " repetitions." End Sub

' example of do until ... loop ' the principle is the same with do while ... loop Sub ChkFirstUntil() counter = 0 myNum = 20 Do Until myNum = 10 myNum = myNum - 1 counter = counter + 1 Loop MsgBox "The loop made " & counter & " repetitions." End Sub

' example of do ... loop until ' the principle is the same with do ... loop while Sub ChkLastUntil() counter = 0 myNum = 1 Do myNum = myNum + 1 counter = counter + 1 Loop Until myNum = 10 MsgBox "The loop made " & counter & " repetitions." End Sub

' example of while ... wend loop Sub ChkWhile() Dim Counter Counter = 0 ' Initialize variable. While Counter < 20 ' Test value of Counter. Counter = Counter + 1 ' Increment Counter. Wend ' End While loop when Counter > 19. Debug.Print Counter ' Prints 20 in the Immediate window. End Sub

Sumber : http://excelvbamacro.com/excel-vba-looping-tutorial.html

link terkait :

http://excel.bigresource.com/Macro-to-loop-through-sheets-and-print-them-out-using-an-array-uAdaYqfl.html

Add comment

Security code
Refresh

Latest Comment

RSS
You are here: N e w s N e w s Software News Perulangan atau Loop pada Macro Excel