Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

call code from within a cell via an If type statment 2

Status
Not open for further replies.

Elbown

IS-IT--Management
Oct 3, 2003
6
NL
I am a novice whene it comes to VBA within excel but I would like to kick off some code dependent on the result typed into a cell on "Sheet1" ie if(sheet1!a1= a, start code,do nothing)
Any ideas how to do this with out haveing to click buttons etc...?
 
HI,

You can use the worksheet_change event...
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    Set rng = Application.Intersect(Target, Range("A1"))
    If Not rng Is Nothing Then MyMacro
End Sub
assuming that A1 is the cell of interest.

You can get to the sheet event code by fight clicking the sheet tab and selecting view code.

Paste this code into the code window.

:)

Skip,
Skip@TheOfficeExperts.com
 
Hi,
Paste this code into the worksheet change event of the sheet you want the code to operate on.

Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1").Value = "a" Then
' start you code here
MsgBox "You inserted an 'a', don't do it again! OK", vbCritical, "What the?"
Else: MsgBox "The value of cell 'A1'is " & Range("A1").Value ' alternate code if cell changes but not 'a'
End If
End Sub

Mike [pipe]
 
Thanks Guys It all works like a charm :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top