And if you do this a second time, the line that tries to rename the newly copied
sheet as Temp will fail. If you hit the End option on the Debug dialog, you'll
lose the contents of the variables.
And it looks like you're not showing all the code. It looks to me that you have
an "On error resume next" line that avoids the rename error. Maybe something in
the code you're not sharing is the problem.
Sorry, I did not post all the code:
Here is the complete one:
Private Sub b_Copy_Click()
Dim i_Sheet As Integer
Dim i_ExistSheet As Integer
i_Sheet = 1
i_ExistSheet = 0
If ActiveSheet.Name = "Data Entry" Then
Do While i_Sheet <= Sheets.Count
If Sheets(i_Sheet).Name = "Temp" Then
i_ExistSheet = i_Sheet
End If
i_Sheet = i_Sheet + 1
Loop
If i_ExistSheet > 0 Then
If MsgBox("The existing Temp sheet will be deleted, before the
copy.", vbOKCancel) = vbOK Then
Application.DisplayAlerts = False
Sheets(i_ExistSheet).Delete
i_ExistSheet = 0
Application.DisplayAlerts = True
End If
End If
If i_ExistSheet = 0 Then
ActiveSheet.Copy After:=Sheets("Legend")
ActiveSheet.Name = "Temp"
If ActiveSheet.Name = "Temp" Then
ActiveSheet.Unprotect Password:=c_Password
ActiveSheet.OLEObjects("b_Copy").Delete
ActiveSheet.OLEObjects("Copy_Prev_Row").Delete
MsgBox ("Delete this temporary sheet after finishing working
with it.")
End If
End If
End If
End Sub