VBA IF Statement

Simply put the VBA if statement determines if a condition is true or false and then performs some action based on the results of that determination. In the three if statement examples below, I add a level of complexity and utility.  Pay attention to how they flow
If activecell.value > .2 then
activecell.interior.color = 255
end if
The first line of this simple if statement checks to see if the active cells value (the currently selected cell) is greater than .2.  If true, it reads the next line and sets the color to 255 Red. If false, it jumps to the end if and creates no action.
If activecell.value > .2 then activecell.interior.color = 255 else activecell.interior.color = 65535 end if
By adding the else, we are essentially saying if its true color it red and if it is not, color it yellow
If activecell.value > .2 then activecell.interior.color = 255 elseif activecell.value > .18 then activecell.interior.color = 65535 else activecell.interior.color = 5287936 end if
If the active cell value is greater than .2, make it red. else if it is greater than .18, make it yellow. otherwise make it green.
If you were able to utilize what you learned here or improve the code,
please leave a comment Here

If you enjoyed this post or found it helpful andl would like to say hello and
buy me a cup of coffee Click here

Raymond Mills MBA, MS
Raymond Mills, M.B.A., M.S.  has spent over 20 years of his career as Accountant, Investment Bank and Credit Card Technical Auditor/ Data Analyst.  His specialty was using Excel to get Big Databases including Teradata, Oracle,  Squel Server and Sybase to give up their secrets. Ray has said “I love nothing better than using VBA to unleash the power of Microsoft Office.” You can contact Ray @ 484 574-3190 or by emailing him Here

If you have a challenge with Excel, Access or Word and would like to speak with Ray,   You can get his contact details by clicking here: Contact Me

Comments are closed.