I have a simple function contained within an AciveX dll. Basically I want to copy an existing worksheet to the same workbook, and change the name. Here is the function
VB Code:
  1. Public Function AddSheetCopy(CopyName As String, NewName As String)
  2.     For Each xlSheet In xlApp.worksheets
  3.         If xlSheet.Name = NewName Then
  4.             MsgBox ("sheet exists")
  5.             Exit Function
  6.         End If
  7.     Next xlSheet
  8.     MsgBox (xlApp.worksheets(1).Name)
  9.     xlApp.worksheets(CopyName).Copy after:=xlApp.worksheets(xlApp.worksheets.Count)
  10.     xlApp.activesheet.Name = NewName
  11.     Set xlSheet = xlApp.activesheet
  12. End Function

The above function does exactly as I intend it to do, with one problem. Above there is a message box which displays the name of the first sheet, I don't really want to be plagued with this every time I call the function (I placed it there for debugging). If I comment out that line, the function fails. Instead of making a copy of "copyname" it makes a copy of the current active sheet. I have tries just about everything I can think of to make it work, yet it still doesn't.

Any ideas? thanks in advance.