+ Reply to Thread
Results 1 to 12 of 12

Macro running slowly, copy/paste values only from one workbook to another

Hybrid View

  1. #1
    Registered User
    Join Date
    02-03-2011
    Location
    Reno, Nevada
    MS-Off Ver
    Excel 2007
    Posts
    4

    Macro running slowly, copy/paste values only from one workbook to another

    I created a macro that will copy all the worksheets from one workbook and save as values only into another workbook. It works, but it is a little slow. I am doing some looping and wondering if maybe I can consolidate some of the code to make the macro work more efficiently. Thank you in advance for your help.

    Please find the code here:
    Sub PoolSheetValuesOnly()
    ' Created by:   Jeanette White
    ' Last revised: 020311
    ' Purpose:      To create Pool Sheets as values only
    
         Dim ws As Worksheet
         Dim ws_Count As Integer
              
         'Loop through all of the worksheets in the active workbook
         For Each ws In Worksheets
            'count the number of worksheets
            ws_Count = Worksheets.Count
            For i = 1 To ws_Count
                'copy the worksheet name to cell D1
                ws.Cells(1, 4).Value = ws.Name
            Next i
         Next
            
        'Loop through all of the worksheets and select each worksheet
        ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
        For Each ws In Worksheets
            With ws
                If .Visible = True Then .Select Replace:=False
            End With
        Next ws
        
        'copy the data
        Cells.Select
        Selection.Copy
        
        'Open a new workbook
        Workbooks.Add
        
        'add worksheets to the new workbook based on the count of the original number of worksheets
        If ws_Count > 0 Then
            For i = -1 To ws_Count
                Sheets.Add After:=Sheets(Sheets.Count)
            Next i
        End If
        
        'make sure to be on the first workbook
        Sheets("Sheet1").Select
        Range("A1").Select
        
        'Paste special values only
        Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
        Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        
        'remove any blank worksheets
        'turn the alert off
        Application.DisplayAlerts = False
        For Each ws In Worksheets
            If Worksheets.Count > 1 Then
                If IsEmpty(ws.UsedRange) Then
                    ws.Delete
                End If
            End If
        Next ws
        'turn the alerts on
        Application.DisplayAlerts = True
        
        'loop through and name the new worksheets based on the value in the holding cell
        For Each ws In Worksheets
            ws.Name = ws.Cells(1, 4).Value
        Next
        
        'loop through and remove the worksheet name from the holding cell
        ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
        For Each ws In Worksheets
            With ws
                If .Visible = True Then .Select Replace:=False
                Range("D1").Select
                Selection.ClearContents
            End With
        Next ws
    End Sub
    Last edited by Jeanette White; 02-03-2011 at 02:35 PM. Reason: Adding code tags

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Macro running slowly, copy/paste values only from one workbook to another

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    02-03-2011
    Location
    Reno, Nevada
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Macro running slowly, copy/paste values only from one workbook to another

    I have added the code tags to my original post, my apologies, this was my first post to this forum.
    Last edited by Jeanette White; 02-03-2011 at 02:37 PM. Reason: Removing duplicated information

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Macro running slowly, copy/paste values only from one workbook to another

    Thanks for taking the time to read and comply with the rule .

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Macro running slowly, copy/paste values only from one workbook to another

    Why do you loop through the worksheets twice?

    You don't need to select, this will slow the code

    Just copy the sheets to a new workbook

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Macro running slowly, copy/paste values only from one workbook to another

    Try this
    Option Explicit
    Option Base 0
    'amended code added
    Sub PoolSheetValuesOnly()
    ' Created by:   Jeanette White
    ' Last revised: 020311
    ' Purpose:      To create Pool Sheets as values only
    
        Dim ws As Worksheet
        Dim SheetsCopy()
        Dim i As Integer
    
        ReDim SheetsCopy(0)
        'Loop through all of the worksheets in the active workbook
        For Each ws In ThisWorkbook.Worksheets
            'count the number of worksheets
            With ws
                .Cells(1, 4).Value = ws.Name
                If .Visible = xlSheetVisible Then SheetsCopy(UBound(SheetsCopy)) = ws.Name
                ReDim Preserve SheetsCopy(UBound(SheetsCopy) + 1)
            End With
        Next ws
    
        ReDim Preserve SheetsCopy(UBound(SheetsCopy) - 1)
        Sheets(SheetsCopy).Copy
        'loop through and name the new worksheets based on the value in the holding cell
        For Each ws In ActiveWorkbook.Worksheets
            ws.Name = ws.Cells(1, 4).Value
            ws.Range("D1").Clear
        Next
        'all formulas to values
        ActiveWorkbook.Worksheets.Select
        Cells.Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues
        ActiveSheet.Select
        Application.CutCopyMode = False
    
    End Sub
    Last edited by royUK; 02-03-2011 at 03:45 PM. Reason: add formulas to values

  7. #7
    Registered User
    Join Date
    02-03-2011
    Location
    Reno, Nevada
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Macro running slowly, copy/paste values only from one workbook to another

    I get an error in the following block (Red Font) when I run the code. Also, I can't see where the code is opening a new workbook to copy the worksheets too.

    ReDim Preserve SheetsCopy(UBound(SheetsCopy) - 1)
        Sheets(SheetsCopy).Copy
        'loop through and name the new worksheets based on the value in the holding cell
        For Each ws In ActiveWorkbook.Worksheets
            ws.Name = ws.Cells(1, 4).Value
            ws.Range("D1").Clear
        Next

  8. #8
    Registered User
    Join Date
    02-03-2011
    Location
    Reno, Nevada
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Macro running slowly, copy/paste values only from one workbook to another

    Moderator please mark as solved

    I set application.ScreenUpdating to false at the beginning of my code and it solved the problem.

    Sub PoolSheetValuesOnly()
    ' Created by:   Jeanette White
    ' Last revised: 021011
    ' Purpose:      To create worksheets as values only
    
        'stop screen flickering
        Application.ScreenUpdating = False
    
         'if the personal workbook is open hide it
        If Windows("PERSONAL.XLSB").Visible Then
            Windows("PERSONAL.XLSB").Visible = False
        End If
                    
        'Loop through all of the worksheets and select each worksheet
        Dim ws As Worksheet
        Dim ws_Count As Integer
        ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
        For Each ws In Worksheets
            With ws
                If .Visible = True Then .Select Replace:=False
                'count the number of worksheets
                    ws_Count = Worksheets.Count
                For i = 1 To ws_Count
                    'copy the worksheet name to cell D1
                    ws.Cells(1, 4).Value = ws.Name
                Next i
            End With
        Next ws
        
        'copy the data
        Cells.Select
        Selection.Copy
            
        'Open a new workbook
        Workbooks.Add
        
        'add worksheets to the new workbook based on the count of the original number of worksheets
        If ws_Count > 0 Then
            For i = -1 To ws_Count
                Sheets.Add After:=Sheets(Sheets.Count)
            Next i
        End If
        
        'make sure to be on the first worksheet
        Sheets("Sheet1").Select
        Range("A1").Select
        
        'Paste special values only
        Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
        Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
        
        'remove any blank worksheets
        'turn the alert off
        Application.DisplayAlerts = False
        For Each ws In Worksheets
            If Worksheets.Count > 1 Then
                If IsEmpty(ws.UsedRange) Then
                    ws.Delete
                End If
            End If
        Next ws
        'turn the alerts on
        Application.DisplayAlerts = True
        
        'loop through and name the new worksheets based on the name in the holding cell
        For Each ws In Worksheets
            ws.Name = ws.Cells(1, 4).Value
        Next
        
        'loop through and remove the worksheet name from the holding cell
        ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
        For Each ws In Worksheets
            With ws
                If .Visible = True Then .Select Replace:=False
                Range("D1").Select
                Selection.ClearContents
            End With
        Next ws
        
        'go back to orginal spreadsheet
        ActiveWindow.ActivatePrevious
        
        'loop through and remove the worksheet name from the holding cell
        ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
        For Each ws In Worksheets
            With ws
                If .Visible = True Then .Select Replace:=False
                Range("D1").Select
                Selection.ClearContents
            End With
        Next ws
        
        'go back to orginal spreadsheet
        ActiveWindow.ActivatePrevious
        Application.ScreenUpdating = True
    End Sub

  9. #9
    Registered User
    Join Date
    06-22-2009
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Macro running slowly, copy/paste values only from one workbook to another

    Hi I used this Macro and it worked fine in Excel versions prior to 2007 and for some reason there are errors popping up in excel 2007 version. Has anyone encountered the same and if yes do you know what the fix is. Any help here would be much appreciated.

    Error Description - Method 'Undo' of object'_Application' failed
    Procedure: modcopypaste.MakeUndo

    Cheers
    Prad

  10. #10
    Registered User
    Join Date
    06-22-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Macro running slowly, copy/paste values only from one workbook to another

    Hi,

    Following macro is running slowly (30 minutes). I have to copy paste several rows from each drop down box to new worksheet. However, each time calculations have to be made to get data for each drop down list member (there are about 760 members). I am sending the code below. Is there any way to make it work faster with alternative codes.

    Thanks

    Sub AllCounties()
    '
    ' AllCounties Macro
    '
    '
        For Counter = 21 To 785
            Set curCell = Sheet7.Cells(Counter, 1)
            curCell.Copy Destination:=Sheet2.Range("C2")
            Application.CutCopyMode = False
            Dim Zeile As Long
            Zeile = ((Counter - 21) * 1)
            Sheet2.Range("e81:I81").Copy
            Sheet1.Cells((Zeile + 3), 2).PasteSpecial xlPasteValues
            Sheet2.Range("e92:I92").Copy
            Sheet1.Cells((Zeile + 3), 7).PasteSpecial xlPasteValues
            Sheet2.Range("e103:I103").Copy
            Sheet1.Cells((Zeile + 3), 12).PasteSpecial xlPasteValues
            Sheet2.Range("e125:I125").Copy
            Sheet1.Cells((Zeile + 3), 17).PasteSpecial xlPasteValues
            Sheet2.Range("C2").Copy
            Sheet1.Cells((Zeile + 3), 1).PasteSpecial xlPasteValues
                       
         Next Counter
       
         Sheet2.Activate
         
         Sheet2.Range("C2").Select
        With Selection.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
            xlBetween, Formula1:="=CountiesStates"
            .IgnoreBlank = True
            .InCellDropdown = True
            .InputTitle = vbNullString
            .ErrorTitle = vbNullString
            .InputMessage = vbNullString
            .ErrorMessage = vbNullString
            .ShowInput = True
            .ShowError = True
        End With
    End Sub

  11. #11
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Macro running slowly, copy/paste values only from one workbook to another

    Kaplan275,

    Welcome to the Forum, unfortunately:

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  12. #12
    Registered User
    Join Date
    06-22-2012
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Macro running slowly, copy/paste values only from one workbook to another

    Hi Arlette,

    Thanks for your reply, I added new thread.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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