Consulting

Results 1 to 6 of 6

Thread: 2nd UCase/Target Challenge

  1. #1
    VBAX Tutor joelle's Avatar
    Joined
    Apr 2005
    Location
    Sunnyvale, CA
    Posts
    204
    Location

    2nd UCase/Target Challenge

    Pls how do I add a 2nd target [A2] to the code so that:
    If A2 is equal to "V", unhide range "newrange",
    else, hide "new range"

    My current code:
    If target A5 is <> blank, unhide superregion, else, hide it.

    [vba]Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Intersect(Target, [A5]) Is Nothing Then Exit Sub

    If UCase(Target) <> "" Then
    Range("superregion").EntireRow.Hidden = False

    Else
    Range("superregion").EntireRow.Hidden = True
    End If[/vba]
    Its a challenge because I heard that we can only have one
    private sub per ws, and I dont know how to expand the vba above
    to hold one more target and condition (A2 = "V")

    Your help is hugely appreciated.

    Nee

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Are you trying to do this?

    [VBA] If UCase(Target) <> "" Or Range("A2") = "V" Then [/VBA]

  3. #3
    VBAX Tutor joelle's Avatar
    Joined
    Apr 2005
    Location
    Sunnyvale, CA
    Posts
    204
    Location
    [Quoting Jake]
    Are you trying to do this?
    If UCase(Target) <> "" Or Range ("A2") = "V" Then [/quote]

    Hello Jake,
    No.

    Condition 1 : If A5 <> blank, unhide range "superregion"

    Condition 2 is: independent, not an "OR", nor part of condition 1.
    Its operation is like that of condition 1, but separate, because it looks at
    another cell [A2] and another entry, then unhide yet another range (shown below).

    If A2 = "V", unhide range "newrange"

    My challenge: how do I get these 2 operations to roof under one Private Sub below since I cannot have 2 private subs?

    [VBA]
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Intersect(Target, [A5]) Is Nothing Then Exit Sub
    If UCase(Target) <> "" Then
    Range("superregion").EntireRow.Hidden = False
    Else
    Range("superregion").EntireRow.Hidden = True
    End If
    [/VBA]

    'as you can see right now, I only have one condition that looks
    at entry to A5 only.

    Many thanks for any help you can provide.
    Nee
    Last edited by joelle; 04-06-2006 at 02:36 PM.

  4. #4
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Hi Nee,



    There is a select case method to use but you want to see if 2 private subs could work in one worksheet module. See if this helps paste this code in the worksheet module.


    [vba]
    Private Sub Worksheet_Change(ByVal Target As Range)
    Call ChangeEvent1(Target)
    Call ChangeEvent2(Target)
    End Sub

    Private Sub ChangeEvent1(ByVal Target As Range)
    If Intersect(Target, [A5]) Is Nothing Then Exit Sub

    If UCase(Target) <> "" Then
    Range("superregion").EntireRow.Hidden = False

    End If
    End Sub
    Private Sub ChangeEvent2(ByVal Target As Range)

    If Intersect(Target, [A2]) Is Nothing Then Exit Sub

    If UCase(Target) = "V" Then
    Range("superregion").EntireRow.Hidden = True
    End If
    End Sub
    [/vba]

  5. #5
    VBAX Tutor joelle's Avatar
    Joined
    Apr 2005
    Location
    Sunnyvale, CA
    Posts
    204
    Location
    Hello Shazam,

    Thank you for looking at my question and take the time to get me the code.
    I ran it, and no, it does not work (like you said 2 private subs might work in one ws).
    But I appreciate your kindness no matter.

    I normally did not cross post, but under the gun for half a day, I went on another forum and I was so lucky to get the code below from another goodhearted exceller (he's "dcardno"), and I'd like to share it here. Hopefully, it does help another person that happens to run in the same challenge.

    To me, this is a real vba challenge because I had never seen 2 UCase's (or targets) and 2 different conditions residing under one single Private Sub, which equals to 2 Private Subs in a WS -- which is, as I know, impossible, without some advanced twist.
    Pls enjoy.

    [vba]Credited to Dean (dcardno):
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Intersect(Target, union([A2],[A5])) is Nothing Then Exit Sub
    If Target.Address = [A2].Address Then
    If [A2] = "V" Then
    Range("newregion").EntireRow.Hidden = False
    Else
    Range("newregion").EntireRow.Hidden = True
    End If
    End If
    If [A5] <> "" Then
    Range("superregion").EntireRow.Hidden = False
    Else
    Range("superregion").EntireRow.Hidden = True
    End If
    End Sub
    [/vba]
    Best regards and thanks again for stopping at my post for
    the effort to help.

    Nee
    Last edited by joelle; 04-06-2006 at 05:05 PM.

  6. #6
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    Hi
    If Target.Address = [A2].Address Then
    If [A2] = "V" Then
    Range("newregion").EntireRow.Hidden = False
    Else
    Range("newregion").EntireRow.Hidden = True
    End If
    End If
    If I misunderstanding the problem, just ignore this post, but
    I don't think you need
    If Target.Address = [A2].Address Then
    try:
    select A1:A5 and hit delete key
    you can hide "superregion", but not the other one..

    CROSS POST
    http://www.mrexcel.com/board2/viewto...=985718#985718
    Last edited by jindon; 04-06-2006 at 09:59 PM.

Posting Permissions

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