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, 6 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
-
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
37 minutes ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
38 minutes ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
7 hours, 39 minutes ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
8 hours, 46 minutes ago -
0Patch, where to begin
by
cassel23
2 hours, 48 minutes ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
22 hours, 24 minutes ago -
89 million Steam account details just got leaked,
by
Alex5723
10 hours, 9 minutes ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
1 day, 6 hours ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
21 hours, 55 minutes ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
9 hours, 3 minutes ago -
Installer program can’t read my registry
by
Peobody
4 hours ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
19 hours, 42 minutes ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
1 day, 3 hours ago -
False error message from eMClient
by
WSSebastian42
1 day, 18 hours ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
2 days, 3 hours ago -
Office 2021 Perpetual for Mac
by
rebop2020
2 days, 4 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
1 day ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
2 days, 7 hours ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
1 day, 8 hours ago -
Outdated Laptop
by
jdamkeene
2 days, 13 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
2 days, 18 hours ago -
Another big Microsoft layoff
by
Charlie
2 days, 18 hours ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
15 hours, 47 minutes ago -
May 2025 updates are out
by
Susan Bradley
1 hour, 20 minutes ago -
Windows 11 Insider Preview build 26200.5600 released to DEV
by
joep517
3 days ago -
Windows 11 Insider Preview build 26120.3964 (24H2) released to BETA
by
joep517
3 days ago -
Drivers suggested via Windows Update
by
Tex265
3 days ago -
Thunderbird release notes for 128 esr have disappeared
by
EricB
20 hours, 23 minutes ago -
CISA mutes own website, shifts routine cyber alerts to X, RSS, email
by
Nibbled To Death By Ducks
3 days, 7 hours ago -
Apple releases 18.5
by
Susan Bradley
3 days, 1 hour 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.