Results 1 to 6 of 6

Thread: SpecialCells causes SelectionChange

  1. #1
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,773

    SpecialCells causes SelectionChange

    I responded to a question in this thread, http://www.vbaexpress.com/forum/showthread.php?t=17797 with a SelectionChange routine that used SpecialCells.

    I discovered that SpecialCells triggers a SelectionChange event. And am very confused.
    With this test routine,[VBA]Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Static i As Long
    If Not (Application.Intersect(Target, Range("1:6")) Is Nothing) Then
    If Comments.Count = 0 Then
    Range("b7").Select
    Else
    i = i + 1
    If Application.Intersect(Target, Cells.SpecialCells(xlCellTypeFormulas)) Is Nothing Then
    MsgBox "x": i = 0: Rem reset C
    Range("b7").Select
    End If
    MsgBox Target.Address & ":" & i
    End If
    Else
    'i = 0: Rem reset B
    End If
    'i = 0: Rem reset A
    End Sub[/VBA]If I click on a cell in 1:6, the cursor dances a bit and a message box appears showing the address of my formula followed by ":28".
    Then 27 identical message boxes appear.
    Then "x".
    Then a message box with the cell address I click on and ":0" if my test cell is a non-formula, ":28" if it has a formula.

    Putting the reset in different places has some effect, but the intial msgbox is always 28.

    Adding formulas to the sheet doesn't change the 28.

    Any insights?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    Just goes into a loop for me. What am I doing wrong?
    ____________________________________________
    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

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,773
    Are there formulas in your sheet? (I would think that would give a Not Found error)
    A commented cell is needed.

    This is annoying, its not behaving as it used to, I can't reproduce my own error.

    If I don't disable events in the code in the other thread, I get cascade problems.

  4. #4
    I know this is an old thread, but I have also just discovered that SpecialCells triggers a SelectionChange event. I was using SpecialCells on a Range that was not on the active sheet. That non-activesheet still fired it's SelectionChange event even with ScreenUpdating off.

    Did you find out if it was something you were doing to cause it, or is it just an Excel bug? Is there an alternative to get the xlCellTypeFormulas of a range?

    Cheers

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    ScreenUpdate will not make a difference, you'll need to set EnableEvents to False.
    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'

  6. #6
    Yep, thanks.

    I have surrounded my SpecialCells code with EnableEvents off, on. I only mentioned the ScreenUpdate to emphisize I was not changing sheet or range.

Posting Permissions

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