I have a macro, code below, which fires when the workbook is opened. It checks to see if the username is written in column A of Sheet2; if it is, it writes the date beside it. If it isn't, it writes the username in the next open cell in column A, puts the date beside it, saves itself, then displays a timed messag of my choosing.
Right now I can put the UserName (StrUser) in my message, but I want to personalize it more by putting the actual name (User) in the message, and I'm trying it with a Select Case routine, but it's not working. I always plugs in "I don't know who you are", though I've verified my StrUser name is correct. I'd appreciate it if anyone can point out my problem in the code below. BTW, the "Sheet1.Select" statements are because I want my users to only be looking at sheet1.
Thanks.
Option Explicit
Private Sub Workbook_Open()
Dim strUser As String, lrow As Long
strUser = Environ("username")
Dim cell As Range
Dim User As String
Application.ScreenUpdating = False
Sheet1.Select
' check to see if the file was opened in readonly mode, if so exit sub
If Me.ReadOnly = True Then Exit Sub
' can write and save changes - not read only
With Sheets("Sheet2")
' can write and save changes - not read only
For Each cell In .UsedRange
If cell.Value = strUser Then
cell.Offset(0, 1).Value = Date
Exit Sub ' username is present
End If
Next cell
With .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
.Value = strUser
.Offset(0, 1).Value = Date
End With
Select Case Cells(Rows.Count, 1).End(xlUp).Text
Case "LUPOREJ"
User = "John"
'Case "IDUNNO"
'User = "Somebody else"
Case Else
User = "I don't know who you are."
End Select
End With
Me.Save 'save now to capture username in log
Sheet1.Select
CreateObject("WScript.Shell").Popup "Hello" & User & " This is a first attempt at a messaging system within Excel for specific worksheets." & vbCrLf & "This workbook will close itself after 7 seconds, which should be enough time to read this message." & vbCrLf & "If I've done it right you should not see this msgbox again. Thanks for trying it.", 7, "This Msgbox will close itself."
'Run "Msgbox_Test()"
Application.ScreenUpdating = True
End Sub
Bookmarks