Consulting

Results 1 to 3 of 3

Thread: Test if active cell is within 2 separate unrelated ranges

  1. #1
    VBAX Regular
    Joined
    Jul 2015
    Posts
    10
    Location

    Test if active cell is within 2 separate unrelated ranges

    Hi,
    I am trying to change the value of cell E21 if the user selects a cell within range("B2:AC16"). If the user selects a cell within range("AE2:AF16") then I want to change the value of cell G21.

    I have the following code and was trying to adopt a solution using two With statements but encountered errors. Can someone help suggest a better approach?

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim ws As Worksheet
    Dim rack, issue, rackRng, issueRng As Range
    
    Set ws = ThisWorkbook.Sheets("Sheet3")
    Set rack = ws.Range("E21")
    Set issue = ws.Range("g21")
    Set rackRng = Intersect(ActiveCell, Range("B2:AC16"))
    Set issueRng = Intersect(ActiveCell, Range("AE2:AF16"))
    
    With issueRng
        For Each c In rackRng
                If Not c Is Nothing Then
                   rack.Value = c.Value
    
    
                End If
            Exit For
     Next c
    End With
    
    'With rackRng
    'For Each f In issueRng
     '           If Not f Is Nothing Then
      '             issue.Value = f.Value
    
    
       '         End If
       '     Exit For
     'Next f
     
    'End With
    
    End Sub

    Thanks

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    in Worksheet_SelectionChange event, selected cell (Target) is the active cell.

    do you want to change values of E21 and G21 to the selected cells' values?
    if so try:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        If Not Intersect(Target, Range("B2:AC16")) Is Nothing Then Range("E21").Value = Target.Value
        If Not Intersect(Target, Range("AE2:AF16")) Is Nothing Then Range("G21").Value = Target.Value
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I would add to mancubus' code a new first line
    If Target.count > 1 then exit sub
    However it is my preference to NOT use Event Subs for any actual work. I only use them to decide which other process to run.

    Starting with mancubus' offering, I get:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    If Target.Count > 1 then exit sub 
    If Not Intersect(Target, Range("B2:AC16")) Is Nothing Then E21 Target
    If Not Intersect(Target, Range("AE2:AF16")) Is Nothing Then G21 Target
    
    'If not Intersect(Target, SomeOtherRange)) Is Nothing Then SomeOtherRange Target
    End Sub
    
    
    Private Sub E21(Target As Range)
    Range("E21").Value = Target.Value
    End sub
    
    Private Sub G21(Target As Range
    Range("G21").Value = Target.Value
    End sub
    
    Private Sub SomeOtherRange(Target)
    Blah, Blah
    End sub
    This keeps the actual Event sub clean and simple. It also keeps the code for one process from interfering with the code for another process. A little thought will reveal other advantages of this method
    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

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
  •