Sunday, April 2, 2017

Loop

Option Explicit

'Endless do loop

Sub TestDoLoop()

    Range("a1").Select
  Do
    ActiveCell.Offset(1, 0).Select
  Loop
 
End Sub

'Do until active cell is not blank
Sub TestDoUntil()

Range("a1").Select

    Do Until ActiveCell.value = ""
    ActiveCell.Offset(1, 0).Select
    Loop
   
End Sub
'Loop until active cell is not blank
Sub TestLoopUntil()
 
   Range(Range("a1"), Range("a1").End(xlToRight)).Copy Range("a17")
   Range("c2").Select
   
    Do
        If ActiveCell.value = "Male" Then
            Range("a" & ActiveCell.Row, ActiveCell.End(xlToRight)).Copy _
                Range("a" & Rows.Count).End(xlUp).Offset(1, 0)
        End If
       
    ActiveCell.Offset(1, 0).Select
   
    Loop Until ActiveCell.value = ""
   
End Sub

'Do While active cell is not blank
Sub TestDoWhile()
   Range(Range("a1"), Range("a1").End(xlToRight)).Copy Range("a17")
   Range("c2").Select
   
    Do While Not ActiveCell.value = ""
        If ActiveCell.value = "Male" Then
            Range("a" & ActiveCell.Row, ActiveCell.End(xlToRight)).Copy _
                Range("a" & Rows.Count).End(xlUp).Offset(1, 0)
        End If
    ActiveCell.Offset(1, 0).Select

    Loop
   
End Sub

'Loop While active cell is not blank
Sub TestLoopWhile()
Range(Range("a1"), Range("a1").End(xlToRight)).Copy Range("a17")
Range("c2").Select

Do
        If ActiveCell.value = "Male" Then
            Range("a" & ActiveCell.Row, ActiveCell.End(xlToRight)).Copy _
                Range("a" & Rows.Count).End(xlUp).Offset(1, 0)
        End If
    ActiveCell.Offset(1, 0).Select

Loop While Not ActiveCell.value = ""

End Sub


No comments:

Post a Comment

*INTERVIEW QUESTIONS

* Ques 01. What is the difference between ByVal and ByRef and which is default ? Ans-  ByRef : If you pass an argument by reference when...