Consulting

Results 1 to 19 of 19

Thread: Click on a cell, which one was it?

  1. #1

    Click on a cell, which one was it?

    If I click a cell with the left mouse the name box displays the cell id.

    Could anyone tell me where this is stored end what command would I use to add it in to a routine.

    e.g I click on C4 I would like to get the contents of C4 and transfer them to another cell. But I can not find how to do it. That is why I would like to know how Name box knows, and then hopefully I can do the same thing in my routine.

  2. #2
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    If you use the Worksheet_SelectionChange event, it provides a range argument called Target that represents the selected cell(s). You can use Target.Value to get the contents. If you are running the code manually, you can just use Selection.value to get the value of the selected cells.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    The cell clicked on is the Activecell. You can address all of its properties through Activecell.
    ____________________________________________
    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

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hello,

    Maybe you could describe your process a little more here. Depending on what you are doing, maybe we could suggest an alternative method or some ways of making it easier on you.

  5. #5
    Thank you guys, I have been searching using things like "Excel cell contents", "Excel mouse click" and allsorts of other things. Got loads of info but not what I wanted.

    YOU have set me on the right road in one question, THANK YOU.

  6. #6
    Sorry Fireftyr, missed your comment.

    I am writing a lap scorer for racing using excel and this is my first attempts with VBA so I am struggling like feck at the moment. But I would like to thank each and everyone of you who keeps offering help and support.

  7. #7
    Following on, I have tried to make one cell store the result of two cells selected by clicking and then added them together.

    I only seem to show the cell thats active when I click on the cmdbtn I have used to run the macro.

    Could anyone offer some sample code. Thank you.

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Show your code?
    ____________________________________________
    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

  9. #9
    Please be gentle I am a beginner.

    Dim num1 As Integer
    Dim num2 As Integer
    MsgBox "Click first Cell "
    num1 = ActiveCell.Value
    Sheet6.Cells(10, 8) = num1
    MsgBox "Click second Cell "
    num2 = ActiveCell.Value
    Sheet6.Cells(11, 8) = num2
    Sheet6.Cells(12, 8) = Sum(H10, H11)


    All I want to do is click one cell Click another and add.

    Its killing me searching the net.

  10. #10
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Try this:
    [VBA]Dim rngFirst As Range, rngSecond As Range
    Set rngFirst = Application.InputBox(prompt:="Click first cell", Title:="Select 1st cell", Type:=8)
    Set rngSecond = Application.InputBox(prompt:="Click second cell", Title:="Select 2nd cell", Type:=8)
    With Sheet6
    .Cells(10, 8).Value = rngFirst.Value
    .Cells(11, 8).Value = rngSecond.Value
    .Cells(12, 8).FormulaR1C1 = "=Sum(R[-2]C:R[-1]C)"
    End With
    [/VBA]
    Regards,
    Rory

    Microsoft MVP - Excel

  11. #11
    Rory, I have put this in my cmdbtn_click sub and it works for the two inputs but I am getting a 424 runtime object reqd whan I would expect to see the result.

    I am sorry about this but have I put it in the wrong place.

  12. #12
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    On which line? Did you press OK after selecting each cell rather than Cancel?
    Regards,
    Rory

    Microsoft MVP - Excel

  13. #13
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    This version will put 0 in the relevant cell if you don't choose a valid range:
    [VBA] Dim varOne, varTwo
    varOne = Application.InputBox(prompt:="Click first cell", Title:="Select 1st cell", Type:=8)
    varTwo = Application.InputBox(prompt:="Click second cell", Title:="Select 2nd cell", Type:=8)
    With Sheet6
    If varOne <> False Then
    .Cells(10, 8).Value = varOne
    Else
    .Cells(10, 8).Value = 0
    End If
    If varTwo <> False Then
    .Cells(11, 8).Value = varTwo
    Else
    .Cells(11, 8).Value = 0
    End If
    .Cells(12, 8).FormulaR1C1 = "=Sum(R[-2]C:R[-1]C)"
    End With
    [/VBA]
    Regards,
    Rory

    Microsoft MVP - Excel

  14. #14
    Rory, I am still having problems, could my sheet be set different to yours.

    I have done it on a blank sheet and still having difficulty, so I have zipped it up.

    Sorry for being a total pr**k.

  15. #15
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    You don't have a Sheet6! (I assumed you did since it was in your original code!)
    Try this, adapting sheet names as necessary:

    [VBA]
    Dim varOne, varTwo
    varOne = Application.InputBox(prompt:="Click first cell", Title:="Select 1st cell", Type:=8)
    varTwo = Application.InputBox(prompt:="Click second cell", Title:="Select 2nd cell", Type:=8)
    With Sheets("Sheet1")
    If varOne <> False Then
    .Cells(10, 8).Value = varOne
    Else
    .Cells(10, 8).Value = 0
    End If
    If varTwo <> False Then
    .Cells(11, 8).Value = varTwo
    Else
    .Cells(11, 8).Value = 0
    End If
    .Cells(12, 8).FormulaR1C1 = "=Sum(R[-2]C:R[-1]C)"
    End With
    [/VBA]
    Regards,
    Rory

    Microsoft MVP - Excel

  16. #16
    Rory , Sorry for being an ass, I am doing my best to learn the basics.

    Thank you for your help AND patience.

    All ok now, can you suggest a book that will help me get into prgramming for excel.

  17. #17
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Depends how quickly you learn! 'Dummies'-type books will get you started but I think they are a waste of money because they are pretty limited. John Walkenbach's VBA books are good (as are all his Excel books that I have read) and the Wrox Press 'Excel 200n VBA' series (there are versions for 2000, 2002, 2003 and I think 2007) are good - either the 2002 or 2000 would probably be my pick.
    Regards,
    Rory

    Microsoft MVP - Excel

  18. #18
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by rory
    Depends how quickly you learn! 'Dummies'-type books will get you started but I think they are a waste of money because they are pretty limited. John Walkenbach's VBA books are good (as are all his Excel books that I have read) and the Wrox Press 'Excel 200n VBA' series (there are versions for 2000, 2002, 2003 and I think 2007) are good - either the 2002 or 2000 would probably be my pick.
    Not the 2003 version, that is a very poor revision, lots of errors.
    ____________________________________________
    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

  19. #19
    Hey, Just a bit of input on the books. I just started VBA a couple of months ago and used the Excel VBA Programming for Dummies as a starter, and found it invaluable. It is quite simple and I found I soon had questions that it couldn't answer. I now have the Excel 2002 VBA book that Rory mentioned and am finding it a very useful reference, but its not as easy to read and understand as the -For Dummies one and I think I would have been totally lost had I started with it.

    I would probably recommend seeing if you can borrow/get out of the library a copy of the Dummies book and then possibly invest in one of the ones that Rory Recommended

Posting Permissions

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