Help to clarify the macro

K0st4din

Well-known Member
Joined
Feb 8, 2012
Messages
501
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
Hello, everyone,
I need a little explanation because I get confused or I do not know how to do it.
I found what I need as a macro, but on a page of the Microsoft, the final is somehow explained, at least difficult for me.


Here is the article:

https://www.microsoft.com/en-us/mic...mit-the-number-of-times-a-file-can-be-opened/

I'm not clear if the macros are locked in workbook, there's really no way to go in and change the opening time of the file itself.


Man says to do, a macro of this kind:

"To reset this to zero on your own machine, you can either run SaveSetting "Demo", "Demo", "Demo", 0, or you can run another variation of the VBA called DeleteSetting. This syntax is:
DeleteSetting "Demo", "Demo", "Demo", 0"


My first request is whether I correctly wrote
Code:
[B]DeleteSetting "Demo", "Demo", "Demo", 0[/B]
or is not it correct?
My second question is - To work, should I make this macro in another workbook?
Because if it is in the same workbook, after the specified number of openings, the workbook will close and there is no option to cancel.
Thank you in advance to everyone.
Greetings
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi,

I read the post - First let me say Thank you for posting this question. This is not a concept I have come across yet and I am glad I did.

According to Microsoft's site DeleteSetting takes 3 arguments and you have 4 in your example.

[FONT=&quot]DeleteSetting appname,[/FONT][FONT=&quot] [/FONT][FONT=&quot]section[/FONT][FONT=&quot] [ [/FONT][FONT=&quot],[/FONT][FONT=&quot] [/FONT][FONT=&quot]key[/FONT][FONT=&quot] ][/FONT]

In regards to where to put the Code - I am questioning how you are trying to use this. The original article on the topic was being used to only allow a workbook to open X number of times and his code was placed directly in the On Open event of the workbook. I am questioning how you are trying to use this as you made the comment

Because if it is in the same workbook, after the specified number of openings, the workbook will close and there is no option to cancel.

This is the intention of the original article so how are you trying to use this?
 
Upvote 0
Hello,
my idea is this:
Example:
Workbook "Main" - Set the first macro of the article.

This workbook, I send it to the employee to open it 6 times. After that, he will not be able to open it, so he saw the information in the workbook.
This file, however, I have it and I open it a lot more times and look for a way instead of unlocking the module, then resetting the openings, etc. with DeleteSetting "Demo", "Demo", "Demo", 0, is much faster.
The author did not explain where to put this DeleteSetting "Demo", "Demo", "Demo", 0.
Personally, for me it should be something of the sort:
Code:
Sub delete_settting()
DeleteSetting "Demo", "Demo", 0
End SUb
By the way of logic, if I put the deletion in the "Main" file, and if the VBA code is password protected, the user will not be able to easily prevent the demo program from ending. So I think it should be in another Excel file
Quote: To reset this to zero on your own machine, you can either run SaveSetting "Demo", "Demo", 0, or you can run another variation of the VBA called DeleteSetting.
PS - I think the arguments should be 4th because -> SaveSetting is 4 -> SaveSetting "Demo", "Demo", "Demo", n
 
Last edited:
Upvote 0
Maybe something like this?

Code:
Public Const AdminPass = "TestPass"


' This is placed within Workbook Open
Private Sub Workbook_Open()
    ' Gets the count of times opened already
    n = GetSetting("Demo", "Demo", "Demo", 0) + 1
    ' Checkes if times open exceeding limit
    If n > 5 Then
        'Calls a message alerting limit exceeded and closes the workbook
        myReply = MsgBox("This file has reached its maximum usage!", vbCritical + vbYesNo + vbQuestion)
        If myReply = vbYes Then
            myPass = InputBox("Please enter the Admin Password", "Admin ByPass", "Enter Password")
            If myPass = AdminPass Then
                DeleteSetting "Demo", "Demo"
                MsgBox "Reset Count"
                Exit Sub
            Else
                MsgBox "Invalid Password!"
                Application.DisplayAlerts = False
                Application.Quit
            End If
        Else
            Application.DisplayAlerts = False
            Application.Quit
        End If


    End If
    'Saves the count of times opened.
    SaveSetting "Demo", "Demo", "Demo", n
End Sub
 
Upvote 0
I will caution this is only so secure and if someone knows what they are doing they could easily change this value to bypass this even if the excel code is password protected.
 
Upvote 0
To clarify as I noticed it was off

This goes in a standard module

Code:
Public Const AdminPass = "TestPass"

This goes in the code of the workbook directly

Code:
' This is placed within Workbook Open
Private Sub Workbook_Open()
    ' Gets the count of times opened already
    n = GetSetting("Demo", "Demo", "Demo", 0) + 1
    ' Checkes if times open exceeding limit
    If n > 5 Then
        'Calls a message alerting limit exceeded and closes the workbook
        myReply = MsgBox("This file has reached its maximum usage!", vbCritical + vbYesNo + vbQuestion)
        If myReply = vbYes Then
            myPass = InputBox("Please enter the Admin Password", "Admin ByPass", "Enter Password")
            If myPass = AdminPass Then
                DeleteSetting "Demo", "Demo"
                MsgBox "Reset Count"
                Exit Sub
            Else
                MsgBox "Invalid Password!"
                Application.DisplayAlerts = False
                Application.Quit
            End If
        Else
            Application.DisplayAlerts = False
            Application.Quit
        End If


    End If
    'Saves the count of times opened.
    SaveSetting "Demo", "Demo", "Demo", n
End Sub
 
Upvote 0
Hello, I'll try it out right now.
A little stupid question: The password should be - TestPass
 
Upvote 0
Everything works properly.
I thank you with all my heart.
Last request, how to make the password not visible when I write it.
Be of the type: *********
 
Upvote 0
Apparently in order to make a the InputBox used show **** vs showing the password itself is actually rather challenging.

A simpler option is to create a UserForm within the code window and then use this to prompt the password. The reason is by using a UserForm the text boxes within allow you to mask the textboxes with *** input.

In order to do this you need to create a UserForm
Add 2 buttons - OK and Cancel
Then add a label and set the text to reflect "Password:" and add a textbox.

When you add the TextBox you will right click and look at the properties, within the list of options you should see PassWord Char. Type in * or w/e you want to appear in place of the typed text.

When you add the buttons to the form, you will double click on the OK button which will take you into the Click command for that button. This should be the code for the OK button. Note I used the default names for the items which are reflected in the code, if you change the names you will need to change the code to reflect this. Meaning TextBox1, UserForm1, CommandButton1, CommandButton2

Also note there is a difference between the Name and the Text or Caption that appears on the item itself.

The code behind the OK button
Code:
Private Sub CommandButton1_Click() ' OK Button
myPass = TextBox1.Text
UserForm1.Hide
End Sub

The code behind the Cancel button
Code:
Private Sub CommandButton2_Click() ' Cancel Button
UserForm1.Hide
End Sub

This code goes in "ThisWorkbook" - The Workbook Code
Code:
' This is placed within Workbook Open
Private Sub Workbook_Open()
    ' Gets the count of times opened already
    n = GetSetting("Demo", "Demo", "Demo", 0) + 1
    ' Checkes if times open exceeding limit
    If n > 5 Then
        'Calls a message alerting limit exceeded and closes the workbook
        myReply = MsgBox("This file has reached its maximum usage! Would you like to reset with Admin Password?", vbCritical + vbYesNo + vbQuestion)
        If myReply = vbYes Then
            UserForm1.Show
            If myPass = AdminPass Then
                DeleteSetting "Demo", "Demo"
                MsgBox "Reset Count"
                UserForm1.Hide
                Exit Sub
            Else
                MsgBox "Invalid Password!"
                Application.DisplayAlerts = False
                Application.Quit
            End If
        Else
            Application.DisplayAlerts = False
            Application.Quit
        End If


    End If
    'Saves the count of times opened.
    SaveSetting "Demo", "Demo", "Demo", n
End Sub

This goes in a standard module
Code:
Public Const AdminPass = "TestPass"
Public myPass As String
 
Last edited:
Upvote 0
This is amazing.
And you're number one.
Everything has gotten right.
You must be given a medal.
Thank you so much.
I think administrators should put the topic in important !!! :)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top