Msgbox based on Msgbox

surkdidat

Well-known Member
Joined
Oct 1, 2011
Messages
594
Office Version
  1. 365
Is it possible to have a messagebox on pop up with text entry, i.e.on opening the spreadsheet it asks the user to enter their name. When they press ok, then depending on that was entered a specific message box appears, ie

Opening MsgBox - "Please Enter Your Name..."

(User enters "surkdidat")

Msgbox 2 appears "Hello surkdidat... Please work on sheet 2"
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Code:
Private Sub Workbook_Open()
    Dim LogName As String, MsgStr As String
    LogName = InputBox("Please enter your name.", "Name required...")
    Select Case LogName
        Case Is = "surkdidat"
            MsgStr = "Hello surkdidat... Please work on sheet 2"
        'etc etc
    End Select
    MsgBox MsgStr, vbInformation, "Have a nice day..."
End Sub
 
Upvote 0
Excellent, thank you for this.
Sorry to be a pain, but is there an option if the name is not on the list, it states "unauthorised access", and gives one more opportunity to put their name in correctly, and if still not on the list brings up a message "sorry, too many attempts" and then closes?

Code:
Private Sub Workbook_Open()
    Dim LogName As String, MsgStr As String
    LogName = InputBox("Please enter your name.", "Name required...")
    Select Case LogName
        Case Is = "surkdidat"
            MsgStr = "Hello surkdidat... Please work on sheet 2"
        'etc etc
    End Select
    MsgBox MsgStr, vbInformation, "Have a nice day..."
End Sub
 
Upvote 0
Another way to do this without user Select Case for each username: (Create a named range of all of your users and name it "UserNames")

Code:
Private Sub Workbook_Open()    Dim LogName, response As Integer
    Dim UserName As Range
    Do
        response = 0
        LogName = Application.InputBox("Please enter your name.", "Name required...")
        If LogName = False Then ThisWorkbook.Close
        Set UserName = Range("UserNames").Find(LogName, lookat:=xlWhole)
        If UserName Is Nothing Then
            response = MsgBox("You did not enter a valid username. Do you wish to continue?", _
            vbRetryCancel + vbCritical, "Invalid Username")
        End If
        If response = 2 Then ThisWorkbook.Close
    Loop Until response <> 4
    
    MsgBox "Hello " & UserName & "... Please work on sheet 2"
    Worksheets(2).Activate
End Sub

Regards,

CJ
 
Upvote 0
I went the select case route because of

depending on that was entered a specific message box appears

Your message box only changes the user name, not the direction.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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