Greetings,
I consider myself a novice at VBA and am looking for some comments and suggestions.
I am creating a workbook that I want to restrict to using on only two computers. I want to register the two computers on first use and when opening the workbook to check to see if the computer has already been registered. If the computer is eligible, then register it. If the number of computers is already = 2, then deny access to the workbook.
I have written up some simple code that checks registration and auto registers the computer if it is eligible. If the new computer is already registered, then the VBA allows access and if the new computer is not eligible for either registration or access, it closes the workbook.
Now, I know that a person with VBA skills can get in and disable this code, but for most users of the workbook, they will not have that level of skill ... at least I doubt they will.
Here is the code that I wrote and would appreciate any comments or suggestions. Right now the code is in its own module, but when final, I plan to insert the code into the Private Sub Workbook_Open module.
Thanks for any assistance.
Steve
I consider myself a novice at VBA and am looking for some comments and suggestions.
I am creating a workbook that I want to restrict to using on only two computers. I want to register the two computers on first use and when opening the workbook to check to see if the computer has already been registered. If the computer is eligible, then register it. If the number of computers is already = 2, then deny access to the workbook.
I have written up some simple code that checks registration and auto registers the computer if it is eligible. If the new computer is already registered, then the VBA allows access and if the new computer is not eligible for either registration or access, it closes the workbook.
Now, I know that a person with VBA skills can get in and disable this code, but for most users of the workbook, they will not have that level of skill ... at least I doubt they will.
Here is the code that I wrote and would appreciate any comments or suggestions. Right now the code is in its own module, but when final, I plan to insert the code into the Private Sub Workbook_Open module.
Code:
Sub validatewb()
Dim scompone As String
Dim scomptwo As String
Dim shostname As String
On Error Resume Next
'Get computer name
shostname = Environ$("computername")
'get registered names
scompone = Sheet5.Range("scomp1").Value
scomptwo = Sheet5.Range("scomp2").Value
' Check if computer is already registered
If shostname = scompone Then
GoTo oktogo
End If
If shostname = scomptwo Then
GoTo oktogo
End If
' At this point, then hostname has not yet been registered
' Check if maximum number of computers have been registered
If Sheet5.Range("scomp_number").Value = 2 Then ' If already 2, then maximum allowed
MsgBox "Computer not elible" 'computer is not eligable
' VBA code here to close the workbook
Exit Sub
Else
'computer is elible, register it
If scompone = "" Then
Sheet5.Range("scomp1").Value = shostname
Else
Sheet5.Range("scomp2").Value = shostname
End If
MsgBox "New computer registered"
End If
oktogo:
'at this point continue with normal operation of workbook
End Sub
Thanks for any assistance.
Steve