USERNAMES

mrbeanyuk

Board Regular
Joined
Nov 30, 2005
Messages
212
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
 

Some videos you may like

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

rjp

Board Regular
Joined
Jan 5, 2006
Messages
148
...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
 

onlyadrafter

Well-known Member
Joined
Aug 19, 2003
Messages
5,703
Platform
  1. Windows
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?
 

mrbeanyuk

Board Regular
Joined
Nov 30, 2005
Messages
212
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
 

mrbeanyuk

Board Regular
Joined
Nov 30, 2005
Messages
212
VBA

Debug picks up this as an error:

If user_name = False Or user_name = "" Then
 

onlyadrafter

Well-known Member
Joined
Aug 19, 2003
Messages
5,703
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,114,675
Messages
5,549,363
Members
410,911
Latest member
AniEx
Top