Hello, I search quite a while with google and also through this forum, but I didn't find an answer to my problem.
I have a excel sheet with several status in coloumn C (open, closed, etc.).
And I have the following macro:
The code works perfectly and always changes the pattern color when I choose closed.Code:Sub Worksheet_Change(ByVal Target As Range) Dim Cell As Object For Each Cell In Range("C3:C250") If Cell = "closed" Then ' Make the background color of the cell the 3rd color on the ' current palette. Cell.EntireRow.Interior.Pattern = xlPatternLightUp Else Cell.EntireRow.Interior.Pattern = xlPatternNone End If Next Cell End Sub
BUT: When I try to copy and past a cell to several other cells, everytime I perform a CTRL + V the selected cell is unselected and I have to select it again.
Is there a trick or something that the selection is kept after the "Worksheet_Change" ?
Thanks for any answer!
Last edited by schwallo; 07-08-2009 at 09:24 AM.
The easiest workaround would be to select all your target cells first and then paste to all of them at once.
If that's not an option, you would have to assign the copied cell contents to a DataObject at the start of the Change event, do your formatting, then put the contents of the DataObject back into the clipboard. Let me know if you want some code for that.
So long, and thanks for all the fish.
Thanks for the answer, but that sound quite complicated.
Is there perhaps another possibility to change the row color (ad hoc on change of the cell) without using Worksheet_Change?
Yes, you could use conditional formatting to do it (I assumed you had a reason for using code instead?)
So long, and thanks for all the fish.
Yes I had, because I'm using the 3 given lines for conditional formating otherwi
se...
Then you will need to adapt the Change event. At the very least I would think you would want to check what has been changed - currently your code reformats everything for any change on the worksheet
So long, and thanks for all the fish.
Is there a possibility to only check for column C? If so could you please provide the code? Thanks in advance
Sure. This version will check if the changed cell(s) is/are in the range C3:C250:
Code:Sub Worksheet_Change(ByVal Target As Range) Dim rngCell As Range ' check if anything in C3:C250 is changed If Not Intersect(Target, Me.Range("C3:C250")) Is Nothing Then For Each rngCell In Intersect(Target, Me.Range("C3:C250")) ' check if the cell value is "closed" (not case-sensitive) If StrComp(rngCell.Value, "closed", vbTextCompare) = 0 Then ' Make the background color of the cell the 3rd color on the ' current palette. rngCell.EntireRow.Interior.Pattern = xlPatternLightUp Else rngCell.EntireRow.Interior.Pattern = xlPatternNone End If Next rngCell End If End Sub
So long, and thanks for all the fish.
Thank you very much!!! This is perfect!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks