PDA

View Full Version : Solved: simple activation key macro



sunilmulay
10-17-2008, 05:56 AM
Hi Guys
I'm trying to set up a simple Activation Key for my worksheet so I can control it's use.
Cell "O4" in the hidden worksheet "H00-Reference Data" will contain the activation key that the user needs to enter.
I want the key the user enters to be put in Cell "O5" of this hidden worksheet. Another macro will then compare the values, and if they are equal allow the user to work on the file, otherwise close the workbook.

I know the code below isn't right (I'm new to VBA), but i've made a start. the part of the code below: " .Cells("O5").Value = Case Is" is my useless attempt at getting the user entered key to be inserted in Cell O5.

Can anyone help with this?
Thanks
S


Sub ActivationKey()
'Prompt the user for a password and unhide the worksheet if correct
Select Case InputBox("Please enter your Activation Key", _
"Activation Key")

Case Is = Worksheets("H00-Reference Data").Cells("O4").Value
With Worksheets("H00-Reference Data")
.Cells("O5").Value = Case Is
End With
Case Else
MsgBox "Sorry, the activation key is incorrect!", _
vbCritical + vbOKOnly, "You are not authorized!"
ActiveWorkbook.Close False
End Select
End Sub

Bob Phillips
10-17-2008, 06:15 AM
Sub ActivationKey()
Dim mKey As String

mKey = InputBox("Please enter your Activation Key", _
"Activation Key")

If mKey <> "" Then

If mKey = Worksheets("H00-Reference Data").Cells("O4").Value Then

Exit Sub
End If
End If

MsgBox "Sorry, the activation key is incorrect!", _
vbCritical + vbOKOnly, "You are not authorized!"
ActiveWorkbook.Close False
End Sub

sunilmulay
10-17-2008, 07:24 AM
Hi There

Thanks for that. i also want the activation code the user enters to be put in Cell O5 though. i've tried the following code, but even if Cell O5 is blank and Cell O4 contains the required code, the activation key box isn't called up when opening the file for some reason......what am i doing wrong??

Private Sub Workbook_Open()
'Turn off screen updates
Application.ScreenUpdating = False
'Hide confidential sheet at startup

Call HideSheets

With ActiveWorkbook.Worksheets("H00-Reference Data")
If Range("O4").Value <> Range("O5").Value Then
Call ActivationKey
Else
With Worksheets("Project-Nav")
.Activate
.Range("D5").Select
End With
End If
End With
'Restore screen updates
Application.ScreenUpdating = True
End Sub

Sub ActivationKey()
Dim mKey As String

mKey = InputBox("Please enter your Activation Key", _
"Activation Key")

If mKey <> "" Then

If mKey = Worksheets("H00-Reference Data").Cells("O4").Value Then
Worksheets("H00-Reference Data").Cells("O5").Value = mKey
Exit Sub
End If
End If

MsgBox "Sorry, the activation key is incorrect!", _
vbCritical + vbOKOnly, "You are not authorized!"
ActiveWorkbook.Close False
End Sub

Sub HideSheets()
'Set worksheet to Very Hidden so that it can only be unhidden by a macro
Worksheets("H00-Reference Data").Visible = xlSheetVeryHidden
Worksheets("H01-EVGraphInfo").Visible = xlSheetVeryHidden

Kenneth Hobs
10-17-2008, 10:30 AM
Did you put the Workbook code in the ThisWorkbook object?

In your comparison in the Open event, you would also need to input the user's entry by tweaking xld's code by moving one line.
Sub ActivationKey()
Dim mKey As String

mKey = InputBox("Please enter your Activation Key", _
"Activation Key")

If mKey <> "" Then
Worksheets("H00-Reference Data").Cells("O5").Value = mKey
If mKey = Worksheets("H00-Reference Data").Cells("O4").Value Then
Exit Sub
End If
End If

MsgBox "Sorry, the activation key is incorrect!", _
vbCritical + vbOKOnly, "You are not authorized!"
ActiveWorkbook.Close False
End Sub

sunilmulay
10-17-2008, 07:07 PM
Hi Kenneth/XLD
For some reason, the Activation Box Dialog box still isn't called up, even though cells O4 in my hidden sheet has a value calculated by a formula, and Cell O5 is blank. I don't know what I'm doing wrong. I have the following code in ThisWorkbook:

Private Sub Workbook_Open()
'Turn off screen updates
Application.ScreenUpdating = False
'Hide confidential sheet at startup


Call HideSheets

With ActiveWorkbook.Worksheets("H00-Reference Data")
If Range("O4").Value <> Range("O5").Value Then
Call ActivationKey
End If
End With

'Activate cell A1 on the Dashboard sheet at startup
With Worksheets("Project-Nav")
.Activate
.Range("D5").Select
End With

'Restore screen updates
Application.ScreenUpdating = True
End Sub

and this is one in Module 10:

Sub HideSheets()
'Set worksheet to Very Hidden so that it can only be unhidden by a macro
Worksheets("H00-Reference Data").Visible = xlSheetVeryHidden
Worksheets("H01-EVGraphInfo").Visible = xlSheetVeryHidden

End Sub

Sub ActivationKey()
Dim mKey As String

mKey = InputBox("Please enter your Activation Key", _
"Activation Key")

If mKey <> "" Then
Worksheets("H00-Reference Data").Cells("O5").Value = mKey
If mKey = Worksheets("H00-Reference Data").Cells("O4").Value Then
Exit Sub
End If
End If

MsgBox "Sorry, the activation key is incorrect!", _
vbCritical + vbOKOnly, "You are not authorized!"
ActiveWorkbook.Close False
End Sub


Thanks in advance....
Sunil

sunilmulay
10-17-2008, 07:23 PM
I've tested it a bit more, and it just seems to skip the ActivationKey sub completely.....

sunilmulay
10-20-2008, 06:09 AM
Hi Kenneth/XLD

Any chance you can help me resolve the above?

Many thanks!
Sunil

Kenneth Hobs
10-20-2008, 06:37 AM
If you post an example xls, it might help us troubleshoot it.

sunilmulay
10-21-2008, 01:17 AM
Sure - attached is the simpliefied file. There is a macro which hide sheets on opening. To reveal the hdden sheet, you need to run the macro called ShowSheets and the password is password.

Sometimes the activation box seems to pop up, other times it doesn't, but it never enters the user input into the cell I want it to....

Thanks
Sunil

Kenneth Hobs
10-21-2008, 05:22 AM
When using With, be sure to prefix the Range with a period as corrected below.


Private Sub Workbook_Open()
'Turn off screen updates
Application.ScreenUpdating = False
'Hide confidential sheet at startup


Call HideSheets

With ActiveWorkbook.Worksheets("H00-Reference Data")
If .Range("O4").Value <> .Range("O5").Value Then
Call ActivationKey
End If
End With

'Activate cell A1 on the Dashboard sheet at startup
With Worksheets("Project-Nav")
.Activate
.Range("D5").Select
End With

'Restore screen updates
Application.ScreenUpdating = True
End Sub


I would recommend that you just call the Activation sub rather than compare O4 and 05 unless this is just a one time deal.

I would also recommend that you display a userform with a textbox control and set the PassChar property for the textbox to *. This hides your password for the user as they enter it.

The other correction is in the Activation sub. I changed the Cells to Range.
Sub ActivationKey()
Dim mKey As String

mKey = InputBox("Please enter your Activation Key", _
"Activation Key")

If mKey <> "" Then

If mKey = Worksheets("H00-Reference Data").Range("O4").Value Then
Worksheets("H00-Reference Data").Range("O5").Value = mKey
Exit Sub
End If
End If

MsgBox "Sorry, the activation key is incorrect!", _
vbCritical + vbOKOnly, "You are not authorized!"
ActiveWorkbook.Close False
End Sub

sunilmulay
10-21-2008, 09:19 PM
Thanks Kenneth - that worked well.
i move the following sub:
With Worksheets("Project-Nav")
.Activate
.Range("D5").Select
End With
above the Call Activation Sub. This way the user nevers sees the sheet with the codes anyway.
I'm not sure about your comment about not comparing cells O4 and O5. this is a fundamental part of my licensing strategy. cell O4 is calculated by a formula that refers to the table below. It picks the relevant key depending on what date range the Current Day lies in. So if the user enters the key for the first year, the CallActivation will not be kicked off until, the next year, when there is a mismatch between cells O4 and O5. Of course all the user has to do to get around this, is change the date on their computer to the previous year!

Also, I haven't created userforms before. I could probably figure that out, but I'm not sure how to get the Maco to call up the form???

Remember - I'm a beginner!

On this subject, i have another question. Is there an easy enough way to create a simple installation program, so that the file cannot be copied and used more than once without a separate activation key?

Thanks a bunch
S

Kenneth Hobs
10-22-2008, 05:30 AM
I guess you could poke and retrieve the password in the registry with GetSetting(), SaveSetting() and DeleteSetting(). Your way should be fine though.

You can find many PasswordChar examples if you search for passwordchar. Here is another.

msbharani
11-21-2008, 06:54 PM
Sure - attached is the simpliefied file. There is a macro which hide sheets on opening. To reveal the hdden sheet, you need to run the macro called ShowSheets and the password is password.

Sometimes the activation box seems to pop up, other times it doesn't, but it never enters the user input into the cell I want it to....

Thanks
Sunil