Note that there are some explanatory texts on larger screens.

plurals
  1. POpastespecial Crashing when pasting excel data
    primarykey
    data
    text
    <p>I am using the following code to get data from several excel files and paste them into a single new file:</p> <pre><code> For i = 0 To amountOfFiles - 1 Dim xlAppSource As New Excel.Application Dim xlAppTarget As New Excel.Application Dim xlWbSource As Excel.Workbook Dim xlWbTarget As Excel.Workbook Dim xlsheetSource As Excel.Worksheet Dim xlsheetTarget As Excel.Worksheet Dim CurrentFile As String = strFileNames(i) Dim IntAmountOfRows As Integer = amountOfRows(CurrentFile) Dim intStartOfEmptyRow As Int16 = amountOfRows(SummaryLocation) 'Set current workbook xlWbSource = xlAppSource.Workbooks.Open(CurrentFile) xlWbTarget = xlAppTarget.Workbooks.Open(SummaryLocation) 'set current worksheet xlsheetSource = xlWbSource.ActiveSheet xlsheetTarget = xlWbTarget.ActiveSheet 'copy range of data from source to target file xlsheetSource.Range("A2:k" &amp; IntAmountOfRows).Copy() xlsheetTarget.Range("A2:k" &amp; intStartOfEmptyRow).PasteSpecial(Excel.XlPasteType.xlPasteAll, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, False, False) 'Set focus on Summary ExcelSheet xlWbTarget = xlAppSource.Workbooks.Open(SummaryLocation) xlsheetSource = xlWbTarget.ActiveSheet 'close excel xlWbSource.Close(True) xlWbTarget.Close(True) xlAppSource.Quit() xlAppTarget.Quit() 'Cleanup xlAppSource = Nothing xlAppTarget = Nothing xlWbSource = Nothing xlWbTarget = Nothing xlsheetSource = Nothing xlsheetTarget = Nothing Next </code></pre> <p>However, when I execute the code, it throws the following error:</p> <p><img src="https://i.stack.imgur.com/x13fY.png" alt="ComException"></p> <p><strong>"Method PasteSpecial of Class Range has failed"</strong> </p> <p>Wich points to the line:</p> <pre><code>xlsheetTarget.Range("A2:k" &amp; intStartOfEmptyRow).PasteSpecial(Excel.XlPasteType.xlPasteAll, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, False, False) </code></pre> <p>I have no idea how to solve this, seeing as its a pretty general Error, googling it gives me next to none answers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload