Temporary cell highlighting, reverting the original cell color











up vote
-1
down vote

favorite












I want to make a macro that Highlights the selected rows but does not change the original cell color once the highlighting is over (once the cell is not selected anymore).
*(Original cell color : The color of the originally specified cell.)



and here is code that I used



Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Static xRow
If xRow <> "" Then
With Rows(xRow).Interior
.ColorIndex=Xlnone
End with
End If

pRow = Selection.Row
xRow = pRow

With Rows(pRow).Interior
.ColorIndex=6
.Pattern=XlSolid
End With
End Sub


but when I use code like this, Highlight is active, but the original cell color is changed.



How Can I fix it?










share|improve this question
























  • This .ColorIndex=X|none is no valid syntax. It should be xlNone and xlSolid. I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration to avoid typos like that.
    – Pᴇʜ
    Nov 20 at 9:17










  • ah, it is typo sorry.. it is not |, I want to wrote l (L) .. sorry
    – Chae-Hee Kim
    Nov 20 at 9:20








  • 1




    I think you are looking for something like that: atlaspm.com/toms-tutorials-for-excel/… There are several tutorials on Google how to highlight cells, rows and columns. Use one of them so you don't need to invent it on your own.
    – Pᴇʜ
    Nov 20 at 9:31








  • 5




    "I'm not a American so I'm not good at English" - That's ok, Americans aren't good at English either. :-P
    – Gravitate
    Nov 20 at 10:49










  • If you want to preserve the original colour you have to store it somewhere, not erase it.
    – Luuklag
    Nov 20 at 10:55















up vote
-1
down vote

favorite












I want to make a macro that Highlights the selected rows but does not change the original cell color once the highlighting is over (once the cell is not selected anymore).
*(Original cell color : The color of the originally specified cell.)



and here is code that I used



Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Static xRow
If xRow <> "" Then
With Rows(xRow).Interior
.ColorIndex=Xlnone
End with
End If

pRow = Selection.Row
xRow = pRow

With Rows(pRow).Interior
.ColorIndex=6
.Pattern=XlSolid
End With
End Sub


but when I use code like this, Highlight is active, but the original cell color is changed.



How Can I fix it?










share|improve this question
























  • This .ColorIndex=X|none is no valid syntax. It should be xlNone and xlSolid. I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration to avoid typos like that.
    – Pᴇʜ
    Nov 20 at 9:17










  • ah, it is typo sorry.. it is not |, I want to wrote l (L) .. sorry
    – Chae-Hee Kim
    Nov 20 at 9:20








  • 1




    I think you are looking for something like that: atlaspm.com/toms-tutorials-for-excel/… There are several tutorials on Google how to highlight cells, rows and columns. Use one of them so you don't need to invent it on your own.
    – Pᴇʜ
    Nov 20 at 9:31








  • 5




    "I'm not a American so I'm not good at English" - That's ok, Americans aren't good at English either. :-P
    – Gravitate
    Nov 20 at 10:49










  • If you want to preserve the original colour you have to store it somewhere, not erase it.
    – Luuklag
    Nov 20 at 10:55













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I want to make a macro that Highlights the selected rows but does not change the original cell color once the highlighting is over (once the cell is not selected anymore).
*(Original cell color : The color of the originally specified cell.)



and here is code that I used



Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Static xRow
If xRow <> "" Then
With Rows(xRow).Interior
.ColorIndex=Xlnone
End with
End If

pRow = Selection.Row
xRow = pRow

With Rows(pRow).Interior
.ColorIndex=6
.Pattern=XlSolid
End With
End Sub


but when I use code like this, Highlight is active, but the original cell color is changed.



How Can I fix it?










share|improve this question















I want to make a macro that Highlights the selected rows but does not change the original cell color once the highlighting is over (once the cell is not selected anymore).
*(Original cell color : The color of the originally specified cell.)



and here is code that I used



Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Static xRow
If xRow <> "" Then
With Rows(xRow).Interior
.ColorIndex=Xlnone
End with
End If

pRow = Selection.Row
xRow = pRow

With Rows(pRow).Interior
.ColorIndex=6
.Pattern=XlSolid
End With
End Sub


but when I use code like this, Highlight is active, but the original cell color is changed.



How Can I fix it?







excel vba excel-vba highlight






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 13:56









Pᴇʜ

20.1k42650




20.1k42650










asked Nov 20 at 9:09









Chae-Hee Kim

11




11












  • This .ColorIndex=X|none is no valid syntax. It should be xlNone and xlSolid. I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration to avoid typos like that.
    – Pᴇʜ
    Nov 20 at 9:17










  • ah, it is typo sorry.. it is not |, I want to wrote l (L) .. sorry
    – Chae-Hee Kim
    Nov 20 at 9:20








  • 1




    I think you are looking for something like that: atlaspm.com/toms-tutorials-for-excel/… There are several tutorials on Google how to highlight cells, rows and columns. Use one of them so you don't need to invent it on your own.
    – Pᴇʜ
    Nov 20 at 9:31








  • 5




    "I'm not a American so I'm not good at English" - That's ok, Americans aren't good at English either. :-P
    – Gravitate
    Nov 20 at 10:49










  • If you want to preserve the original colour you have to store it somewhere, not erase it.
    – Luuklag
    Nov 20 at 10:55


















  • This .ColorIndex=X|none is no valid syntax. It should be xlNone and xlSolid. I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration to avoid typos like that.
    – Pᴇʜ
    Nov 20 at 9:17










  • ah, it is typo sorry.. it is not |, I want to wrote l (L) .. sorry
    – Chae-Hee Kim
    Nov 20 at 9:20








  • 1




    I think you are looking for something like that: atlaspm.com/toms-tutorials-for-excel/… There are several tutorials on Google how to highlight cells, rows and columns. Use one of them so you don't need to invent it on your own.
    – Pᴇʜ
    Nov 20 at 9:31








  • 5




    "I'm not a American so I'm not good at English" - That's ok, Americans aren't good at English either. :-P
    – Gravitate
    Nov 20 at 10:49










  • If you want to preserve the original colour you have to store it somewhere, not erase it.
    – Luuklag
    Nov 20 at 10:55
















This .ColorIndex=X|none is no valid syntax. It should be xlNone and xlSolid. I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration to avoid typos like that.
– Pᴇʜ
Nov 20 at 9:17




This .ColorIndex=X|none is no valid syntax. It should be xlNone and xlSolid. I recommend to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration to avoid typos like that.
– Pᴇʜ
Nov 20 at 9:17












ah, it is typo sorry.. it is not |, I want to wrote l (L) .. sorry
– Chae-Hee Kim
Nov 20 at 9:20






ah, it is typo sorry.. it is not |, I want to wrote l (L) .. sorry
– Chae-Hee Kim
Nov 20 at 9:20






1




1




I think you are looking for something like that: atlaspm.com/toms-tutorials-for-excel/… There are several tutorials on Google how to highlight cells, rows and columns. Use one of them so you don't need to invent it on your own.
– Pᴇʜ
Nov 20 at 9:31






I think you are looking for something like that: atlaspm.com/toms-tutorials-for-excel/… There are several tutorials on Google how to highlight cells, rows and columns. Use one of them so you don't need to invent it on your own.
– Pᴇʜ
Nov 20 at 9:31






5




5




"I'm not a American so I'm not good at English" - That's ok, Americans aren't good at English either. :-P
– Gravitate
Nov 20 at 10:49




"I'm not a American so I'm not good at English" - That's ok, Americans aren't good at English either. :-P
– Gravitate
Nov 20 at 10:49












If you want to preserve the original colour you have to store it somewhere, not erase it.
– Luuklag
Nov 20 at 10:55




If you want to preserve the original colour you have to store it somewhere, not erase it.
– Luuklag
Nov 20 at 10:55












1 Answer
1






active

oldest

votes

















up vote
1
down vote













Something that could work (did not test it) is to use conditional formatting to change the color and therefore let it always automatically restore the original color. Not sure about how good the performance of this workaround will be.





  1. In a module add the following function:



    Public Function IsSelected() As Boolean
    IsSelected = Not Intersect(Application.Caller, Range("SelectedRange")) Is Nothing
    End Function



  2. In your desired worksheet add the following event:



    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Me.Names.Add "SelectedRange", Target
    End Sub



  3. In the desired range add a conditional formatting with the formula:



    =IsSelected()







share|improve this answer























  • really appreciate for your answer, I'll do it right now :D
    – Chae-Hee Kim
    Nov 21 at 6:18










  • @Chae-HeeKim If it solved your question please mark it as solution: Accepting Answers: How does it work?
    – Pᴇʜ
    Nov 21 at 6:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389586%2ftemporary-cell-highlighting-reverting-the-original-cell-color%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Something that could work (did not test it) is to use conditional formatting to change the color and therefore let it always automatically restore the original color. Not sure about how good the performance of this workaround will be.





  1. In a module add the following function:



    Public Function IsSelected() As Boolean
    IsSelected = Not Intersect(Application.Caller, Range("SelectedRange")) Is Nothing
    End Function



  2. In your desired worksheet add the following event:



    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Me.Names.Add "SelectedRange", Target
    End Sub



  3. In the desired range add a conditional formatting with the formula:



    =IsSelected()







share|improve this answer























  • really appreciate for your answer, I'll do it right now :D
    – Chae-Hee Kim
    Nov 21 at 6:18










  • @Chae-HeeKim If it solved your question please mark it as solution: Accepting Answers: How does it work?
    – Pᴇʜ
    Nov 21 at 6:57















up vote
1
down vote













Something that could work (did not test it) is to use conditional formatting to change the color and therefore let it always automatically restore the original color. Not sure about how good the performance of this workaround will be.





  1. In a module add the following function:



    Public Function IsSelected() As Boolean
    IsSelected = Not Intersect(Application.Caller, Range("SelectedRange")) Is Nothing
    End Function



  2. In your desired worksheet add the following event:



    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Me.Names.Add "SelectedRange", Target
    End Sub



  3. In the desired range add a conditional formatting with the formula:



    =IsSelected()







share|improve this answer























  • really appreciate for your answer, I'll do it right now :D
    – Chae-Hee Kim
    Nov 21 at 6:18










  • @Chae-HeeKim If it solved your question please mark it as solution: Accepting Answers: How does it work?
    – Pᴇʜ
    Nov 21 at 6:57













up vote
1
down vote










up vote
1
down vote









Something that could work (did not test it) is to use conditional formatting to change the color and therefore let it always automatically restore the original color. Not sure about how good the performance of this workaround will be.





  1. In a module add the following function:



    Public Function IsSelected() As Boolean
    IsSelected = Not Intersect(Application.Caller, Range("SelectedRange")) Is Nothing
    End Function



  2. In your desired worksheet add the following event:



    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Me.Names.Add "SelectedRange", Target
    End Sub



  3. In the desired range add a conditional formatting with the formula:



    =IsSelected()







share|improve this answer














Something that could work (did not test it) is to use conditional formatting to change the color and therefore let it always automatically restore the original color. Not sure about how good the performance of this workaround will be.





  1. In a module add the following function:



    Public Function IsSelected() As Boolean
    IsSelected = Not Intersect(Application.Caller, Range("SelectedRange")) Is Nothing
    End Function



  2. In your desired worksheet add the following event:



    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Me.Names.Add "SelectedRange", Target
    End Sub



  3. In the desired range add a conditional formatting with the formula:



    =IsSelected()








share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 at 14:11

























answered Nov 20 at 13:34









Pᴇʜ

20.1k42650




20.1k42650












  • really appreciate for your answer, I'll do it right now :D
    – Chae-Hee Kim
    Nov 21 at 6:18










  • @Chae-HeeKim If it solved your question please mark it as solution: Accepting Answers: How does it work?
    – Pᴇʜ
    Nov 21 at 6:57


















  • really appreciate for your answer, I'll do it right now :D
    – Chae-Hee Kim
    Nov 21 at 6:18










  • @Chae-HeeKim If it solved your question please mark it as solution: Accepting Answers: How does it work?
    – Pᴇʜ
    Nov 21 at 6:57
















really appreciate for your answer, I'll do it right now :D
– Chae-Hee Kim
Nov 21 at 6:18




really appreciate for your answer, I'll do it right now :D
– Chae-Hee Kim
Nov 21 at 6:18












@Chae-HeeKim If it solved your question please mark it as solution: Accepting Answers: How does it work?
– Pᴇʜ
Nov 21 at 6:57




@Chae-HeeKim If it solved your question please mark it as solution: Accepting Answers: How does it work?
– Pᴇʜ
Nov 21 at 6:57


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389586%2ftemporary-cell-highlighting-reverting-the-original-cell-color%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen