+ Reply to Thread
Results 1 to 8 of 8

Code That Allows User to Select Two Cells and Get the Difference as an Output

  1. #1
    Registered User
    Join Date
    09-02-2012
    Location
    New Roads, LA
    MS-Off Ver
    Excel 2007\10
    Posts
    22

    Code That Allows User to Select Two Cells and Get the Difference as an Output

    I'm very new to VBA but, I have a spreadsheet with start and stop times and would like an application or macro that would allow the user to get the span of a selected Start and Stop time. I know this might be a little complicated but, I would appreciate any leads you could offer.

    Thanks in advance.
    B

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Code That Allows User to Select Two Cells and Get the Difference as an Output

    Be sure to put the start time first:

    Please Login or Register  to view this content.
    PHP Code: 
    1:30:15    4:47:12    3:16:57 
    PHP Code: 
    1:30:15    4:47:12   =Span(H9,I9
    Last edited by xladept; 09-05-2012 at 08:43 PM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    09-02-2012
    Location
    New Roads, LA
    MS-Off Ver
    Excel 2007\10
    Posts
    22

    Re: Code That Allows User to Select Two Cells and Get the Difference as an Output

    XLAdept

    Thank you for the expedient reply. What I'm really trying to do is build a macro that would subtract the min time from the max time of a User Selection that isn't a specified range until Selected and display the difference in any way but, the thing I can't figure out is how to get minimum and maximum values of a selection called up.
    I fear I'm not doing a very good job of explaining myself.
    The following is the idea of what I'm trying to accomplish but of course my syntax is all wrong and the code does not work. I'm really having trouble wrapping my head around VBA's hierarchy and syntax.

    Please Login or Register  to view this content.
    Last edited by Cutter; 09-08-2012 at 06:22 AM. Reason: Added code tags

  4. #4
    Registered User
    Join Date
    09-02-2012
    Location
    New Roads, LA
    MS-Off Ver
    Excel 2007\10
    Posts
    22

    Re: Code That Allows User to Select Two Cells and Get the Difference as an Output

    Thank you to all of you who post to this forum. I have a piece of code that is working so far. If so inclined before I mark as solved could someone explain
    why the following code works? I mostly don't understand why I sometimes need an object like range before selection and in this case omitting anything in front of Selection was the ticket. I got this working mostly by accident. Also is this the most efficient form of what I'm trying to accomplish?

    Thanks

    PHP Code: 
    Sub MinMaxDiff()

    Dim StartTime As DateStopTime As Date
    Dim Duration 
    As DateRng As Range

        Set Rng 
    Selection
        StartTime 
    Application.WorksheetFunction.Min(Rng)
        
    StopTime Application.WorksheetFunction.Max(Rng)
        
    Duration StopTime StartTime

        msgbox Duration

    End Sub 
    I am also unsure as to why I had to use the Set Rng = Selection.
    Last edited by jbwitty; 09-07-2012 at 07:32 PM.

  5. #5
    Registered User
    Join Date
    09-02-2012
    Location
    New Roads, LA
    MS-Off Ver
    Excel 2007\10
    Posts
    22

    Re: Code That Allows User to Select Two Cells and Get the Difference as an Output

    I am also unsure as to why I had to use the Set Rng = Selection.

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Code That Allows User to Select Two Cells and Get the Difference as an Output

    You don't need to set the object if you use selection:

    Please Login or Register  to view this content.
    But wouldn't you rather write the duration on the spreadsheet?

    Here's a function and a sub to call it:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Last edited by xladept; 09-07-2012 at 08:25 PM.

  7. #7
    Registered User
    Join Date
    09-02-2012
    Location
    New Roads, LA
    MS-Off Ver
    Excel 2007\10
    Posts
    22

    Re: Code That Allows User to Select Two Cells and Get the Difference as an Output

    Thank you again XLAdept.

    This works great and gives me a little further understanding as to how one can use the Selection range. I will mark as solved. Is there a plus side to calling up a function as opposed to getting the answer straight from a sub in this situation?

    Once again your time and attention is very much appreciated.

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Code That Allows User to Select Two Cells and Get the Difference as an Output

    You're welcome! - Functions can also be used as formulas in the spreadsheet.

    But you could reestablish the rng and, rather than actually selecting with the mouse, you could specify the actual range with the Set statement.

+ 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