+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Registered User
    Join Date
    07-08-2009
    Location
    Munich
    MS-Off Ver
    Excel 2003
    Posts
    5

    Question Using Worksheet_Change and User Selection

    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:

    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
    The code works perfectly and always changes the pattern color when I choose closed.
    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.

  2. #2
    Forum Moderator romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Alibi
    MS-Off Ver
    All
    Posts
    8,262

    Re: Using Worksheet_Change and User Selection

    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.

  3. #3
    Registered User
    Join Date
    07-08-2009
    Location
    Munich
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Using Worksheet_Change and User Selection

    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?

  4. #4
    Forum Moderator romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Alibi
    MS-Off Ver
    All
    Posts
    8,262

    Re: Using Worksheet_Change and User Selection

    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.

  5. #5
    Registered User
    Join Date
    07-08-2009
    Location
    Munich
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Using Worksheet_Change and User Selection

    Yes I had, because I'm using the 3 given lines for conditional formating otherwi
    se...

  6. #6
    Forum Moderator romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Alibi
    MS-Off Ver
    All
    Posts
    8,262

    Re: Using Worksheet_Change and User Selection

    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.

  7. #7
    Registered User
    Join Date
    07-08-2009
    Location
    Munich
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Using Worksheet_Change and User Selection

    Is there a possibility to only check for column C? If so could you please provide the code? Thanks in advance

  8. #8
    Forum Moderator romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Alibi
    MS-Off Ver
    All
    Posts
    8,262

    Re: Using Worksheet_Change and User Selection

    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.

  9. #9
    Registered User
    Join Date
    07-08-2009
    Location
    Munich
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Using Worksheet_Change and User Selection

    Thank you very much!!! This is perfect!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0