+ Reply to Thread
Results 1 to 8 of 8

Run macro when enter a value in a cell

Hybrid View

SMILE Run macro when enter a value... 09-07-2011, 03:47 PM
Mordred Re: Run macro when enter a... 09-07-2011, 03:52 PM
SMILE Re: Run macro when enter a... 09-07-2011, 04:03 PM
Mordred Re: Run macro when enter a... 09-07-2011, 04:10 PM
SMILE Re: Run macro when enter a... 09-07-2011, 04:41 PM
mudraker Re: Run macro when enter a... 09-07-2011, 09:46 PM
Mordred Re: Run macro when enter a... 09-07-2011, 10:19 PM
SMILE Re: Run macro when enter a... 09-08-2011, 02:10 AM
  1. #1
    Forum Contributor
    Join Date
    01-14-2004
    Location
    India
    MS-Off Ver
    365
    Posts
    264

    Run macro when enter a value in a cell

    Hi everyone

    I need a help ..
    In my worksheet "EDIT" if I enter any value in A1, I want a macro (mymacro) to run automatically

    Can someone help in coding..
    Thanks
    Tom
    Last edited by SMILE; 09-08-2011 at 02:11 AM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Run macro when enter a value in a cell

    Hi SMILE, try
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address = "$A$1" Then
        '''Your code here'''
        End If
    End Sub
    EDIT: Sorry for skipping this part but the previous code needs to go into your worksheet module in the VBE.
    Last edited by Mordred; 09-07-2011 at 03:54 PM.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Forum Contributor
    Join Date
    01-14-2004
    Location
    India
    MS-Off Ver
    365
    Posts
    264

    Re: Run macro when enter a value in a cell

    Hi..Mordred
    Thanks for the reply but I am getting the following error
    compile error:
    Ambiguous name detected:Worksheet_Change

    Is it because I have another codes in that worksheet as below
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Const sTRIGGER_CELL = "e11"
    
    If Not Intersect(Target, Range(sTRIGGER_CELL)) Is Nothing Then
      Select Case UCase(Range(sTRIGGER_CELL).Value)
          Case "Y"
              yes
           Case "N"
               no
       End Select
    End If
    
    End Sub
    Hope you will sort it out for me.
    Thanks
    Tom

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Run macro when enter a value in a cell

    Yes, it is because of that code. You can get around this by either:
    1. erase the other code
    2. add the if statement from my solution to your existing code
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Const sTRIGGER_CELL = "e11"
    
    If Target.Address = "$A$1" Then
        '''Your code here'''
        End If
    
    
    If Not Intersect(Target, Range(sTRIGGER_CELL)) Is Nothing Then
      Select Case UCase(Range(sTRIGGER_CELL).Value)
          Case "Y"
              yes
           Case "N"
               no
       End Select
    End If
    
    End Sub
    or you can use the double click event
    Private Sub Worksheet_BeforeDoubleClick(ByVal _
        Target As Range, Cancel As Boolean)
        If Target.Address = "$A$1" Then
        '''Your code here'''
        End If
    End Sub

  5. #5
    Forum Contributor
    Join Date
    01-14-2004
    Location
    India
    MS-Off Ver
    365
    Posts
    264

    Re: Run macro when enter a value in a cell

    Hi Mordred
    I am really sorry to bother you again...
    Your code is working perfect..but I need to add one more condition
    My existing code is as below
    Private Sub Worksheet_Change(ByVal Target As Range)
    Const sTRIGGER_CELL = "e11"
    
        If Target.Address = "$a$1" Then
        '''Your code here'''
        
            Range("B13:e18").Select
           Selection.clearcontents
        Range("e11").Select
      
        End If
    
    If Not Intersect(Target, Range(sTRIGGER_CELL)) Is Nothing Then
      Select Case UCase(Range(sTRIGGER_CELL).Value)
          Case "Y"
              yes
           Case "N"
               no
       End Select
    End If
    
    End Sub
    I need the following condtion also to be added
    IF I go to A1 and press back space (no value) and enter, then I just need the following cell to select.
     Range("e11").Select
    If any other value enter in A1, then I need the following as my previous code

            Range("B13:e18").Select
           Selection.clearcontents
        Range("e11").Select
    Thanks
    tom

  6. #6
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Run macro when enter a value in a cell

    just add code to test value of a1

    if range("a1").value = "" then exit sub
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  7. #7
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Run macro when enter a value in a cell

    Hi Tom, try the following out
    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Const sTRIGGER_CELL = "e11"
    
        If Target.Address = "$A$1" And Target.Value <> "" Then
            '''Your code here'''
            Application.EnableEvents = False
            Range("B13:e18").ClearContents
            Range("E11").Select
            Application.EnableEvents = True
        ElseIf Target.Address = "$A$1" And Target.Value = "" Then
            Application.EnableEvents = False
            Range("E11").Select
            Application.EnableEvents = True
        End If
    
        'If Not Intersect(Target, Range(sTRIGGER_CELL)) Is Nothing Then
        '  Select Case UCase(Range(sTRIGGER_CELL).Value)
        '      Case "Y"
        '          yes  <<---|
        '       Case "N"     |-------What defines these?
        '           no  <<---|
        '   End Select
        'End If
    
    End Sub
    Note: I commented out the existing code because I couldn't run it and am honestly confused as to how the variables yes and no are defined or known! At any rate, see if the code works the way it is and then remove the single quotes for the other code.

    Regards:

  8. #8
    Forum Contributor
    Join Date
    01-14-2004
    Location
    India
    MS-Off Ver
    365
    Posts
    264

    Re: Run macro when enter a value in a cell

    Thank u Mordred.. Its working perfect...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1