USERNAMES

mrbeanyuk

Board Regular
Joined
Nov 30, 2005
Messages
213
Does anyone know how to adapt the code below to kick you out the spreadsheet if you press enter without typing a username or cancel?

Also, the restrict the username so that it will only accept one username rather than any old text?

Thanks

Private Sub Workbook_Open()
Dim user_name As String
Dim FBR As Long 'First Blank Row

'instead of the default "", you can use Application.UserName)

user_name = Application.InputBox("name please", "title", "")
With Sheets("login")
FBR = .Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
.Cells(FBR, 2) = user_name
.Cells(FBR, 1) = Now
End With

End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
...be aware that this can be overridden by holding the shift key to skip startup macros.

Code:
Private Sub Workbook_Open()

    Dim user_name As String
    Dim FBR As Long 'First Blank Row

    user_name = Application.InputBox("name please", "title", Application.UserName)

    With Sheets("login")

    FBR = .Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    .Cells(FBR, 2) = user_name
    .Cells(FBR, 1) = Now

    If user_name <> "YOUR NAME HERE" Then
        ThisWorkbook.Saved = True
        ThisWorkbook.Close
    End If

    End With

End Sub
 
Upvote 0
Hello,

for the first part try

Code:
Private Sub Workbook_Open()
Dim user_name As String
Dim FBR As Long 'First Blank Row

'instead of the default "", you can use Application.UserName)

user_name = Application.InputBox("name please", "title", "")
If user_name = False Or user_name = "" Then
    activeworkbook.close(0)
End If
With Sheets("login")
FBR = .Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
.Cells(FBR, 2) = user_name
.Cells(FBR, 1) = Now
End With

End Sub

not sure what you mean by

Also, the restrict the username so that it will only accept one username rather than any old text?
 
Upvote 0
Drafter - This works to a point in which the pop up appears. However, i receive the error message

'Run-time error 13' 'Type mismatch'

The code being used is identical to yours above. What I still need to achieve is that the username will be 'TEST'. If this username is not inserted, then the spreadsheet closes down.

Please HELP!

Thanks
 
Upvote 0
Hello,

is this working as expected?

Code:
Private Sub Workbook_Open()
Dim user_name As String
Dim FBR As Long
    user_name = InputBox("name please", "title", "")
    If user_name <> "TEST" Then
        ActiveWorkbook.Close (0)
    End If
    With Sheets("login")
        FBR = .Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        .Cells(FBR, 2) = user_name
        .Cells(FBR, 1) = Now
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,150
Members
448,552
Latest member
WORKINGWITHNOLEADER

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