excelvba

Problem with ActiveSheet.Paste Link:=True


I have a number of files with raw data. I have a macro that goes through the selected files in a loop and put the data of each file onto one sheet in xlsm template for further calculations and evaluations. That means, I end up with a xlsm file with corresponding Worksheet for each raw data file. The macro also creates a Result file (xlsx, from provided Results template) that contains 1 row of resuls and 1 graph from each Worksheet in xlsm file. I need each row of data and each line in the graph linked back to the original Worksheet in the xlsm file, so changes made in the xlsm file translate to the Resuls file.

Currently, my code looks like this:

Sub AssembleResults()
'// Subroutine goes through every list in Workbook and copies row of results and graph to Resuls file

    Dim SingleSheet As Worksheet
    Dim wksSource As Worksheet, 
    Dim wksDest As Worksheet    
    Dim rngSource, rngDest As Range            
    Dim chrtSource As ChartObject, chrtDest As Chart          
    
    '// Open Results template
    Application.DisplayAlerts = True   
    Workbooks.Open FileName:=XltResults, Editable:=True
    Set wbResults = ActiveWorkbook
    Application.DisplayAlerts = False
    
    For Each SingleSheet In wbTemplate.Worksheets      
        '//wbTemplate is berofe defined and used xlsm file with Worksheets

        Set wksSource = wbTemplate.Worksheets(SingleSheet.Name)
        Set rngSource = wksSource.Range("A3:L3")
        Set chrtSource = wksSource.ChartObjects(2)
            
        wbResults.Worksheets("Results").Activate
        Set wksDest = ActiveSheet
        Set rngDest = wksDest.Range(Range("A1").End(xlDown).Offset(-1,0),Range("L1").End(xlDown).Offset(-1,0))
        
        Set chrtDest = wbResults.Charts(1)
            
        '//Copying row of results
        rngSource.Copy
        wbResults.Activate   
        wksDest.Activate     
        rngDest.Select
        ActiveSheet.Paste Link:=True          '//HERE IS THE PROBLEM
        Application.CutCopyMode = False
          
        '//Copying lines of graph into single graph
        chrtSource.Activate
        chrtSource.Copy  
        wbResults.Activate
        chrtDest.Select
        chrtDest.Paste    
        Application.CutCopyMode = False
        
        '// Cleaning the variables
        Set wksSource = Nothing
        Set wksDest = Nothing
        Set rngSource = Nothing
        Set rngDest = Nothing
        Set chrtSource = Nothing
        Set chrtDest = Nothing

End Sub

Randomly, on indicated line (while trying to paste the linked row), macro will throw: Run-time Error'1004': No Link to Paste However, if you step into debug mode and just press 'F5'/Run, it will run again without problem for random number of loops.

It's truly random. Some batches of data, macro runs without error. Other, errors occurs three times in a batch. If run more times on the same batch of data, it can run without error or randomly stop in any loop. There is no pattern I can see. I would appriciate any help.


Solution

  • I didn't get an answer here, but I managed to figure out solution on my own. So, for anyone interested, here it is.

    Seems the problem is with copying anything to clipboard, especially, if user clicks anywhere while macro is running. To avoid this I swiched from copy/paste to assigning strings into formulas for cells in Row of Results and into NewSeries properties for Graph. Final code:

    Sub AssembleResults()
    '// Subroutine goes through every list in Workbook and copies row of results and graph to Resuls file
    
        Dim SingleSheet As Worksheet
        Dim wksDest As Worksheet    
        Dim rngDest As Range            
        Dim chrtDest As Chart
        Dim cel As Range     
        
        For Each SingleSheet In wbTemplate.Worksheets      
            '//wbTemplate is berofe defined and used xlsm file with Worksheets
            '//wbResults is berofe defined and opened xltx file
    
            Set wksDest = wbResults.Worksheets(1)
            Set rngDest = wksDest.Range(Range("A1").End(xlDown).Offset(-1,0),Range("L1").End(xlDown).Offset(-1,0))
            Set chrtDest = wbResults.Charts(1)
                
            '//Linking row of results
            For Each cel In rngDest
                cel.Formula = "='" & wbTemplate.Path & "\[" & wbTemplate.Name & "]" & SingleSheet.Name & "'!" & Cells(3, cel.Column).Address
            Next cel
           
            '//Copying lines of graph into single graph
                 '// RadekStart and
             With chrtDest.SeriesCollection.NewSeries
                .Name = "='[" & wbTemplate.Name & "]" & wshTemplate.Name & "'!$A$3"
                .Values = "='[" & wbTemplate.Name & "]" & wshTemplate.Name & "'!" & Range(Range("AB3"),Range("AB3").End(xlDown)).Address
                .XValues = "='[" & wbTemplate.Name & "]" & wshTemplate.Name & "'!" & Range(Range("AA3"),Range("AA3").End(xlDown)).Address
             End With
    
        Next SingleSheet
            
            '// Cleaning the variables
            Set wksDest = Nothing
            Set rngDest = Nothing
            Set chrtDest = Nothing
    
    End Sub
    

    Hope it helps.