Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 12th, 2009, 11:23 AM
prabha_friend
Guest
 
Posts: n/a
Default Finding any coloured cell

Today we had to solve a very peculiar but seemingly very very easy scenario but its not as we thought. Actually the task we assigned is to check a workbook for filled cells i.e filled by any color. If there is a filled one then we have to report 'no' in a statement. Yeah initially i also thought the same idea. Going cell by cell and evaluating its colorindex. To say you the truth i hates this idea. Actually i want to achieve this in a smartest way possible. First i considered about the 'find and replace' functionality. I started to record a macro entered no string in the text to find and selected the 'no fill' in the formats. I eagerly expected to find an expression like this 'where cell.fill color = none' and dreamed about changing the operator to '<>' and solving the problem. But i never imagined that the find function is depending upon an object call findformat and its properties like interior and fill format like all. Actually it first assigns the object with fillcolour constant before calling the find function thus i cant assign a value with '<>'. I still believe there must be a smarter way other than looping each and every cell of the workbook. Thanks in advance.
 
Old December 13th, 2009, 04:03 PM
Authorized User
 
Join Date: Nov 2007
Posts: 48
Thanks: 0
Thanked 4 Times in 4 Posts
Default

prabha_friend,

Quote:
If there is a filled one then we have to report 'no' in a statement.
Where do you have to report the 'no' statements (in a cell in a certain column)?


The following code will find the cells with a background color in the active worsheet used range, and report in a message box the address of the cell.


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Adding the Macro
1. Copy the below macro, by highlighting the macro code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
 
Option Explicit
Sub FindColor()
' stanleydgromjr, 12/13/2009
Dim c As Range
For Each c In ActiveSheet.UsedRange
  If c.Interior.ColorIndex <> xlNone Then
    MsgBox "Cell " & c.Address & " contains a background color."
  End If
Next c
End Sub

Then run the "FindColor" macro.
__________________
stanleydgromjr

Windows 8.1, Excel 2007.





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to display different coloured text in textbox codeuser BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 1 August 9th, 2009 05:46 AM
[VB2005]Finding last cell of the column in excel bellaelysium Visual Basic 2005 Basics 2 June 22nd, 2008 11:33 PM
Finding Adjacent Empty Cell keithd Excel VBA 7 July 19th, 2006 06:20 AM
Lose cell Text when editing cell in VSFlexGrid 6 bobcratchet VB How-To 0 July 30th, 2004 09:32 AM
Coloured Text in Listview omb Beginning VB 6 1 September 11th, 2003 05:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.