#1
|
|||
|
|||
I don't understand event handling for the DoubleClick event
I'm slowly getting more comfortable with VBA, but I frequently don't understand the logic in sample code that I find on the net. My present problem is understanding the event handling for a double click event. The upper code is how I edited the sample code (which is commented out). Both seem to function the same. I understand the logic in my edited version, but not the logic in the sample: If the intercept is Nothing, doesn't that mean that target cell is not within the designated range? So why the "Not"? Why did the sample's author use the Not logic (I found a number of postings with this logic); does it have an advantage over my edited logic?I'd sincerely appreciate any comments that you can offer. Very respectfully, Larry Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) ' If the cell is not within the range B1:B10, then cancel ' If the cell is within the range, then act If Application.Intersect(Target, Range("B1:B10")) Is Nothing Then MsgBox ("is NOT in B1:B10") ' Action for out-of-range target Cancel = True Else: MsgBox ("is in B1:B10") ' Action if target is within the range End If ' Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) ' If Not Application.Intersect(Target, Range("C40")) Is Nothing Then ' Cancel = True ' SelectPayFreq.Show ' End If ' End Sub End Sub |
#2
|
|||
|
|||
This means that the Range B1:B10 is blank / empty / does not have data :
Code:
If Application.Intersect(Target, Range("B1:B10")) Is Nothing Then This means that the Range C40 has data / is not empty / is not blank : Code:
If Not Application.Intersect(Target, Range("C40")) Is Nothing Then When you see two negatives "Not" / "is Nothing" .. think in terms of your High School Algebra : Two Negatives make a positive. |
#3
|
|||
|
|||
Thank you, Logit. That makes sense, but the approach seems to go around the barn unnecessarily. Yet that double negative logic was in all of the on-line samples that I found. Is there an advantage to it?
Larry |
#4
|
|||
|
|||
.
"Is there an advantage to it ? " I can't answer that question ... I'm not an expert programmer. It's a hobby that I take seriously. (Is that bad ?) One advantage I understand is : If you have code that basically says, "Range("A1"A12").Value = "XYZ", then the logic to that is Range("A1"A12").Value ALWAYS must equal "XYZ" for the remainder of the line of code to work correctly. Now, if you use, "If Not Range("A1:A12").Value is Nothing Then" ... The Values in the stated range can be anything. The code has not restricted the value to always be XYZ in order for it to work. I'm retired now but I still recall being in High School Algebra and wondering how that gobble-d-goop was ever going to be useful. It wasn't until I began hobby programming that I finally understood a use for variables, less than, greater than, equals, double negatives, etc. etc. Most of the time I'm still as confused as I was 50 years ago ! |
#5
|
|||
|
|||
Logit,
It sounds like we're in very similar circumstances, but you're well up the learning curve from me. I'd not thought of the cells' contents, and your logic would need to be considered for a test on the contents. But my understanding of the Intersect function is that it's only testing to see if the ranges have any cells in common and that it does not look at their contents. If that's correct, then the advantage doesn't seem to apply. Very respectfully, Larry |
#6
|
|||
|
|||
You're right Larry, cell content has no relevance with regards to range intersection (nor with the double click event).
I'm not sure there is any advantage to using either other than personal consistency. Is Nothing and Not Is Nothing give opposite answers for the same condition. If one says True the other always says False and both will be right. For this old retired guy it's easiest to think in one direction only. |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Event planning | cajos | Project | 3 | 03-26-2016 05:19 PM |
calendar event not showing | licinio | Outlook | 0 | 05-18-2015 05:27 AM |
Document_ContentControlBeforeDelete Event | Tejas.T | Word VBA | 5 | 02-27-2015 08:01 PM |
Hyperlinks in Calendar Event? | komobu | Outlook | 0 | 07-16-2014 08:09 AM |
Create calendar event | groegee | Outlook | 1 | 12-05-2011 09:56 PM |