Copy Selected Rows to a Closed Excel Workbook with VBA
Question details
Copy selected rows from source worksheets into an Excel workbook that is initially closed.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- The user wants to copy rows between the Open Items and Open Items End markers on each source worksheet to a destination workbook.
- Observed behavior
- Ordinary VBA Range assignments cannot write directly to a closed workbook; the destination must be opened or updated through another suitable data-access method.
Confirm the destination file path and worksheet name, and make sure the destination workbook is closed and writable. The example expects one pair of exact markers in column A of each relevant source sheet and copies columns A through Z.
Open the destination workbook in the background and copy values
Use this approach when the destination is initially closed and you want to append the rows between markers from each source worksheet.
The macro opens the destination in a separate hidden Excel instance, appends values, saves the workbook, and closes that instance. It uses explicit workbook and worksheet references rather than ActiveSheet.
Sheets without either marker are skipped. If a sheet has only one marker or the end marker appears first, the macro stops without saving the destination. Adapt the column range and marker search if your layout differs.
Open the source workbook in desktop Excel, press Alt+F11, and select Insert > Module. Save the source as a macro-enabled workbook if it needs to retain the code.
Paste the code below into the module. Replace the destination path and the sheet name "That" with your own values. Sub CopyOpenItems() Const DEST_PATH As String = "C:\Path\Destination.xlsx" Const DEST_SHEET As String = "That" Dim destApp As Object Dim destBook As Object Dim destSheet As Object Dim ws As Worksheet Dim startCell As Range, endCell As Range Dim lastCell As Object Dim nextRow As Long, rowCount As Long Dim errorMessage As String On Error GoTo Failed Set destApp = CreateObject("Excel.Application") destApp.Visible = False destApp.DisplayAlerts = False Set destBook = destApp.Workbooks.Open(DEST_PATH, UpdateLinks:=0, ReadOnly:=False) If destBook.ReadOnly Then Err.Raise vbObjectError + 1, , "Destination is read-only." Set destSheet = destBook.Worksheets(DEST_SHEET) Set lastCell = destSheet.Cells.Find(What:="*", After:=destSheet.Cells(1, 1), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False) If lastCell Is Nothing Then nextRow = 1 Else nextRow = lastCell.Row + 1 End If For Each ws In ThisWorkbook.Worksheets Set startCell = ws.Columns("A").Find(What:="Open Items", After:=ws.Cells(ws.Rows.Count, 1), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) Set endCell = ws.Columns("A").Find(What:="Open Items End", After:=ws.Cells(ws.Rows.Count, 1), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) If Not startCell Is Nothing Or Not endCell Is Nothing Then If startCell Is Nothing Or endCell Is Nothing Then Err.Raise vbObjectError + 2, , "Missing marker on " & ws.Name If endCell.Row <= startCell.Row Then Err.Raise vbObjectError + 3, , "Markers are out of order on " & ws.Name rowCount = endCell.Row - startCell.Row - 1 If rowCount > 0 Then destSheet.Cells(nextRow, 1).Resize(rowCount, 26).Value2 = ws.Cells(startCell.Row + 1, 1).Resize(rowCount, 26).Value2 nextRow = nextRow + rowCount End If End If Next ws destBook.Close SaveChanges:=True Set destBook = Nothing destApp.Quit Set destApp = Nothing MsgBox "Copy completed." Exit Sub Failed: errorMessage = Err.Description On Error Resume Next If Not destBook Is Nothing Then destBook.Close SaveChanges:=False If Not destApp Is Nothing Then destApp.Quit On Error GoTo 0 MsgBox "Copy failed: " & errorMessage, vbExclamation End Sub
In Excel, select Developer > Macros, choose CopyOpenItems, and click Run. Open the destination workbook and check the appended rows on the specified sheet.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and continue working with your existing spreadsheets; it does not directly fix Excel VBA issues.

Frequently Asked Questions
Can VBA write to a closed workbook using Range.Value?
No. Ordinary Range assignments require the destination workbook to be open in Excel. You can open it in a hidden instance, write the values, and then save and close it.
What happens if a source sheet has no marker rows?
The example skips a sheet when neither marker is present. If only one marker is present, or the end marker comes first, it stops and closes the destination without saving the changes.
How do I copy different columns or include formatting?
In the VBA editor, change the starting column and the width in both Resize calls to select a different range. The example uses Value2, so copying formulas or formatting requires a different approach, such as Range.Copy to an open destination.
Is there a way to update the file without opening it in Excel?
A suitable external data-access method, such as a database connection, may update a closed workbook without an ordinary Excel Range assignment. Its capabilities and setup depend on the file format, provider, and data layout.




