Consulting

Results 1 to 13 of 13

Thread: Solved: how do I execute a macro on a cell click

  1. #1
    VBAX Newbie
    Joined
    Nov 2008
    Posts
    5
    Location

    Solved: how do I execute a macro on a cell click

    Dear all,

    I have written a macro and I am happy with it.
    I have assign this macro to a text box and things are runnning fine.

    I want to execute it when the users clicks on a given cell.
    What I think I miss is something like an event call cell_click or similar

  2. #2
    VBAX Mentor Hoopsah's Avatar
    Joined
    Nov 2007
    Posts
    303
    Location
    Hi bramanad

    If you right click on the tab you will be using and goto view code.

    Enter something like:

    [VBA]Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Select Case Target.Address
    Case "$D$10"
    Call Macro1
    End Select
    End Sub[/VBA]

    In this case when cell D10 is clicked on the macro "macro1" will be executed.
    I am playing all the right notes, but not necessarily in the right order.
    Eric Morecambe

  3. #3
    VBAX Newbie
    Joined
    Nov 2008
    Posts
    5
    Location
    hi hoopsah. thnaks for the suggestion.
    what I was missing is the Worksheet_selectionChange procedure.
    now I got my stuff up and running (see code below)

    alternate question becomes now. How can I get access to all existing
    predefiened procedure such Worksheet_selectionChange

    [VBA]Sub Workbook_open()
    Call Worksheet_SelectionChange
    End Sub
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Application.Intersect(Target, Range("H:H")) Is Nothing Then
    Call MyMacro
    End If
    End Sub[/VBA]

  4. #4
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    [vba]Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Not Intersect(Target, Range("A1:A1")) Is Nothing Then 'Enter the cell range that the user is going to clikc in here
    Call MacroName 'the name of your macro
    End If
    End Sub[/vba]
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You have some issuse there. Workbook code goes in the Thisworkbook code module, worksheet code goes into the worksheet code module. You cannot call from one to the other without making the procedures public, and prefix with the codemodule name.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi bramanad,
    Instead of spending my time helping you I just spent some time cleaning up your mess on this forum.

    You posted the identical post 3 times. You had responses in two of them.

    I cannot emphasize strongly enough how inefficient this is. It is not only confusing for you but for those trying to help you.

    Please only post a question one time and follow up questions in the same thread.

    Extra threads deleted or merged with this one.

    If you don't understand how the forum works then I would advise that you read our FAQ and spend some time trying to figure out how to post before you assail us with multiple posts with the same question in future. Thank you for your understanding.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    VBAX Newbie
    Joined
    Nov 2008
    Posts
    5
    Location
    dear xld,
    as it is now, the code behaves the way I want.
    Now if your suggestion helps me to writte with better coding practice, I
    will do it.

    what can go wrong if I dont ?

  8. #8
    VBAX Newbie
    Joined
    Nov 2008
    Posts
    5
    Location
    lucas, I am suprised, I have just clicked the "quick reply" button,
    typed my text then hit "post quick reply"
    I just read -quickly - the FAQ. I dont see what I have done wrong

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi bramanad,
    It happens I guess so please just be aware of it when/if it happens. If it happens again, contact an admin so we can delete/merge the posts.

    I apologize if I jumped to conclusions.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by bramanad
    dear xld,
    as it is now, the code behaves the way I want.
    I don't see how.

    Quote Originally Posted by bramanad
    what can go wrong if I dont ?
    It won't work?
    Last edited by Bob Phillips; 11-17-2008 at 09:20 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Bob is trying to tell you that this:
    [VBA]Sub Workbook_open()
    Call Worksheet_SelectionChange
    End Sub[/VBA]
    Will not work. You cannot call the worksheet_selectionChange event from the workbookOpen event as you have in the code above.

    The workbook open code must go in the thisworkbook module and the worksheet_selectionChange code must go in the code module for the sheet you want it to work on.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  12. #12
    VBAX Newbie
    Joined
    Nov 2008
    Posts
    5
    Location
    xld and bob claim it must not be working, but it does, and all is located in a shhet code. not in "thisworkbook" section.
    I just managed to make it work by respecting your suggestions
    but I wonder why I should bother (except for good programming practices)

  13. #13
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    No, you are wrong......if you put it all in the code for the sheet then the only code that is working is the worksheet_SelectionChange code.....the other is being ignored and can be deleted.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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