How to Auto Fill Color with VBA for Loop in Microsoft Excel

If you want to auto fill color specific range using for loop VBA. In this article you will learn for loop in VBA meeting certain condition.

The idea is if range A1:A20 contains anything text or number then excel for loop will auto fill the blank cells.

 

Following is the snapshot of data in column A:

image 1

 

To automatically fill the color in blank cells, we need follow the below steps:

  • Click on Developer tab
  • From Code group, select Visual Basic

image 2

 

  • Enter the following code in the worksheet module

Sub VBAForLoop()

    For x = 1 To 20

    Cells(x, 1).Select

        If Selection.Value = "" Then

            With Selection.Interior

                .Color = 65535

            End With

        End If

    Next x

End Sub

 

image 3

 

  • After copying the code you need to run the macro by using ALT + F8 & select the macro.

image 4

 

In this way we can use VBA to auto fill color using for loop.

Excel Download-How to Auto Fill color with vba for loop

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.