VBA example - Excel 2007: VBA loop to create animation


VBA loop to create animation

 Generating animation in Excel is rarely seen. In this example, we are going to build a simple program that generates a simple animation on the color patterns when you push the Animate button.

To run this example VBA code, you will need a Form, and a CommandButton.


VBA for Excel: VBA loop example code to generate animaion

VBA code for this example:


Option Explicit   Private Sub CommandButton1_Click()
   Dim l As Integer    l = 1    
 
While l < 50 'animate the color patterns
   
       
Style1
    
       
Style2
   
       
l = l + 1
 
  
Wend
   

End Sub
 

Sub Style1()
  Dim i, j As Integer
   i = 10
   j = 10 For i = 1 To 10  
  
For j = 10 To 1 Step -1
   
        
If (i + j = 11) Then
       
            
Cells(i, j).Interior.Color = RGB(200, 10, 10)
                 
        
ElseIf (i + j > 11) Then
       
              
Cells(i, j).Interior.Color = RGB(10, 10, 200)
               
        
ElseIf (i + j < 11) Then
    
           
Cells(i, j).Interior.Color = RGB(10, 10, 200)
                  
       
End If
           
 
Next

 Next
End Sub  

Sub Style2()
  Dim i, j As Integer
  i = 10
  j = 10
  For i = 1 To 10  
  
For j = 1 To 10
      
     
If (i = j) Then
     
          
Cells(i, j).Interior.Color = RGB(200, 10, 10)
                    
      
ElseIf (i > j) Then
     
           
Cells(i, j).Interior.Color = RGB(10, 10, 200)
                      
     
ElseIf (i < j) Then
         
          
Cells(i, j).Interior.Color = RGB(10, 10, 200)
                       
    
End If
       
   
Next
 Next
End Sub

Comments

Pinka comment

 Pinka

Good One!! :)


2016-10-25




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.