Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VBA Excel Target & Intersect

Posted on 2009-12-29
2
Medium Priority
?
1,240 Views
Last Modified: 2012-05-08
Hi All,

I have a little proc that runs on a change in the worksheet in front of it, but it's behaving badly.
The proc allows the user to place a checkmark in Column A when clicking a cell in that column or moving the cursor there via the keyboard.  There are a couple of problems with it:

1) If the user selects the entire row, the contents are deleted.
2) It does not play well with Filters.

If you see a glaring issue or have a better way to provide this ability, let me know.
Thank you!

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'
    Dim p As Integer
    Dim r As Range

    p = Cells(Rows.count, "B").End(xlUp).Row
    Set r = Range("A4:A" & p)
   
    If Not Intersect(Target, r) Is Nothing Then
        If Target.Text = "a" Then
            Target.Value = ""
        Else
            Target.Value = "a"
        End If
    End If
'
End Sub
0
Comment
Question by:bfreescott
2 Comments
 
LVL 19

Accepted Solution

by:
folderol earned 1000 total points
ID: 26143569
I think all you need is this small change.  I don't see anything immediately, that would act eradicately if a filter was in use.

Tom.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'
    Dim p As Integer
    Dim r As Range

    p = Cells(Rows.count, "B").End(xlUp).Row
    Set r = Range("A4:A" & p)
    
    If Not Intersect(Target, r) Is Nothing Then
        If Target.cells(1,1).value = "a" Then
            Target.cells(1,1).value = ""
        Else
            Target.cells(1,1).value = "a"
        End If
    End If
'
End Sub

Open in new window

0
 

Author Closing Comment

by:bfreescott
ID: 31671052
like a charm!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question