Results 1 to 6 of 6

Thread: Copy worksheet problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    8

    Copy worksheet problem

    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.

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Copy worksheet problem

    Does XlApp refer to the application or to an individual workbook? If it is the Application, then you will need to specify a workbook.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Copy worksheet problem

    Here's how I would do it.
    BTW, I'm not sure why you have this coded as a Function rather than a Sub, as it doesn't have a return value.
    VB Code:
    1. Public Sub AddSheetCopy(CopyName As String, NewName As String)
    2. Dim wksSheet As Worksheet
    3. Dim bExists As Boolean
    4.    
    5.     'step 1 Check if the sheet exists
    6.     For Each wksSheet In ThisWorkbook.Worksheets
    7.         If UCase(wksSheet.Name) = UCase(CopyName) Then
    8.             bExists = True
    9.             Exit For
    10.         End If
    11.     Next wksSheet
    12.    
    13.     'step 2 If it doesn't exist then exit sub
    14.     If Not bExists Then Exit Sub
    15.    
    16.     'step 3 Copy the sheet
    17.     ThisWorkbook.Worksheets(CopyName).Copy after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
    18.     Set wksSheet = ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
    19.     wksSheet.Name = NewName
    20.    
    21.     Set wksSheet = Nothing
    22. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Copy worksheet problem

    In the above example I'm using ThisWorkbook, you will probably need to change that.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Copy worksheet problem

    I've found that Excel treats me a lot better when I use explicit references instead of using the Active* objects and the Application collections. Try setting the Workbook explicitly:
    VB Code:
    1. Public Function AddSheetCopy(CopyName As String, NewName As String)
    2.  
    3.     Dim xlBook As Excel.Workbook, xlSource As Excel.Worksheet
    4.  
    5.     Set xlBook = xlApp.ActiveWorkbook                           'Get the current book.
    6.    
    7.     For Each xlSheet In xlBook.Worksheets                       'Make sure the NewName doesn't exist.
    8.         If xlSheet.Name = NewName Then
    9.             Debug.Print ("sheet exists")
    10.             Exit Function
    11.         End If
    12.     Next xlSheet
    13.    
    14.     Set xlSource = xlBook.Worksheets(CopyName)                  'Get a ref to the source sheet.
    15.     Call xlSource.Copy(, xlBook.Worksheets(xlBook.Worksheets.Count))    'Copy it.
    16.    
    17.     Set xlSheet = xlBook.Worksheets(xlBook.Worksheets.Count)    'Get a ref to the new copy.
    18.     xlSheet.Name = NewName                                      'Name it.
    19.    
    20.     Set xlBook = Nothing
    21.     Set xlSource = Nothing
    22.  
    23. End Function
    Also, instead of just exiting if the CopyName is a duplicate, you can use this function to generate a garunteed unique name. It should always allow a rename to succeed if passed through it. You use something other than V.#, like Copy # of [Sheet], but the principle would be the same.
    VB Code:
    1. Public Function UniqueName(xlBook As Excel.Workbook, sName As String) As String
    2.  
    3.     Dim sTest As String, lCount As Long, lNum As Long, lTop As Long, bDuped As Boolean
    4.    
    5.     lTop = xlBook.Worksheets.Count                  'Get the number of sheets in the book.
    6.     If lTop = 1 Then                                'One sheet, can't be a duplicate.
    7.         UniqueName = sName                          'Return it.
    8.         Exit Function
    9.     End If
    10.    
    11.     sTest = sName                                   'Set the testing name to the passed arg.
    12.     Do
    13.         bDuped = False                              'Reset the duplicated flag.
    14.         For lCount = 1 To lTop                      'Test the name against all sheets.
    15.             If xlBook.Worksheets(lCount).Name = sTest Then
    16.                 bDuped = True                       'If it's found, set the flag.
    17.             End If
    18.         Next lCount
    19.         If bDuped = True Then
    20.             sTest = sName & " V." & CStr(lNum + 2)  'Try V.#
    21.             lNum = lNum + 1                         'Increment the number counter.
    22.         End If
    23.     Loop Until bDuped = False                       'Loop until it's unique.
    24.  
    25.     UniqueName = sTest
    26.  
    27. End Function
    It's called like this:
    VB Code:
    1. xlSheet.Name = UniqueName(xlSheet.Parent, NewName)

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    8

    Re: Copy worksheet problem

    Hats of to you, you really know your stuff. Thankyou I have it working now thanks to your example.

    I am really more of a C kinda guy, never really coded any VB before the last few days. At the moment I am learning from the examples spread over the web, most of which I have found to be quite confusing.

    Thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width