Excel - Change cell colour based on a date
I'm trying to use this Sub I found on the forum, but I'm having lots of problems because in my sheet, I've got some date cells with * or other without any information inside and when the Sub gets to these cells multiple errors occurs, could anyone help me with this issue?
Thank you very much in advance.
Sub WorkOutTime()
dim columnToUse as string
columnToUse = "X"
Dim expired As Integer
expired = 3 'red
Dim twoDays As Integer
twoDays = 8 'blue
Dim sevenDays As Integer
sevenDays = 27 ' yellow
Dim fourteenDays As Integer
fourteenDays = 7 ' purple
Dim currentCell As Integer
currentCell = 1
Do While (True)
If (Range(columnToUse & currentCell).Value = "") Then
Exit Do
End If
Dim timeNow As Date
timeNow = Date
Dim willContinue As Boolean
willContinue = True
Dim dateDifference As Integer
dateDifference = dateDiff("d", timeNow, Range(columnToUse & currentCell).Value)
If dateDifference >= 14 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = fourteenDays
willContinue = False
End If
If dateDifference <= 7 And dateDifference > 2 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = sevenDays
End If
If dateDifference <= 2 And dateDifference >= 0 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = twoDays
End If
If dateDifference < 0 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = expired
End If
currentCell = currentCell + 1
Loop
End Sub
microsoft-excel worksheet-function
add a comment |
I'm trying to use this Sub I found on the forum, but I'm having lots of problems because in my sheet, I've got some date cells with * or other without any information inside and when the Sub gets to these cells multiple errors occurs, could anyone help me with this issue?
Thank you very much in advance.
Sub WorkOutTime()
dim columnToUse as string
columnToUse = "X"
Dim expired As Integer
expired = 3 'red
Dim twoDays As Integer
twoDays = 8 'blue
Dim sevenDays As Integer
sevenDays = 27 ' yellow
Dim fourteenDays As Integer
fourteenDays = 7 ' purple
Dim currentCell As Integer
currentCell = 1
Do While (True)
If (Range(columnToUse & currentCell).Value = "") Then
Exit Do
End If
Dim timeNow As Date
timeNow = Date
Dim willContinue As Boolean
willContinue = True
Dim dateDifference As Integer
dateDifference = dateDiff("d", timeNow, Range(columnToUse & currentCell).Value)
If dateDifference >= 14 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = fourteenDays
willContinue = False
End If
If dateDifference <= 7 And dateDifference > 2 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = sevenDays
End If
If dateDifference <= 2 And dateDifference >= 0 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = twoDays
End If
If dateDifference < 0 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = expired
End If
currentCell = currentCell + 1
Loop
End Sub
microsoft-excel worksheet-function
1
Would Conditional Formatting not work? Also, what error(s) do you get, where?
– BruceWayne
Jan 25 at 16:21
add a comment |
I'm trying to use this Sub I found on the forum, but I'm having lots of problems because in my sheet, I've got some date cells with * or other without any information inside and when the Sub gets to these cells multiple errors occurs, could anyone help me with this issue?
Thank you very much in advance.
Sub WorkOutTime()
dim columnToUse as string
columnToUse = "X"
Dim expired As Integer
expired = 3 'red
Dim twoDays As Integer
twoDays = 8 'blue
Dim sevenDays As Integer
sevenDays = 27 ' yellow
Dim fourteenDays As Integer
fourteenDays = 7 ' purple
Dim currentCell As Integer
currentCell = 1
Do While (True)
If (Range(columnToUse & currentCell).Value = "") Then
Exit Do
End If
Dim timeNow As Date
timeNow = Date
Dim willContinue As Boolean
willContinue = True
Dim dateDifference As Integer
dateDifference = dateDiff("d", timeNow, Range(columnToUse & currentCell).Value)
If dateDifference >= 14 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = fourteenDays
willContinue = False
End If
If dateDifference <= 7 And dateDifference > 2 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = sevenDays
End If
If dateDifference <= 2 And dateDifference >= 0 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = twoDays
End If
If dateDifference < 0 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = expired
End If
currentCell = currentCell + 1
Loop
End Sub
microsoft-excel worksheet-function
I'm trying to use this Sub I found on the forum, but I'm having lots of problems because in my sheet, I've got some date cells with * or other without any information inside and when the Sub gets to these cells multiple errors occurs, could anyone help me with this issue?
Thank you very much in advance.
Sub WorkOutTime()
dim columnToUse as string
columnToUse = "X"
Dim expired As Integer
expired = 3 'red
Dim twoDays As Integer
twoDays = 8 'blue
Dim sevenDays As Integer
sevenDays = 27 ' yellow
Dim fourteenDays As Integer
fourteenDays = 7 ' purple
Dim currentCell As Integer
currentCell = 1
Do While (True)
If (Range(columnToUse & currentCell).Value = "") Then
Exit Do
End If
Dim timeNow As Date
timeNow = Date
Dim willContinue As Boolean
willContinue = True
Dim dateDifference As Integer
dateDifference = dateDiff("d", timeNow, Range(columnToUse & currentCell).Value)
If dateDifference >= 14 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = fourteenDays
willContinue = False
End If
If dateDifference <= 7 And dateDifference > 2 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = sevenDays
End If
If dateDifference <= 2 And dateDifference >= 0 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = twoDays
End If
If dateDifference < 0 And willContinue Then
Range(columnToUse & currentCell).Interior.ColorIndex = expired
End If
currentCell = currentCell + 1
Loop
End Sub
microsoft-excel worksheet-function
microsoft-excel worksheet-function
edited Jan 25 at 15:55
cybernetic.nomad
2,591617
2,591617
asked Jan 25 at 13:12
MarneMarne
1
1
1
Would Conditional Formatting not work? Also, what error(s) do you get, where?
– BruceWayne
Jan 25 at 16:21
add a comment |
1
Would Conditional Formatting not work? Also, what error(s) do you get, where?
– BruceWayne
Jan 25 at 16:21
1
1
Would Conditional Formatting not work? Also, what error(s) do you get, where?
– BruceWayne
Jan 25 at 16:21
Would Conditional Formatting not work? Also, what error(s) do you get, where?
– BruceWayne
Jan 25 at 16:21
add a comment |
0
active
oldest
votes
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1398357%2fexcel-change-cell-colour-based-on-a-date%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1398357%2fexcel-change-cell-colour-based-on-a-date%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Would Conditional Formatting not work? Also, what error(s) do you get, where?
– BruceWayne
Jan 25 at 16:21