2

I have a password authentication step when anyone opens the workbook. I have multiple users who will be accessing this workbook and based on that i have assigned a variable called user to identify who amongst them is using it.

Public pwd, user As String
Public graccess As Integer

Private Sub workbook_open()
graccess = 0
Do
pwd = InputBox("Please provide your password", "Authentication")
If pwd = "access" Then
    user = "GTS"
    graccess = 1
ElseIf pwd = "enter" Then
    user = "AP"
    graccess = 1
Else
    MsgBox ("failed to authenticate")
    pwd = ""
    graccess = 0
End If
Loop Until graccess = 1
End Sub

Now when this user starts working on the excel sheet i need to check the user variable to check which user is accessing the sheet. Since this code will now come in the *worksheet_change* event it is a different sub routine which will be run after the previous code has finished running and is out of use.

so my questions are am i not able to see the user in the variable because my workbook_open event is over and the code has stopped running? what can i do to make sure the variable does not lose its value?

thanks for the patience :)

4

2 に答える 2

0

2つのこと:

  1. DIMを修正する

    Dim pwd を文字列、user を文字列として

  2. DIM を標準モジュールに移動する

于 2013-09-06T13:32:12.383 に答える
0

Nameワークブックの変数に現在のユーザー名を格納します。

まず、Nameワークブックに a を追加します。リボンから、次のFormulas操作を行います。

ここに画像の説明を入力

これは基本的に、実行後も持続する「変数」です。

Workbook_Openイベント ハンドラーで、次のようにします。

ActiveWorkbook.Names("username") = Environ("username") 

これは、「username」と呼ばれるシステム環境変数を使用します。これは、誤ってまたは悪意を持って間違った名前を入力する機会がないため、ユーザー名を要求するよりもおそらく望ましい方法です。

または、 を使用しInputBoxてユーザー名を要求することもできます。

ActiveWorkbook.Names("username") = Application.InputBox("Please enter your username", "Username", "enter your username")

Nameまたは、を文字列値として定義するその他のメソッド。

後で、コード モジュールまたはイベント ハンドラーのいずれかで、いつでも を参照して、Nameその方法で検証を行うことができます。

于 2013-09-06T13:47:21.680 に答える