-
March 18th, 2020, 05:50 PM
#1
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
-
March 18th, 2020, 11:57 PM
#2
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.
-
March 19th, 2020, 02:55 AM
#3
Re: need help to understand code
Originally Posted by DataMiser
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 ......
-
March 19th, 2020, 03:13 AM
#4
Re: need help to understand code
Originally Posted by sylviam
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
-
March 19th, 2020, 03:27 AM
#5
Re: need help to understand code
Originally Posted by VictorN
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
-
March 19th, 2020, 03:30 AM
#6
Re: need help to understand code
Originally Posted by sylviam
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
-
March 19th, 2020, 03:37 AM
#7
Re: need help to understand code
Originally Posted by sylviam
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
-
March 19th, 2020, 04:11 AM
#8
Re: need help to understand code
Originally Posted by VictorN
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 .
-
March 19th, 2020, 01:09 PM
#9
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.
-
March 19th, 2020, 05:48 PM
#10
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 ?
-
March 19th, 2020, 09:31 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|