+ Reply to Thread
Results 1 to 19 of 19

Code running slowly

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-05-2010
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    747

    Code running slowly

    I have the following code that is running slowly. Is there another way to speed up the copy and paste function that it is performing? The copy range should always start in cell B3 and the last row of the copy range will vary every time I run the code. Any help would be appreciated.


    Sub CopyLiveData()
    
    Windows("ANR for multiple tickers.xls").Activate
        Range("B3:Q5999").Select
        Selection.Copy
    
    Windows("ANR for multiple tickers 2.xls").Activate
        Range("B1").Select
    
        Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
        Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
    
    End SUb
    Last edited by rhudgins; 02-01-2011 at 11:09 AM.

  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: Code running slowly

    You don't need to select. There's 100s of examples of copying data correctly on the forum
    Hope that helps.

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

    Free DataBaseForm example

  3. #3
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Code running slowly

    Try

    Sub CopyLiveData()
    Windows("ANR for multiple tickers.xls").Activate
    Range("B3").CurrentRegion.Copy
    Windows("ANR for multiple tickers 2.xls").Range("B1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
            xlNone, SkipBlanks:=False, Transpose:=False
    Windows("ANR for multiple tickers 2.xls").Range("B1").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
    End Sub
    Martin

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Code running slowly

    Don't use copy to copy:

    Sub CopyLiveData()
      With Workbooks("ANR for multiple tickers.xls").Range("B3:Q5999")
        Workbooks("ANR for multiple tickers 2.xls").Range("B1").resize(.rows.count,.columns.count)=.value
      End With
    End SUb



  5. #5
    Forum Contributor
    Join Date
    01-05-2010
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    747

    Re: Code running slowly

    There are blank cells in the code range so currentregion.copy does not work. Any other ideas?

  6. #6
    Forum Contributor
    Join Date
    01-05-2010
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    747

    Re: Code running slowly

    SNB - The range B3:Q5999 will sometimes be larger and other times smaller. (ex:B3:Q3000, B3:Q10000). Is there a way to change the code to capture this?

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Code running slowly

    Sub CopyLiveData()
      With Workbooks("ANR for multiple tickers.xls").sheets(1).Range([B3],Workbooks("ANR for multiple tickers.xls").sheets(1).cells.specialcells(xlcelltypelastcell))
        Workbooks("ANR for multiple tickers 2.xls").sheets(1).Range("B1").resize(.rows.count,.columns.count)=.value
      End With
    End SUb
    The more information you give the more specific the answer can be.
    Blank cells do not jeopardize currentregion, empty rows/columns do.
    Last edited by snb; 01-31-2011 at 05:25 PM.

  8. #8
    Forum Contributor
    Join Date
    01-05-2010
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    747

    Re: Code running slowly

    I am getting a syntax error on the first line..

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Code running slowly

    Me too.
    I think you need to scrutinize the code to improve it.

  10. #10
    Forum Contributor
    Join Date
    01-05-2010
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    747

    Re: Code running slowly

    I really don't know how to fix it. The colon and [] are confusing me.

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Code running slowly

    Maybe your VBEditor has some help-facility ?

  12. #12
    Forum Contributor
    Join Date
    01-05-2010
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    747

    Re: Code running slowly

    Please show me how to fix it so that I will know for the next time.

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Code running slowly

    What determines the last cell to be copied? Is it the last used cell in col Q?

    What's the worksheet name in each workbook?
    Entia non sunt multiplicanda sine necessitate

  14. #14
    Forum Contributor
    Join Date
    01-05-2010
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    747

    Re: Code running slowly

    The last cell to be copied is the last used cell in column Q.

    The worksheet name is called "AnalystRecommendations" in workbook "ANR for multiple tickers" and "Today" in workbook ANR for multiple tickers 2.

  15. #15
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Code running slowly

    If you declare object variables, you get IntelliSense so you're not coding blind.
    Sub CopyLiveData()
        Dim rCopy As Range
        Dim wksFr As Worksheet
        Dim wksTo As Worksheet
        
        Set wksFr = Workbooks("ANR for multiple tickers.xls").Worksheets("AnalystRecommendations")
        Set wksTo = Workbooks("ANR for multiple tickers 2.xls").Worksheets("Today")
        
        With wksFr
            Set rCopy = .Range("B3", .Cells(.Rows.Count, "Q").End(xlUp))
        End With
        If rCopy.Row < 3 Then Exit Sub  ' nothing to copy ...
            
        rCopy.Copy
        With wksTo.Range("B1")
            .PasteSpecial Paste:=xlPasteValues
            .PasteSpecial Paste:=xlPasteFormats
        End With
    End Sub

  16. #16
    Forum Contributor
    Join Date
    01-05-2010
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    747

    Re: Code running slowly

    Thanks so much. I recieved one error at the line .PasteSpecial Paste:=xlPasteValues "This operation requires the merged cells to be identically sized."

    Can you help me fix this?

  17. #17
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Code running slowly

    Unmerge all merged cells, the scourge of Excel.

    I have a moral aversion to solving any problems related to merged cells.
    Last edited by shg; 02-01-2011 at 12:04 PM.

  18. #18
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Code running slowly

    Please show me how to fix it so that I will know for the next time.
    I did. Read the posted code again.

  19. #19
    Forum Contributor
    Join Date
    01-05-2010
    Location
    New York
    MS-Off Ver
    Excel 2016
    Posts
    747

    Re: Code running slowly

    Thanks this works

+ 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