Can anyone suggest why the following code doesn't work? I want to search for a worksheet with the "username_temp" and if it doesn't exist then create a new worksheet.
It works sometimes, but when I've used it once, then logout and login at another terminal it tries to create a new sheet and then obviously can't rename it "username_temp" because that worksheet already exists.
I hope you can help!!
Thanks in advance.
Private Sub Workbook_Open()
Dim CurrentUser
CurrentUser = Application.UserName
TempName = CurrentUser & "_Temp"
For Each ws In Worksheets
If ws.Name = TempName Then
Worksheets(TempName).Activate
Exit For
Else
Worksheets.Add
With ActiveSheet
.Name = TempName
.Cells(5, 3).Value = "Current User:"
End With
End If
Next ws
End Sub
It works sometimes, but when I've used it once, then logout and login at another terminal it tries to create a new sheet and then obviously can't rename it "username_temp" because that worksheet already exists.
I hope you can help!!
Thanks in advance.
Private Sub Workbook_Open()
Dim CurrentUser
CurrentUser = Application.UserName
TempName = CurrentUser & "_Temp"
For Each ws In Worksheets
If ws.Name = TempName Then
Worksheets(TempName).Activate
Exit For
Else
Worksheets.Add
With ActiveSheet
.Name = TempName
.Cells(5, 3).Value = "Current User:"
End With
End If
Next ws
End Sub