#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Selecting Cells with a mouse


    I am trying to write a program that promts the user to input a percentage at the start of a program and then prompt the user to select the cells with a mouse that they want to modify (these cells are not in any specific order) and click OK when done. The program would then take the value of each cell (individually) and multiple that by the percentage (I would also like the program to take this number and round down to the nearest number that is divisible by 12). How would the code look in Visual Basic for the mouse click and the math.
    Thank You.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    What do you mean by "cell". Are you talking about a DataGrid, a FlexGrid, or a MSHFlexGrid?

    J.A. Coutts
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    a cell on an excel spreadshhet
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    289
    Rep Power
    44
    Sorry, can't help you. I have very limited knowledge of using VBA with Excel.

    J.A. Coutts
  8. #5
  9. No Profile Picture
    Stumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,409
    Rep Power
    4538
    Check the online VBA help in excel and you should a section showing the various mouse events available to your code.
    ======
    Doug G
    ======
    It is a truism of American politics that no man who can win an election deserves to. --Trevanian, from the novel Shibumi
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    basic mouse handling


    Basic mouse handling is in the sheet code not in the modules, you can access the sheet code by right clicking the sheet tab and selecting View Code

    Put this in: -

    Right clicks will prompt for a value and double clicks will apply it


    Public xvalue

    Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
    Cancel As Boolean)

    Selection = Selection + xvalue

    End Sub

    Sub Worksheet_BeforeRightClick(ByVal Target As Range, _
    Cancel As Boolean)

    xvalue = InputBox("Value to use", "Hello")


    End Sub
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Thanks GGriggs...this works awesome!! Now I have another question, is it possible to highlight other cells in the same row as the one that is "double clicked"
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    selection


    'get the row and column of your selection

    NowRow = selection.row
    NowColumn = selection.column

    'work out top left corner of what you want to select perhaps

    TopLeftCornerRow = NowRow-4
    TopLeftCornerColumn= NowColumn -1

    'work out bottom right corner of what you want to select perhaps

    BottomRightCornerRow = NowRow+7
    BottomRightCornerColumn= NowColumn

    'Then select it

    range(cells(TopLeftCornerRow,TopLeftCornerColumn),cells(BottomRightCornerRow ,BottomRightCornerColumn)).select


    'as a tip given to me many years ago note the capital letters in the variable names, it makes your code an awful lot easier to read
    Last edited by GGriggs; February 9th, 2013 at 03:15 AM. Reason: typo

IMN logo majestic logo threadwatch logo seochat tools logo