msgbox in VBA

P

peyman

hi,
how can I write a code by which when only a cell like D11, is selected, a
msgbox pops up?
thanx
 
M

Mike

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If ActiveCell.Address <> Range("D11").Address Then Exit Sub
MsgBox "Cell D11 is selected"
End Sub
 
P

peyman

thanks Mike.that's great.I have one more question.what's the difference
between "worksheet code module" and "standard code module"?what codes are
usually posted in "standard module"?
thank you again
 
B

Bob Phillips

Worksheet module is the code module behind a worksheet, it is a specific
type of class module. Typically, it is where you add code specific to that
worksheet, such as the worksheet event code that you were given.

A standard code module is where you add procedures that are more generic, or
need to be accessed from multiple places.

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
M

Mike

Put this into a standard module
Sub exampleforstandardmodulecode()
If ActiveCell.Address <> Range("D11").Address Then Exit Sub
MsgBox "Cell D11 is selected"
End Sub
Put this into a Thisworkbook module
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target
As Range)
Run "exampleforstandardmodulecode"
End Sub

Now for every sheet in workbook when D11 is selected your msgbox will pop up
 
P

peyman

than you MIke.got it.

Mike said:
Put this into a standard module
Sub exampleforstandardmodulecode()
If ActiveCell.Address <> Range("D11").Address Then Exit Sub
MsgBox "Cell D11 is selected"
End Sub
Put this into a Thisworkbook module
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target
As Range)
Run "exampleforstandardmodulecode"
End Sub

Now for every sheet in workbook when D11 is selected your msgbox will pop up
 
Top