Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 309
Default Using "Select Case" inside Worksheet_Change() subroutine.....

I would like to monitor when any of the data in my group of
cells gets modified or deleted.

Here is how my code looks:

Private Sub Worksheet_Change (ByVal Target As Range)
Select Case Target
Case Range("A1")
MsgBox ("A1 was just modified")
Case Range "B1")
MsgBox ("B1 was just modified")
'..... etc. etc.. etc...
Case Range ("M20")
MsgBox ("M20 was just modified")
Case Else
' Do nothing
End Select
End Sub

Does anybody see anything wrong with this approach?? Is my code
correct?? How can I code this in a more efficient manner??

Thank you all!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Using "Select Case" inside Worksheet_Change() subroutine.....

Your code is really doing:

Private Sub Worksheet_Change (ByVal Target As Range)
Select Case Target.value
Case Range("A1").value
MsgBox "A1 was just modified"
Case Range("B1").value
MsgBox "B1 was just modified"
'..... etc. etc.. etc...
Case Range("M20").value
MsgBox "M20 was just modified"
Case Else
' Do nothing
End Select
End Sub

I bet you want:

Private Sub Worksheet_Change (ByVal Target As Range)
Select Case Target.Address
Case Range("A1").address
MsgBox "A1 was just modified"
Case Range("B1").address
MsgBox "B1 was just modified"
'..... etc. etc.. etc...
Case Range("M20").address
MsgBox "M20 was just modified"
Case Else
' Do nothing
End Select
End Sub

Or Private Sub Worksheet_Change (ByVal Target As Range)
Select Case Target.address
Case "$A$1"
MsgBox "A1 was just modified"
Case "$B$1"
MsgBox "B1 was just modified"
'..... etc. etc.. etc...
Case "$M$20"
MsgBox "M20 was just modified"
Case Else
' Do nothing
End Select
End Sub
Robert Crandal wrote:

I would like to monitor when any of the data in my group of
cells gets modified or deleted.

Here is how my code looks:

Private Sub Worksheet_Change (ByVal Target As Range)
Select Case Target
Case Range("A1")
MsgBox ("A1 was just modified")
Case Range "B1")
MsgBox ("B1 was just modified")
'..... etc. etc.. etc...
Case Range ("M20")
MsgBox ("M20 was just modified")
Case Else
' Do nothing
End Select
End Sub

Does anybody see anything wrong with this approach?? Is my code
correct?? How can I code this in a more efficient manner??

Thank you all!


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Using "Select Case" inside Worksheet_Change() subroutine.....

Hi robert,

I would prefer it like the following then all you have to do is add ranges
to the Union instead of lots of case lines.

Note that a space and underscore is a line break in an otherwise single line
of code.

Private Sub Worksheet_Change(ByVal Target As Range)

Dim rngUnion As Range
Dim rngIsect As Range

Set rngUnion = Union(Range("A1"), _
Range("B1"), Range("M20:P40"), _
Range("R20:R40"))

Set rngIsect = _
Application.Intersect(Target, rngUnion)

If Not rngIsect Is Nothing Then
MsgBox Target.Address(0, 0) & _
" was just modified"
End If

End Sub

--
Regards,

OssieMac


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select Case syntax for multiple conditions ("and")? ker_01 Excel Programming 5 October 23rd 09 01:03 AM
Select Case "Procedure to large" Error Little Penny[_3_] Excel Programming 1 May 9th 09 08:04 PM
how do I count only lower case "x" and exclude upper case "X" jbeletz Excel Worksheet Functions 3 October 14th 06 10:50 PM
Why Error Message "End Select without Select Case"? GoFigure[_13_] Excel Programming 5 December 9th 05 12:26 AM
Fix Code: Select Case and "Contains" selection Bettergains Excel Programming 5 April 26th 05 02:22 AM


All times are GMT +1. The time now is 05:50 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"