Consulting

Results 1 to 15 of 15

Thread: VBA Target.value

  1. #1
    VBAX Regular
    Joined
    Dec 2008
    Posts
    85
    Location

    VBA Target.value

    Hi:

    Please need help with the "Target.value = 0" IF to clear the cell contents in the following code:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Value = 0 Then
       Target.Offset.Offset(0, 1).ClearContents
    End If
    If Target.Column = 1 Then
       If Target.Row > 10 Then
          If Target.Row < 15 Then
             Application.EnableEvents = False
             Target.Offset.Offset(0, 1) = Now()
             Application.EnableEvents = True
          End If
       End If
    End If
    End Sub
    Thanks for the help.

    Victor
    Last edited by Aussiebear; 04-11-2023 at 05:32 AM. Reason: Adjusted the code tags

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Could this be the problem. You have:
    Target.Offset.Offset(0, 1).ClearContents
    Try:

    Target.Offset(0, 1).ClearContents
    Last edited by Aussiebear; 04-11-2023 at 05:33 AM. Reason: Adjusted the code tags
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Regular
    Joined
    Dec 2008
    Posts
    85
    Location
    Lucas:

    I did the correction suggested but I still can not run the IF section:

    If Target.Value = 0 Then
       Target.Offset(0, 1).ClearContents
         End If
    See corrected vba code below:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Value = 0 Then
       Target.Offset(0, 1).ClearContents
         End If
        If Target.Column = 1 Then
       If Target.Row > 10 Then
          If Target.Row < 15 Then
             Application.EnableEvents = False
             Target.Offset.Offset(0, 1) = Now()
             Application.EnableEvents = True
          End If
       End If
        End If
    End Sub
    Thanks for the suggestion.

    Victor
    Last edited by Aussiebear; 04-11-2023 at 05:35 AM. Reason: Adjusted the code tags

  4. #4
    VBAX Mentor tpoynton's Avatar
    Joined
    Feb 2005
    Location
    Clinton, MA
    Posts
    399
    Location
    the first if statement works for me, if I put a 0 in column A, anything in column B in the same row is cleared. Perhaps you need to exit the sub if the value is 0? try


    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Value = 0 Then
       Target.Offset(0, 1).ClearContents
       Exit Sub
        End If
        If Target.Column = 1 Then
       If Target.Row > 10 Then
          If Target.Row < 15 Then
             Application.EnableEvents = False
             Target.Offset(0, 1) = Now()
             Application.EnableEvents = True
          End If
       End If
        End If
    End Sub
    Last edited by Aussiebear; 04-11-2023 at 05:34 AM. Reason: Adjusted the code tags

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Try

    Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo ws_exit
    Application.EnableEvents = False
    If Target.Value = 0 Then
       Target.Offset(0, 1).ClearContents
        End If
    If Not Intersect(Target, Me.Range("A11:A14")) Is Nothing Then
       Target.Offset(0, 1) = Now()
        End If
    ws_exit:
        Application.EnableEvents = True
    End Sub
    Last edited by Aussiebear; 04-11-2023 at 05:36 AM. Reason: Adjusted the code tags
    ____________________________________________
    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

  6. #6
    VBAX Regular
    Joined
    Dec 2008
    Posts
    85
    Location
    Hi:

    I am enclosed file with the code. May be there you can catch the problem.

    Thanks for the help.

    Victor

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    My code supplied earlier works fine with that workbook. Why didn't you try it?
    ____________________________________________
    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

  8. #8
    VBAX Regular
    Joined
    Dec 2008
    Posts
    85
    Location
    XLD:

    I just not want a solution but to learn what is the problem with the code that apparently should work.

    I really appreciate your help.

    Thanks.

    Victor

  9. #9
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You need to set EnableEvents to False before the code makes any changes

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Exits
    Application.EnableEvents = False
    If Target.Value = 0 Then
       Target.Offset.Offset(0, 1).ClearContents
    End If
    If Target.Column = 1 Then
       If Target.Row > 10 Then
          If Target.Row < 15 Then
             Target.Offset(0, 1) = Now()
          End If
       End If
    End If
    Exits:
    Application.EnableEvents = True
    End Sub
    Last edited by Aussiebear; 04-11-2023 at 05:37 AM. Reason: Adjusted the code tags
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Victor
    XLD:

    I just not want a solution but to learn what is the problem with the code that apparently should work.

    I really appreciate your help.

    Thanks.

    Victor
    Then why didn't you say that rather than posting a workbook with the same code that you posted earlier.

    When you change a value to 0 it just generates an endless cascade of changes. because you change the value to the right, which triggers the event again, and so on.
    ____________________________________________
    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

  11. #11
    VBAX Regular
    Joined
    Dec 2008
    Posts
    85
    Location
    mdmackillop and xlt:

    I tried both xld code and now mdmackillop and when I run the code with Excel 2003 the code do not erase the offset(0,1) contents when the target.value = 0.

    I posted the file so you can run it and check it out to see if it runs.

    I am sorry if I do not explained myself clearly, it was not my intention, but still I really need your help as to why the suggested code do no run the clearcontents code.

    Thanks for the help.

    Victor

  12. #12
    VBAX Regular
    Joined
    Dec 2008
    Posts
    85
    Location
    Thanks to all:

    If I add Goto Exits after the instruction below solves my problem for the codes suggested by mdmackillop and xlt:

    Target.Offset.Offset(0, 1).ClearContents 
    Goto Exits

    Without this intruction, in both suggested codes it shows the date (now()) when clear the cells contents. Which was my problem inicially.

    Thanks to all for the help.

    Victor
    Last edited by Aussiebear; 04-11-2023 at 05:38 AM. Reason: Adjusted the code tags

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Better to use

    Private Sub Worksheet_Change(ByVal Target As Range) 
    On Error Goto ws_exit 
    Application.EnableEvents = False 
    If Target.Value = 0 Then 
       Target.Offset(0, 1).ClearContents 
       ElseIf Not Intersect(Target, Me.Range("A11:A14")) Is Nothing Then 
       Target.Offset(0, 1) = Now() 
        End If 
    ws_exit: 
        Application.EnableEvents = True 
    End Sub

    than to use Goto
    Last edited by Aussiebear; 04-11-2023 at 05:39 AM. Reason: Adjusted the code tags
    ____________________________________________
    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

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Victor,
    Stepping through your original code would show the events occuring and allow you to see where the code was going wrong.
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  15. #15
    VBAX Regular
    Joined
    Dec 2008
    Posts
    85
    Location
    Another option is the code modified by tpoynton adding:

    Target.Offset(0, 1).ClearContents 
     Exit Sub
    Thanks all for your interest is providing different alternatives to solved the problem but the one provided by xld required less steps.

    Victor
    Last edited by Aussiebear; 04-11-2023 at 05:39 AM. Reason: Adjusted the code tags

Posting Permissions

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