Consulting

Page 2 of 2 FirstFirst 1 2
Results 21 to 32 of 32

Thread: Worksheet_change event executed on a wrong sheet?

  1. #21
    snb
    Guest
    I don't think it's possible to offer a 'solution/remedy' if the 'ailment' isn't clear.
    You'd better post the 'suffering' workbook.

  2. #22
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    Quote Originally Posted by stranno View Post
    1. I am not sure if I know what you mean with using "Target.Cells.Count" What is the purpose of delimiting de range? I mean the target can be any cell in Column L. Or do you mean that I should add something like
    "If Target.Cells.Count>0 then exit" sub?
    What that means is that the user could change many cells in a single action. If that happens and you don't trap it may not work as you want. You should test Target.Cells.Count for > 1 and if so, exit without doing anything, or else you could process every cell in Target within some kind of loop.

    Quote Originally Posted by stranno View Post
    2. Maybe a good idea. But what is exactly the difference between Target.Parent and "Target, Me.Collumns("L:L")" in this case?
    A lot. Target.Parent refers to the Parent object of Target, and as Target is a range, the Parent is the worksheet that that range is contained within. Target, Me.Coilumns("L:L") is referring to two objects, that same range object that triggered the event, and a specific range object in the worksheet, namely all of column L.

    Quote Originally Posted by stranno View Post
    3. Also a good idea, but again it's very hard to verify the effect because so far I could not reproduce this issue myself.

    But Summarized, what is the best solution?

    Use of: "Me.Parent, Columns("L:L")" or "Target, Me.Columns("L:L")" or is there yet another solution?
    The first one won't work, because you are testing whether a worksheet object intersects a range object, apples and pears.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #23
    Site Admin VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,496
    Location
    So, summerising ( if that is indeed the word) Firefighter is somewhere in the world but unknown to GTO, XLD is tossing Apples & Pears, and snb needs some sort of medicare because his workbook is ailing. Gees. I can't wait for the next episode.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #24
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    @Mark: Good point! Probably a better way to put what I was thinking. I don't know if that's actually the case and was just speculating. Would make sense though. And the "y" used to be "firefytr", which I changed to my real name after meeting quite a few people and was apparently confusing, so I figured transparency would be better lol. And I hope it wasn't too painful of thinking!

    @Bob: I try to always specify the worksheet. I mean, I've always tried to use the 'Me' object. If nothing else it makes it easier to read (IMHO). And I certainly hope to toggle events off when needed!

    @Aussiebear: LOL! Oh man, I love this board.

  5. #25
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,888
    Location
    Reading between the lines on On Line 2010 Help ...


    Invalid use of Me keyword
    This page is specific to the Visual Basic for Applications (VBA) Language Reference for Office 2010. The Me keyword can appear in class modules. This error has the following causes and solutions:

    • The Me keyword appeared in a standard module.
      The Me keyword can't appear in a standard module because a standard module doesn't represent an object. If you copied the code in question from a class module, you have to replace the Me keyword with the specific object or form name to preserve the original reference.
    • The Me keyword appeared on the left side of a Set assignment, for example:





    the text in red makes it seem that Range("A1") (no specific parent) on a worksheet's code module resolves to that worksheet, where as in a standard module it resolves to the ActiveSheet.


    I'm with Zack -- I always specify the full name, since I'm in favor of anything that helps prevent me from making errors

    Paul

  6. #26
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,888
    Location
    @stranno

    The ideas and suggestions from all the others are very good coding practices.

    Demonstration of some techniques I like to use that might be applicable based on what I could figure out from your code. This is NOT WAY a solution since I didn't grasp what you were trying to accomplish, but merely some thoughts.

    1. No ActiveSheet
    2. I assumed that only one cell would be changed at a time, and it's that cell .Offsets you wanted
    3. This was not tested for logic error, but it does compile

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim ant As String
         
        If Target.Cells.Count <> 1 Then Exit Sub
         
        If Application.Intersect(Target, Target.Parent.Columns(12)) Is Nothing Then Exit Sub
        
        Application.EnableEvents = False
        
        With Target
            
            ant = .Value
            
            Select Case ant
                Case "n"
                    .Offset(0, -4) = -1 * Abs(Val(.Offset(0, -4).Value))
                    .Offset(0, -6) = -1 * Abs(Val(.Offset(0, -6).Value))
                    .Offset(0, -6).NumberFormat = "0;[Red]0"
                     
                Case "j"
                    .Offset(0, -4) = Abs(Val(.Offset(0, -4).Value))
                    .Offset(0, -6) = Abs(Val(.Offset(0, -6).Value))
                    .Offset(0, -6).NumberFormat = "0;[Red]0"
                     
                Case ""
                    ant = vbNullString
                    
                    Do
                        ant = InputBox("j of n", "Kies j of n")
                    Loop Until (ant = "n" Or ant = "j")
                
                    If ant = "n" Then
                        .Offset(0, -4) = -1 * Abs(Val(.Offset(0, -4).Value))
                    Else
                        .Offset(0, -4) = Abs(Val(.Offset(0, -4).Value))
                    End If
            
            Case Else
                If Err.Number <> 0 Then
                    Call MsgBox(Err.Description, vbCritical + vbOKOnly, Me.Name & " -- " & "Worksheet_Change")
                    Err.Clear
                End If
                On Error GoTo 0
            
            End Select
        End With
         
        Application.EnableEvents = True
    End Sub
    Paul

  7. #27
    VBAX Tutor
    Joined
    Jun 2005
    Posts
    214
    Location
    Thanks Paul. Very helpful.

  8. #28
    snb
    Guest
    Or:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Cells.Count > 1 Then Exit Sub
         
        If Application.Intersect(Target, Target.Parent.Columns(12)) Is Nothing Then Exit Sub
        If InStr("jn", LCase(Target)) = 0 Then
            Target.Select
            Exit Sub
        End If
        
        Application.EnableEvents = False
    
        With Target
            .Offset(, -4) = Abs(.Offset(, -4).Value) * (1 + 2 * (Target = "n"))
            .Offset(, -6) = Abs(.Offset(, -6).Value) * (1 + 2 * (Target = "n"))
            .Offset(, -6).NumberFormat = "0;[Red]0"
        End With
    
        Application.EnableEvents = True
    End Sub

  9. #29
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by Zack Barresse View Post
    @Mark: ...I don't know if that's actually the case and was just speculating. Would make sense though...
    Hi Zack,

    I am probably misunderstanding your comment, in which case, please forgive. Just in case you were saying that you're not sure that the unqualified Range() in a worksheet's module, defaults to the module, it does. The help topic pretty much sucks at not mentioning this any place I found, but it is a "for sure and for certain" to the best of my knowledge. You might enjoy http://www.thecodecage.com/forumz/mi...e-not-why.html, particularly #24 where Rory "caught" me.

    Quote Originally Posted by Zack Barresse View Post
    ...And the "y" used to be "firefytr", which I changed to my real name after meeting quite a few people and was apparently confusing, so I figured transparency would be better lol.
    Thank you so much :-) I hope you won't mind if I occasionally use that in respect to your chosen field. I imagine that Fire there is similar to here in that a lot of learning is required in EMS related as well as fire science. An honorable endeavor and my appreciation.

    Quote Originally Posted by Zack Barresse View Post
    And I hope it wasn't too painful of thinking!
    It's okay, my pea-sized brain is used to hurting/straining easily. Luckily though, when thinking something through is over, it has plenty of room to stretch and relax in the copious amount of empty space in my noggin.

    Quote Originally Posted by Zack Barresse View Post
    @Aussiebear: LOL! Oh man, I love this board.
    As Oorang would way, "WOOT!". I certainly agree!

    Mark

  10. #30
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Ah, I see, I was saying the opposite, that since a standard module would have unqualified/unspecified objects, it was probably the class which [forces] the inheritance.

    These days all I'm doing is EMS actually. You can call me whatever you want, I'll answer to both.

  11. #31
    Site Admin VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,496
    Location
    Quote Originally Posted by Zack Barresse View Post
    You can call me whatever you want, I'll answer to both.
    What makes you think in 2 dimensions.......
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  12. #32
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location
    Private Sub Worksheet_Change(ByVal Target As Range)
         
        Dim ant As String
        Dim X As Integer
        X = 1 'Default value
         
        If Target.Count > 1 Then Exit sub
        If Application.Intersect(Target, Columns("L")) Is Nothing Then Exit Sub
        
        Application.EnableEvents = False
        On Error GoTo proceed
         
        ant = Target.Value 'Target.Value = "j" by Default
    again:
      If ant <> "j" And ant <> "n" Then
        ant = InputBox("j of n", "Kies j of n")
        GoTo again 'repeat until correct
      End If
      
      If ant = "n" Then X = -1
    
        Target.Offset(0, -4) = X * Abs(Val(Target.Offset(0, -4).Value))
        Target.Offset(0, -6) = X * Abs(Val(Target.Offset(0, -6).Value))
        Target.Offset(0, -6).NumberFormat = "0;[Red]0"
         
        Application.EnableEvents = True
        Exit Sub
         
    proceed:
        Call ErrorDescription("pfd", "Worksheet_Change") 'calls the function ErrorDescription
        Err.Clear
        Application.EnableEvents = True
    End Sub
    Last edited by SamT; 02-14-2014 at 04:33 PM.
    Please take the time to read the Forum FAQ

Posting Permissions

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