Consulting

Results 1 to 5 of 5

Thread: Worksheet_SelectionChange(ByVal Target As Range)

  1. #1
    VBAX Contributor
    Joined
    Nov 2015
    Location
    Minnesota
    Posts
    101
    Location

    Worksheet_SelectionChange(ByVal Target As Range)

    I need help with defining my ranges. When I use the code shown below, it will not follow my if statement based on the selected cell.

    'Status Bar Display
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error Resume Next
        
    If Intersect(Target, Range(Cells(4, 2), Cells(Rows.Count, 3))) Then
        sr = Selection.Row
        sc = Selection.Column
        inquiry = Cells(sr, sc)
        
        Select Case Sheets("Hide2").Cells(sr, sc)
        Case False
            Application.StatusBar = inquiry & " : does not exist."
        Case ""
            Application.StatusBar = ""
        Case Else
            MyDesc1 = Sheets("Hide2").Cells(sr, sc)
            Application.StatusBar = inquiry & " : " & MyDesc1
        End Select
    
    ElseIf Intersect(Target, Range(Cells(3, 4), Cells(Rows.Count, Columns.Count))) Then
        Application.StatusBar = "Testing"
    End If
    
    End Sub

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I usually do something like this since Intersect returns a Range or Nothing

    If Not Intersect(Target, Range(Cells(4, 2), Cells(Rows.Count, 3))) Is Nothing Then
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        'Turn offSstatusBar on every selection change
        Application.StatusBar = ""
        
        'Almost always ignore multiple cell selections
        If Target.Count > 1 Then Exit Sub
        
        'Call Inquiry?
        If Target.Row >= 4 And (Target.Column = 2 Or Target.Column = 3) Then Inquire Target
         
        'Call TestStatusBar?
        If Target.Row >= 3 And Target.Column >= 4 Then TestStatusBar
         
    End Sub
    Private Sub Inquire(Target As Range)
    
        With Sheets("Hide2")
            Select Case .Range(Target.Address)
            Case False
                Application.StatusBar = Target.Value & ": does not exist."
            Case Else
              With .Range(Target.Address)
                MyDesc1 = .Value
                Application.StatusBar = Target.Value & ": " & .Value
              End With
            End Select
        End With
    
    End Sub
    Private Sub TestStatusBar()
    Application.StatusBar = "Testing"
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    And get rid of that

     On Error Resume Next

    at the beginning which just hides any errors

    If I have to use it (and there are good reasons to), then I have it in effect for a brief a time as possible, before I have On Error Goto 0, typically only 1-3 lines

    You could have found your error right away

    Capture.JPG
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Contributor
    Joined
    Nov 2015
    Location
    Minnesota
    Posts
    101
    Location
    Thanks for the help! I went with SamT's code. Seems to be doing the trick, and I can add more cases if needed.

Tags for this Thread

Posting Permissions

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