+ Reply to Thread
Results 1 to 7 of 7

Excel crashes with Worksheets().copy

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-17-2004
    MS-Off Ver
    Office 2016
    Posts
    527

    Excel crashes with Worksheets().copy

    On Windows the following code segment works fine however on a Mac, the Worksheets("worksheet").Copy command causes Excel to crash. I've see suggestions where using Worksheets("worksheet").UsedRange.Copy solves the problem, and in fact, the Mac does not crash, however I need the copied data in a new/separate worksheet just as the .copy command does. Using the .UsedRange part does not create the new workbook. The difficulty is that without creating the new workbook, the command following the copy shuts down the worksheet that the vba code is running from instead of closing the copied worksheet.

    Any suggestions on code that could copy the entire worksheet, or even the cells with data, into a new worksheet that then becomes the active workbook?

    Worksheets("Launch Map").Copy
    
    ActiveWorkbook.FollowHyperlink _
         Address:=strFName, _
          NewWindow:=True

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Excel crashes with Worksheets().copy

    I do not know about MACs but this code is a sample found that after editting will work in Excel

    Sub SaveAs()
     
        Dim FName           As String
        Dim FPath           As String
        Dim NewBook         As Workbook
     
        FPath = "G:\Exceptions"
        FName = "Exceptions" & Format(Date, "ddmmyy") & ".xls"
     
        Set NewBook = Workbooks.Add
     
        ThisWorkbook.Sheets("DataSort").Copy Before:=NewBook.Sheets(1)
     
        If Dir(FPath & "\" & FName) <> "" Then
            MsgBox "File " & FPath & "\" & FName & " already exists"
        Else
            NewBook.SaveAs Filename:=FPath & "\" & FName
        End If
     
    End Sub
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Registered User
    Join Date
    12-10-2010
    Location
    California
    MS-Off Ver
    Excel all versions from 5.0 (1993) to 365
    Posts
    38

    Re: Excel crashes with Worksheets().copy

    Quote Originally Posted by Keebellah View Post
    I do not know about MACs but this code is a sample found that after editting will work in Excel

    Sub SaveAs()....
    etc (see comment above)
    i'm unclear how this code enables to copy a sheet to an existing open workbook.

  4. #4
    Forum Contributor
    Join Date
    11-17-2004
    MS-Off Ver
    Office 2016
    Posts
    527

    Re: Excel crashes with Worksheets().copy

    Thanks, I took the few lines from this example that helped and incorporated them into my code. It's too bad that it now takes two lines to perform the same as one should.

  5. #5
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Excel crashes with Worksheets().copy

    Can you show me that part of the code?
    It probably can be done in one line, but since I cannot see what you wrote, I can't tell

  6. #6
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Excel crashes with Worksheets().copy

    In that case instead of workbooks add you create a variable that revers to an opened workbook (or in not open, opens it) en then with some minor editing you can use the same code.
    But, does running the code now, create a new workbook with a copy ad the sheet?

  7. #7
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Excel crashes with Worksheets().copy

    These to macros might work

    Public Sub SaveCopy2NewBook()
        Dim FName           As String
        Dim FPath           As String
        Dim NewBook         As Workbook
        FPath = "G:\Exceptions"
        FName = "Exceptions" & Format(Date, "ddmmyy") & ".xls"
        Set NewBook = Workbooks.Add
        ThisWorkbook.Sheets("DataSort").Copy Before:=NewBook.Sheets(1)
        If Dir(FPath & "\" & FName) <> "" Then
            MsgBox "File " & FPath & "\" & FName & " already exists"
        Else
            NewBook.SaveAs Filename:=FPath & "\" & FName
        End If
    End Sub
    
    Public Sub SaveCopy2WorkBook(WB As Workbook)
        Dim FName           As String
        Dim FPath           As String
        FPath = WB.Path
        On Error GoTo errorFound
        ThisWorkbook.Sheets("DataSort").Copy Before:=WB.Sheets(1)
        GoTo exitSub
    
    errorFound:
        MsgBox "Error: " & Err.Description, vbCritical, "Error Number: " & Err.Number
    
    exitSub:
    Err.Clear
    On Error GoTo 0
    End Sub
    The first one is the one that just creates a new workbook and does it

    The second one expects and open workbook and that you pass the workbook as parameter


    Your own code whatever you have

    and the syntax is

    Call SaveCopy2WorkBook(the workbook)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copy / Paste / Manipulate Form Option Buttons Crashes Excel
    By Steveapa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-12-2016, 12:21 PM
  2. Replies: 4
    Last Post: 10-21-2013, 02:55 PM
  3. [SOLVED] Macro crashes my excel and does not copy the rows
    By AB33 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-29-2012, 06:43 PM
  4. Excel crashes everytime I copy sheet to a new workbook
    By este994 in forum Excel General
    Replies: 0
    Last Post: 03-06-2012, 12:42 PM
  5. [SOLVED] Excel crashes when copy
    By Jeff in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-23-2006, 06:10 PM
  6. [SOLVED] Macro To Printout Worksheets Crashes
    By Martin Fishlock in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-29-2005, 12:35 PM
  7. [SOLVED] Copy row, Insert>Copied>Cells crashes Excel
    By Kent in forum Excel General
    Replies: 0
    Last Post: 04-26-2005, 06:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1