+ Reply to Thread
Results 1 to 5 of 5

Selecting range of cells whilst 'event selection procedure' runs in the background

Hybrid View

kosherboy Selecting range of cells... 01-15-2016, 11:49 AM
kosherboy Re: Selecting range of cells... 01-15-2016, 12:27 PM
LJMetzger Re: Selecting range of cells... 01-15-2016, 12:35 PM
LJMetzger Re: Selecting range of cells... 01-15-2016, 12:42 PM
kosherboy Re: Selecting range of cells... 01-15-2016, 12:54 PM
  1. #1
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Selecting range of cells whilst 'event selection procedure' runs in the background

    Hi.
    I'm having some trouble with target cells in event procedure, when i select any range of more than two cells (for cell formatting purposes) the debugger goes on but not sure why?
    Here is my code:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Target.Column = 28 And Target.Row > 31 And Target.Row < 531 And Target.Value = "UnSelected" Then
            Target.Value = "Selected"
            With Range(Target.Offset(0, -21).Address & ":" & Target.Offset(0, 4).Address)
                .Interior.Color = RGB(204, 255, 204)
            End With
        ElseIf Target.Column = 28 And Target.Row > 31 And Target.Value = "Selected" Then
            Target.Value = "UnSelected"
            With Range(Target.Offset(0, -21).Address & ":" & Target.Offset(0, 4).Address)
                .Interior.ColorIndex = xlNone
            End With
        End If
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: Selecting range of cells whilst 'event selection procedure' runs in the background

    I added this and it works now
    If Target.Row < 31 Then Exit Sub
    If Target.Row > 532 Then Exit Sub
    If Target.Column < 28 Then Exit Sub
    If Target.Column > 28 Then Exit Sub

  3. #3
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Selecting range of cells whilst 'event selection procedure' runs in the background

    Hi,

    Your problem was caused by Target.Row and Target.Column and Target.Value being illegal when more than one cell was selected.

    If you limit selection to one cell only try the following:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        If Target.CountLarge > 1 Then
          MsgBox "Selection of more than one cell is not allowed."
          Exit Sub
        End If
       
        If Target.Column = 28 And Target.Row > 31 And Target.Row < 531 And Target.Value = "UnSelected" Then
            Target.Value = "Selected"
            With Range(Target.Offset(0, -21).Address & ":" & Target.Offset(0, 4).Address)
                .Interior.Color = RGB(204, 255, 204)
            End With
        ElseIf Target.Column = 28 And Target.Row > 31 And Target.Value = "Selected" Then
            Target.Value = "UnSelected"
            With Range(Target.Offset(0, -21).Address & ":" & Target.Offset(0, 4).Address)
                .Interior.ColorIndex = xlNone
            End With
        End If
    End Sub
    Please NOTE: When using Excel 2003 replace CountLarge with Count, because CountLarge is NOT available in Excel 2003.



    If selecting more than one cell is allowed try:
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        Dim rCell As Range
        
        For Each rCell In Target
        
            If rCell.Column = 28 And rCell.Row > 31 And rCell.Row < 531 And rCell.Value = "UnSelected" Then
                rCell.Value = "Selected"
                With Range(rCell.Offset(0, -21).Address & ":" & rCell.Offset(0, 4).Address)
                    .Interior.Color = RGB(204, 255, 204)
                End With
            ElseIf rCell.Column = 28 And rCell.Row > 31 And rCell.Value = "Selected" Then
                rCell.Value = "UnSelected"
                With Range(rCell.Offset(0, -21).Address & ":" & rCell.Offset(0, 4).Address)
                    .Interior.ColorIndex = xlNone
                End With
            End If
        
        Next rCell
        
    End Sub
    Lewis

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Selecting range of cells whilst 'event selection procedure' runs in the background

    Your solution in post #2 still causes a runtime error for me, when selecting more than one cell.

  5. #5
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Waterbury, CT
    MS-Off Ver
    Office 365
    Posts
    1,175

    Re: Selecting range of cells whilst 'event selection procedure' runs in the background

    Dear Lewis,
    I actually did want to have the option of selecting more than one cell but i thought that would be impossible so i inserted in my code the exit sub if i selected anything outside the range mentioned in the code. It did not take care of the issue if i selected more than one cell within the range mentioned in the code, but once again, i thought there was no way out.
    The second code you posted is originally what i was looking for and it works flawlessly! Thank you.

    P.S. sometimes i think it would be impossible to get a solution to certain vba questions i have but then i am proven wrong.
    You inspire me

    Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Executing a bouncing sheep whilst the code/macro runs
    By mike_vr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-03-2013, 08:21 AM
  2. [SOLVED] Pause macro whilst selecting the file i want to copy from
    By stevekirk in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-03-2012, 09:07 PM
  3. VBA for selecting a range to fill a selection
    By mbesspiata in forum Excel General
    Replies: 1
    Last Post: 07-30-2012, 01:15 PM
  4. Selecting range using .range. But can you do same selection using .columns
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-01-2011, 01:31 PM
  5. Pause macro procedure whilst Query runs
    By johncassell in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-19-2010, 01:30 PM
  6. Runs in macro but not in procedure...why?
    By Billy B in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-20-2006, 12:30 AM
  7. Macro that runs in the background
    By tomwashere2 in forum Excel General
    Replies: 0
    Last Post: 10-22-2005, 06:05 PM
  8. Replies: 4
    Last Post: 02-10-2005, 03:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1