dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: need help to understand code

  1. #1
    Join Date
    Mar 2020
    Posts
    5

    need help to understand code

    hello guys i am wondering if any of you could explain what this code do and how it works.
    Code:
    Private Sub Workbook_Open()
        lastSelectionChange = Now
        closeWB
    End Sub
    
    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
        lastSelectionChange = Now
        'MsgBox (Now)
    End Sub
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        lastSelectionChange = Now
        'MsgBox (Now)
    End Sub
    
    
    Module 1 
    Public lastSelectionChange As Date
    
    
    Sub closeWB() // sluit workbook
        If DateDiff("n", lastSelectionChange, Now) > 5 Then
            ThisWorkbook.Close SaveChanges:=True
        Else
            Application.OnTime Now + TimeSerial(0, 1, 0), "closeWb"
        End If
    End Sub
    I am kinda new and wonder how this works . like what is a function or what value's do they have.

    Kind regards
    Last edited by VictorN; March 19th, 2020 at 03:07 AM. Reason: added CODE tags

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,337

    Re: need help to understand code

    Well you do not have any user functions there in that code. You do have a few subs.

    The difference between the two in VB is that a function returns a value, a sub does not.

    You have a few internal functions called Now() and DateDiff() for example both of these return values.
    The values returned depend on the function and usually what is passed to it as arguments when it is called in code.

    I am not an Excel coder but it looks like all that code is doing is setting the time of the last change and upon close if the last change was more than 5 minutes ago it saves it. Kind of odd but that appears to be what it does.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Mar 2020
    Posts
    5

    Re: need help to understand code

    Quote Originally Posted by DataMiser View Post
    Well you do not have any user functions there in that code. You do have a few subs.

    The difference between the two in VB is that a function returns a value, a sub does not.

    You have a few internal functions called Now() and DateDiff() for example both of these return values.
    The values returned depend on the function and usually what is passed to it as arguments when it is called in code.

    I am not an Excel coder but it looks like all that code is doing is setting the time of the last change and upon close if the last change was more than 5 minutes ago it saves it. Kind of odd but that appears to be what it does.
    hello,

    this is indeed right and correct but i dont get how it is working and would like to understand.
    Is it possible for someone to put comment next to the code so i know what is doing what ?
    for example:
    If DateDiff("n", lastSelectionChange, Now) > 5 Then// if ....... (....,.......,.....) is bigger then 5 minutes then ......

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,624

    Re: need help to understand code

    Quote Originally Posted by sylviam View Post
    hello,

    this is indeed right and correct but i dont get how it is working and would like to understand.
    Is it possible for someone to put comment next to the code so i know what is doing what ?
    for example:
    If DateDiff("n", lastSelectionChange, Now) > 5 Then// if ....... (....,.......,.....) is bigger then 5 minutes then ......
    Maybe.
    But you should post your code in a readable format!
    So, please, use CODE tags. Read the Announcement: Before you post....
    Victor Nijegorodov

  5. #5
    Join Date
    Mar 2020
    Posts
    5

    Re: need help to understand code

    Quote Originally Posted by VictorN View Post
    Maybe.
    But you should post your code in a readable format!
    So, please, use CODE tags. Read the Announcement: Before you post....
    like this ?

    Workbook
    Code:
    Private Sub Workbook_Open()
        lastSelectionChange = Now
        closeWB
    End Sub
    
    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
        lastSelectionChange = Now
        'MsgBox (Now)
    End Sub
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        lastSelectionChange = Now
        'MsgBox (Now)
    End Sub

    Module 1
    Code:
    Public lastSelectionChange As Date
    
    
    Sub closeWB() // sluit workbook
        If DateDiff("n", lastSelectionChange, Now) > 5 Then
            ThisWorkbook.Close SaveChanges:=True
        Else
            Application.OnTime Now + TimeSerial(0, 1, 0), "closeWb"
        End If
    End Sub

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,624

    Re: need help to understand code

    Quote Originally Posted by sylviam View Post
    like this ?

    Workbook
    Code:
    Private Sub Workbook_Open()
        lastSelectionChange = Now
        closeWB
    End Sub
    
    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
        lastSelectionChange = Now
        'MsgBox (Now)
    End Sub
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
        lastSelectionChange = Now
        'MsgBox (Now)
    End Sub

    Module 1
    Code:
    Public lastSelectionChange As Date
    
    
    Sub closeWB() // sluit workbook
        If DateDiff("n", lastSelectionChange, Now) > 5 Then
            ThisWorkbook.Close SaveChanges:=True
        Else
            Application.OnTime Now + TimeSerial(0, 1, 0), "closeWb"
        End If
    End Sub
    Exactly!
    Victor Nijegorodov

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,624

    Re: need help to understand code

    Quote Originally Posted by sylviam View Post
    like this ?

    Workbook
    PHP Code:
    Private Sub Workbook_Open()
        
    lastSelectionChange Now
        closeWB
    End Sub
    ... 
    No, your code is not a PHP. So use CODE tags, not the PHP.
    Victor Nijegorodov

  8. #8
    Join Date
    Mar 2020
    Posts
    5

    Re: need help to understand code

    Quote Originally Posted by VictorN View Post
    No, your code is not a PHP. So use CODE tags, not the PHP.
    ok thanks

    i hope someone can help me to correctly understand the code and what everything does .

  9. #9
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,337

    Re: need help to understand code

    Well your first step should be to look up the commands used in your online help or google them. You will get a much more detailed description that way.

    In short the code sets a variable using the Now() function which returns the current date and time.

    DateDiff as used compares the variable that was set in the other code to the current date and time Now() and returns a value in minutes.

    The best way I find to understand this stuff is first lookup the details on the statements used then make a change to the code and see what happens. Is it what you expected or not. If yes then you are on your way to understanding it if no then try again. Of course if the code is important at all make sure to have a backup before making any changes to it just in case.
    Always use [code][/code] tags when posting code.

  10. #10
    Join Date
    Mar 2020
    Posts
    5

    Re: need help to understand code

    Thank you already for the explenation.
    I did played and figured out that it indeed represent times .
    but for example this stuff
    Code:
    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) // the ByVal and Sh etc what does this do ?

  11. #11
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,337

    Re: need help to understand code

    That is more specific to Excel which I do not code in. What you are looking at is an event that is triggered when a selection is made or changed info is passed to the event in Sh and Value. I assume Sh is the sheet and value the range of selected cells but again I do not code in Excel. I code heavily in VB but never liked using Excel much.
    Always use [code][/code] tags when posting code.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)