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
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,168,128
Messages
5,857,531
Members
431,884
Latest member
Gcmoore63

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
Top