Consulting

Results 1 to 11 of 11

Thread: Do something when specific cell selected

  1. #1

    Do something when specific cell selected

    Hi can i have code fordoing something when specific cell selected?
    A mighty flame followeth a tiny sparkle!!



  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi there,

    Something like this?

    [vba]Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$A$4" Then
    MsgBox "Cell A4 has just been selected!"
    End If
    End Sub[/vba]

    Must be placed in the code module for the worksheet you want it to work on.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    Yes like that only thanks
    A mighty flame followeth a tiny sparkle!!



  4. #4
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    You're welcome!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  5. #5
    One problem in thuis code

    I'm unnble to perform cut & paste
    A mighty flame followeth a tiny sparkle!!



  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This works for me.
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Target.Address = "$A$4" Then
            DoMove
        End If
    End Sub
    Sub DoMove()
        Application.EnableEvents = False
        Range("C4:C7").Cut
        Range("E4").Select
        ActiveSheet.Paste
        Application.EnableEvents = True
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    see this file containing macro

    i am not able to perform cut(ctrl+x) & paste (ctrl+v) option after having this macro
    A mighty flame followeth a tiny sparkle!!



  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You have some logic problems here


    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Target.Address = "$A$2" Then
            Application.MoveAfterReturnDirection = xlToLeft
        Else
            Application.MoveAfterReturnDirection = xlDown
        End If
    End Sub

    Moving into cell A2 does change the direction to left, but you cannot go left. Clicking in any other cell returns the move direction to down.

    Regarding the cut and paste, you do not show this bit of your code, but you need the Events disabled to prevent the worksheet change event ocurring when you select the cell where the code is to be pasted.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    I think u r not getting me

    after using above code general cut & paste function is not working

    see file submitted by me for detail
    A mighty flame followeth a tiny sparkle!!



  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I see now! Sorry, I've no idea why this is happening, but maybe someone else can help.

    For some reason it seems the cut/copied value is being lost from the clipboard when the Application line runs. I've tried Application.Calculate as well and the same problem occurs.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I reposted your question here, for more assistance.
    http://www.vbaexpress.com/forum/showthread.php?t=4650
    It just seems to be the way the system works that is causing your problem
    Regards
    Malcolm
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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