Anyone know how to do the above in excel? I would be able to do it with conditional formatting but i need 5 conditions, MS only gave us three!
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Changing Cell colours in Excel using VBA (Excel ’97)
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Changing Cell colours in Excel using VBA (Excel ’97)
- This topic has 9 replies, 3 voices, and was last updated 23 years, 7 months ago.
Viewing 0 reply threadsAuthorReplies-
H. Legare Coleman
AskWoody PlusOctober 30, 2001 at 11:15 pm #549355You can change a cell color like this:
Activesheet.Range("A1").Interior.Colorindex = 5
The above will change the color of cell A1 on the active sheet to Blue.
The above will work in a Subroutine. However, from the sound of this message and your message in the Excel forum, it sounds like you want to do this from a User Defined Function. The above will not work from a User Defined Function because a User Defined Function can only return values to the cell that executed it, it can not make other changes to a worksheet.
To do what I think you want to do, you are probably going to have to use the worksheet change event routine, check your conditions there, and change the color. I you can be a bit more specific about what you want to do, we can help with the code.
-
WSJukebox
AskWoody LoungerNovember 1, 2001 at 6:37 pm #549676Sorry, I’m new to this!
If there is a date in say cell A1, i would have the =WEEKDAY(A1,2) formula in cell B1 to tell me what day the date lands on, i then want to put another formula into cell C1 to change the colour of cell A1 depending on the day in Cell B1. I could do it with conditional formatting if they gave me 5 arguments (i need a different colour for each day of the working week) but as usual MS have come up short. Hope anyone can help, cheers Jamie. -
H. Legare Coleman
AskWoody PlusNovember 1, 2001 at 8:37 pm #549712As I stated earlier, there is no way for a User Defined function to modify another cell, It can only return a value to the cell that called it. So, there is no way to do what you want the way that you described it.
However, the following code, placed in the Worksheet Change event routine for the sheet in question should do what you are trying to do:
Private Sub Worksheet_Change(ByVal Target As Range) Dim oCell As Range, oRange As Range Set oRange = Intersect(Target, Range("A1:A10")) If oRange Is Nothing Then Exit Sub End If For Each oCell In oRange Select Case Weekday(oCell.Value, vbMonday) Case 2 oCell.Interior.ColorIndex = 7 Case 3 oCell.Interior.ColorIndex = 6 Case 4 oCell.Interior.ColorIndex = 5 Case 5 oCell.Interior.ColorIndex = 4 Case 6 oCell.Interior.ColorIndex = 3 Case Else oCell.Interior.ColorIndex = xlColorIndexAutomatic End Select Next oCell End Sub
Since you weren’t very specific, this code assumes that the dates in question are in the range A1:A10. You would need to modify that to fit your needs. You will probably also want to change the colors that this sets.
-
WSJukebox
AskWoody LoungerNovember 2, 2001 at 8:07 pm #549898I have attached the file i need the above to happen on. I can’t explaint it very well. I want the cell with the date in to change colour according to the day that date lands on! Please help. I’ve tried the code you told me and it says compiled error. Since i have NO experiance using VBA i don’t know what is wrong. thanks for trying to help but could you PLEASE try again?
thanks
Jamie -
H. Legare Coleman
AskWoody Plus -
WSJukebox
AskWoody Lounger -
H. Legare Coleman
AskWoody PlusNovember 3, 2001 at 2:32 pm #550012Open the workbook and go to the VBA Editor. In the Project Explorer at the left of the screen double click on the Sheet1 object. In the left drop down list above the edit area select Worksheet. In the right had drop down list select Change. The code should now be displayed in the edit area.
-
WSAndrew Cronnolly
AskWoody LoungerNovember 3, 2001 at 6:37 pm #550022Jamie,
If you have little experience using the VB editor and object browser, you can right click on the sheet tab for Sheet1 (or whatever sheet has the code) and select View Code. You could then just copy all the code you see and paste into another sheet (right click – view code) if that is what you require.
Andrew C
-
WSJukebox
AskWoody Lounger
-
-
-
-
Viewing 0 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Very Quarrelsome Taskbar!
by
CWBillow
49 minutes ago -
Move OneNote Notebook OFF OneDrive and make it local
by
CWBillow
3 hours, 33 minutes ago -
Microsoft 365 to block file access via legacy auth protocols by default
by
Alex5723
4 hours, 51 minutes ago -
Is your battery draining?
by
Susan Bradley
20 minutes ago -
The 16-billion-record data breach that no one’s ever heard of
by
Alex5723
6 hours, 49 minutes ago -
Weasel Words Rule Too Many Data Breach Notifications
by
Nibbled To Death By Ducks
7 hours, 25 minutes ago -
Windows Command Prompt and Powershell will not open as Administrator
by
Gordski
10 hours, 40 minutes ago -
Intel Management Engine (Intel ME) Security Issue
by
PL1
1 hour, 23 minutes ago -
Old Geek Forced to Update. Buy a Win 11 PC? Yikes! How do I cope?
by
RonE22
3 hours, 54 minutes ago -
National scam day
by
Susan Bradley
6 hours, 50 minutes ago -
macOS Tahoe 26 the end of the road for Intel Macs, OCLP, Hackintosh
by
Alex5723
4 hours, 5 minutes ago -
Cyberattack on some Washington Post journalists’ email accounts
by
Bob99
1 day, 8 hours ago -
Tools to support internet discussions
by
Kathy Stevens
1 day, 15 hours ago -
How get Group Policy to allow specific Driver to download?
by
Tex265
23 hours, 34 minutes ago -
AI is good sometimes
by
Susan Bradley
1 day, 15 hours ago -
Mozilla quietly tests Perplexity AI as a New Firefox Search Option
by
Alex5723
1 day, 5 hours ago -
Perplexity Pro free for 12 mos for Samsung Galaxy phones
by
Patricia Grace
2 days, 15 hours ago -
June KB5060842 update broke DHCP server service
by
Alex5723
2 days, 14 hours ago -
AMD Ryzen™ Chipset Driver Release Notes 7.06.02.123
by
Alex5723
2 days, 18 hours ago -
Excessive security alerts
by
WSSebastian42
1 day, 9 hours ago -
* CrystalDiskMark may shorten SSD/USB Memory life
by
Alex5723
3 days, 4 hours ago -
Ben’s excellent adventure with Linux
by
Ben Myers
1 hour, 15 minutes ago -
Seconds are back in Windows 10!
by
Susan Bradley
2 days, 15 hours ago -
WebBrowserPassView — Take inventory of your stored passwords
by
Deanna McElveen
1 day, 8 hours ago -
OS news from WWDC 2025
by
Will Fastie
18 hours, 52 minutes ago -
Need help with graphics…
by
WSBatBytes
1 day, 23 hours ago -
AMD : Out of Bounds (OOB) read vulnerability in TPM 2.0 CVE-2025-2884
by
Alex5723
3 days, 19 hours ago -
Totally remove or disable BitLocker
by
CWBillow
2 days, 18 hours ago -
Windows 10 gets 6 years of ESU?
by
n0ads
2 days, 21 hours ago -
Apple, Google stores still offer China-based VPNs, report says
by
Nibbled To Death By Ducks
4 days, 6 hours ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.