Append files in excel

This article will show you how to easily add/attach file like PDF, Word or any other to Excel spreadsheet.

Applies to: Excel, Excel 2013, Excel 2016, Excel 2019, Excel 365, Office 365

  1. Go to Insert tab


    Excel 2016/2019/365 - Home tab

    Excel 2016/2019/365 — Home tab

  2. Click Object button placed in Text group.

    Narrow window

    Insert Object button - narrow window

    Insert Object button — narrow window

    Medium width window

    Insert Object button - medium width window

    Insert Object button — medium width window

    Maximum width window

    Insert Object button - max width window

    Insert Object button — max width window

  3. Click Create from file and browse for the file.


    Object window in Excel

    Click to enlarge

Skip to content

How to Insert Attachments in Excel?

Do you ever need to insert files into Excel, so you can share more comprehensive information with your colleagues? Either to insert PDF into Excel or to insert word documents into Excel, it’s just as simple as clicking on InsertText, Object, choosing your file, and voila!

Then, what happens after? Your file will float around your spreadsheet and not into a single cell. Yet, to be able to sort or move it with the rest of the content, what you really need is to put it into a single cell. How can you do this?

In this brief article, we will see how Excel can better handle your attachments, then we will look into an alternative that integrates with Excel: RowShare, an online table that offers a collaboration solution.

Insert Files into Excel Sheet

There are several ways to insert files into Excel sheet. You can either create from files, create new or add link to files. We will explore how to do it one by one.

If you want to create from an existing file, follow these steps:

  1. Select the cell into which you want to insert your file
  2. Click on the “Insert” tab
  3. Click on “Object” under the “Text” group
  4. Select “Create from File”
  5. Browse your file
  6. Select the “Display as icon” check box to if you want to insert an icon linking to the files
  7. Click on “OK”

Another possibility is to create a new file. You can do that by selecting “Create New” instead, and choose the type of object you want to attach. A new window will then pop up and you can create a new file you want to insert.

If you only want to add a link to the file instead of embedding the file, select the “Link to File” check box. The file should be stored in a location accessible to those with whom you want to share it. If the link to your file redirects to your computer, you will be the only one able to open it. Other than that, the location of the file should remain the same, if you move it to different location, the link won’t work anymore.

Attach Files in Excel, Within Excel Cells

Once you have made it to insert word documents into Excel or attach PDF to Excel, you probably realize that what you need is to insert your file into a single cell. To do this, follow these steps:

  1. Resize your file or cell until they fit each other
  2. Right click on your file and select “Format Object”
  3. Click on the “Properties” tab
  4. Select “Move and size with cells”

Since your Excel file size will be the size of the sheet itself plus the size of each other files attached, your Excel size will then be so huge. Also, as you can see here, there is no direct way to insert your file to an Excel cell automatically.

So, crunch time: Should I follow all these long endless steps or should I stick to the traditional way and exchange my files using email instead?

Attach Files Easily with RowShare

It’s time to say goodbye to complicated spreadsheet and endless email exchange. RowShare, an online table focusing in sharing and collaboration, offers a simpler solution. You can insert attachments within a click of a mouse! Start by creating a table from RowShare’s templates or from scratch.

Attach your files by following these super simple steps:

  1. Add a column of type “File” to your table
  2. Click on the cell, browse your file and attach it to your RowShare table

You can also control your table and files the way you want it, from sorting, filtering, setting columns as read only, you name it. It couldn’t be any simpler, here is a quick video if you need help:

When it comes to collecting, centralizing and sharing data with your coworkers, partners, suppliers, clients, RowShare is a real time-saver !
Discover all the things you could do :

Tip: If you already have your table in Excel, import it to RowShare and change the appropriate column type to File. Then, click on the cell and upload the file.

Once you have finished adjusting your table in RowShare, you can then synchronize it with Excel to access the best features of both tools. When synchronizing with Excel, instead of having the file itself, you will have the link of the file in your Excel sheet.

Happy RowSharing!

Collaborative, simple and reliable.

Rowshare is the first spreadsheet designed especially for managing projects and organizing administrative tasks.

Page load link

We use cookies to give you the most relevant experience by remembering your preferences. Click “ACCEPT AND CONTINUE” to continue using our web site and allow the use of all the cookies. Click «REJECT ALL» to only use the cookies required by our site to deliver its basic service.
REJECT ALLACCEPT AND CONTINUE

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

Powered by CookieYes Logo

Go to Top

Dorothy wanted to learn how to insert objects into her Excel spreadsheets:

I believe that i have seen a Microsoft Excel worksheet that had a Word document embedded in it. Can you explain how can i insert Word files into Excel and in general how to embed file objects in Office? Just so you are aware, I am using Excel 365.

Thanks for the question. One of the key benefits of an integrated productivity suite, such as Office, is the ability to insert files of specific type into other files. For example – you can add Word document files into other Microsoft Office applications, namely Excel worksheets, Outlook emails and PowerPoint presentations.

This quick tutorial is aimed at explaining how you can embed Word objects (being a document, presentation, diagram, notebook)  into Excel. You can use a similar process when adding docx files to PowerPoint or to other Word files.

Inserting Word docs into Microsoft Excel sheets

  1. First off, go ahead and open Microsoft Excel.
  2. Then hit File, and navigate to the Open tab.
  3. Now search and open for your Excel workbook. (Tip – consider pinning files for easier access in the future).
  4. In your Excel file, navigate to your the tab in Excel into which you would like to add the attachment/embed.
  5. From the Ribbon, hit Insert.
  6. In the right hand side of the Ribbon, hit Object (located in the Text group of the Insert tab).
  7. At this point, you can either add a new Word file to your worksheet or an existing one. Select Create a new file and pick Microsoft Word as the object type from the drop down list to add a brand new document or select Create from file to add an existing file  to the spreadsheet.
  8. Now, go ahead and adjust the look and feel of your embedded object so it will fit your spreadsheet layout.
  9. Next, hit OK.
  10. And obviously, don’t forget to save your Excel spreadsheet on your computer, network drive or OneDrive.

Adding Word as attachments into Excel files

In a similar fashion you are able to insert your Word doc as an attachment to the worksheet.

Follow steps 1-6 above, but be sure of highlighting the Display as Icon and Link to File check-boxes before moving to step 8. Your document will displayed as an icon on your spreadsheet, which you can double click to open it.

Linking to a file from Word and Excel

As shown above, by using the Link to File feature, you can easily link to any embedded file or icon in your spreadsheet or document.

Notes:

  1. As shown above, embedded files can also be displayed as links or icons in your spreadsheet.
  2. The process we just outlined applies for adding any type of files (including if needed, image, graphs, equation objects and so forth) into an Excel spreadsheet.

Embedding Word documents into Excel on macOS

  1. Open Excel for macOS.
  2. Navigate and open your spreadsheet.
  3. Go to the Insert tab.
  4. Now, go ahead and hit Select Object.
  5. The Insert Object form will appear:
Embed_Word_Excel_MAC
  1. Select Microsoft Word document to insert a brand new file, or hit the From file button to add an existing doc to your worksheet.
  2. Last hit OK, and don’t forget to save your file.

Finally, now that you know everything about embedding Word documents into spreadsheets, you might want to learn how to insert Excel sheets into Word docs.

Note:

  1. If you are using Microsoft Office on MAC, you’ll be able to embed Word documents into Excel for MAC, but not into PowerPoint presentations nor Visio diagrams.

Copying Word content into an Excel spreadsheet

A reader asked whether he is able to copy and paste between Word an Excel. A very prevalent use case for that is when you have content in a Word table and you would like to paste it into your spreadsheet. This is possible, but with a couple caveats / tricks mostly related to the pasted content formatting:

  • Assuming that you have a table in your Word document, highlight it, then hit the right mouse click and hit Copy.
  • Open your Excel spreadsheet and navigate to the place you would like to paste your table.
  • Right click and use the Paste Options menu to set the formatting of your pasted data.
  • Alternatively, use Paste Special (also available in your right mouse button) to paste as text, HTML or embed the content as a “live” Word object.

You can use Object Linking and Embedding (OLE) to include content from other programs, such as Word or Excel.

OLE is supported by many different programs, and OLE is used to make content that is created in one program available in another program. For example, you can insert an Office Word document in an Office Excel workbook. To see what types of content that you can insert, click Object in the Text group on the Insert tab. Only programs that are installed on your computer and that support OLE objects appear in the Object type box.

If you copy information between Excel or any program that supports OLE, such as Word, you can copy the information as either a linked object or an embedded object. The main differences between linked objects and embedded objects are where the data is stored and how the object is updated after you place it in the destination file. Embedded objects are stored in the workbook that they are inserted in, and they are not updated. Linked objects remain as separate files, and they can be updated.

Linked and embedded objects in a document

Linked and embedded objects in an Office for Windows document

1. An embedded object has no connection to the source file.

2. A linked object is linked to the source file.

3. The source file updates the linked object.

When to use linked objects

If you want the information in your destination file to be updated when the data in the source file changes, use linked objects.

With a linked object, the original information remains stored in the source file. The destination file displays a representation of the linked information but stores only the location of the original data (and the size if the object is an Excel chart object). The source file must remain available on your computer or network to maintain the link to the original data.

The linked information can be updated automatically if you change the original data in the source file. For example, if you select a paragraph in a Word document and then paste the paragraph as a linked object in an Excel workbook, the information can be updated in Excel if you change the information in your Word document.

When to use embedded objects

If you don’t want to update the copied data when it changes in the source file, use an embedded object. The version of the source is embedded entirely in the workbook. If you copy information as an embedded object, the destination file requires more disk space than if you link the information.

When a user opens the file on another computer, he can view the embedded object without having access to the original data. Because an embedded object has no links to the source file, the object is not updated if you change the original data. To change an embedded object, double-click the object to open and edit it in the source program. The source program (or another program capable of editing the object) must be installed on your computer.

Changing the way that an OLE object is displayed

You can display a linked object or embedded object in a workbook exactly as it appears in the source program or as an icon. If the workbook will be viewed online, and you don’t intend to print the workbook, you can display the object as an icon. This minimizes the amount of display space that the object occupies. Viewers who want to display the information can double-click the icon.

Embed an object in a worksheet

  1. Click inside the cell of the spreadsheet where you want to insert the object.

  2. On the Insert tab, in the Text group, click Object Object icon on the ribbon.
    Insert Object

  3. In the Object dialog box, click the Create from File tab.

  4. Click Browse, and select the file you want to insert.

  5. If you want to insert an icon into the spreadsheet instead of show the contents of the file, select the Display as icon check box. If you don’t select any check boxes, Excel shows the first page of the file. In both cases, the complete file opens with a double click. Click OK.

    Note: After you add the icon or file, you can drag and drop it anywhere on the worksheet. You can also resize the icon or file by using the resizing handles. To find the handles, click the file or icon one time.

Insert a link to a file

You might want to just add a link to the object rather than fully embedding it. You can do that if your workbook and the object you want to add are both stored on a SharePoint site, a shared network drive, or a similar location, and if the location of the files will remain the same. This is handy if the linked object undergoes changes because the link always opens the most up-to-date document.

Note: If you move the linked file to another location, the link won’t work anymore.

  1. Click inside the cell of the spreadsheet where you want to insert the object.

  2. On the Insert tab, in the Text group, click Object Object icon on the ribbon.
    Insert Object

  3. Click the Create from File tab.

  4. Click Browse, and then select the file you want to link.

  5. Select the Link to file check box, and click OK.

Create a new object from inside Excel

You can create an entirely new object based on another program without leaving your workbook. For example, if you want to add a more detailed explanation to your chart or table, you can create an embedded document, such as a Word or PowerPoint file, in Excel. You can either set your object to be displayed right in a worksheet or add an icon that opens the file.

This embedded object is a Word document.

  1. Click inside the cell of the spreadsheet where you want to insert the object.

  2. On the Insert tab, in the Text group, click Object Object icon on the ribbon.
    Insert Object

  3. On the Create New tab, select the type of object you want to insert from the list presented. If you want to insert an icon into the spreadsheet instead of the object itself, select the Display as icon check box.

  4. Click OK. Depending on the type of file you are inserting, either a new program window opens or an editing window appears within Excel.

  5. Create the new object you want to insert.

    When you’re done, if Excel opened a new program window in which you created the object, you can work directly within it.

    You can edit the embedded Word document directly in Excel.

    When you’re done with your work in the window, you can do other tasks without saving the embedded object. When you close the workbook your new objects will be saved automatically.

    Note: After you add the object, you can drag and drop it anywhere on your Excel worksheet. You can also resize the object by using the resizing handles. To find the handles, click the object one time.

Embed an object in a worksheet

  1. Click inside the cell of the spreadsheet where you want to insert the object.

  2. On the Insert tab, in the Text group, click Object.

    The Object option is on the Insert tab.

  3. Click the Create from File tab.

    The "Create from File" tab on the Object dialog box.

  4. Click Browse, and select the file you want to insert.

  5. If you want to insert an icon into the spreadsheet instead of show the contents of the file, select the Display as icon check box. If you don’t select any check boxes, Excel shows the first page of the file. In both cases, the complete file opens with a double click. Click OK.

    Note: After you add the icon or file, you can drag and drop it anywhere on the worksheet. You can also resize the icon or file by using the resizing handles. To find the handles, click the file or icon one time.

Insert a link to a file

You might want to just add a link to the object rather than fully embedding it. You can do that if your workbook and the object you want to add are both stored on a SharePoint site, a shared network drive, or a similar location, and if the location of the files will remain the same. This is handy if the linked object undergoes changes because the link always opens the most up-to-date document.

Note: If you move the linked file to another location, the link won’t work anymore.

  1. Click inside the cell of the spreadsheet where you want to insert the object.

  2. On the Insert tab, in the Text group, click Object.

    The Object option is on the Insert tab.

  3. Click the Create from File tab.

  4. Click Browse, and then select the file you want to link.

  5. Select the Link to file check box, and click OK.

    On the "Create from File" tab, select "Link to file."

Create a new object from inside Excel

You can create an entirely new object based on another program without leaving your workbook. For example, if you want to add a more detailed explanation to your chart or table, you can create an embedded document, such as a Word or PowerPoint file, in Excel. You can either set your object to be displayed right in a worksheet or add an icon that opens the file.

This embedded object is a Word document.

  1. Click inside the cell of the spreadsheet where you want to insert the object.

  2. On the Insert tab, in the Text group, click Object.

    The Object option is on the Insert tab.

  3. On the Create New tab, select the type of object you want to insert from the list presented. If you want to insert an icon into the spreadsheet instead of the object itself, select the Display as icon check box.

    The Create New tab in the Object dialog box.

  4. Click OK. Depending on the type of file you are inserting, either a new program window opens or an editing window appears within Excel.

  5. Create the new object you want to insert.

    When you’re done, if Excel opened a new program window in which you created the object, you can work directly within it.

    You can edit the embedded Word document directly in Excel.

    When you’re done with your work in the window, you can do other tasks without saving the embedded object. When you close the workbook your new objects will be saved automatically.

    Note: After you add the object, you can drag and drop it anywhere on your Excel worksheet. You can also resize the object by using the resizing handles. To find the handles, click the object one time.

Link or embed content from another program by using OLE

You can link or embed all or part of the content from another program.

Create a link to content from another program

  1. Click in the worksheet where you want to place the linked object.

  2. On the Insert tab, in the Text group, click Object.

    Excel Ribbon Image

  3. Click the Create from File tab.

  4. In the File name box, type the name of the file, or click Browse to select from a list.

  5. Select the Link to file check box.

  6. Do one of the following:

    • To display the content, clear the Display as icon check box.

    • To display an icon, select the Display as icon check box. Optionally, to change the default icon image or label, click Change Icon, and then click the icon that you want from the Icon list, or type a label in the Caption box.

    Note: You cannot use the Object command to insert graphics and certain types of files. To insert a graphic or file, on the Insert tab, in the Illustrations group, click Picture.

Embed content from another program

  1. Click in the worksheet where you want to place the embedded object.

  2. On the Insert tab, in the Text group, click Object.

    Excel Ribbon Image

  3. If the document does not already exist, click the Create New tab. In the Object type box, click the type of object that you want to create.

    If the document already exists, click the Create from File tab. In the File name box, type the name of the file, or click Browse to select from a list.

  4. Clear the Link to file check box.

  5. Do one of the following:

    • To display the content, clear the Display as icon check box.

    • To display an icon, select the Display as icon check box. To change the default icon image or label, click Change Icon, and then click the icon that you want from the Icon list, or type a label in the Caption box.

Link or embed partial content from another program

  1. From a program other than Excel, select the information that you want to copy as a linked or embedded object.

  2. On the Home tab, in the Clipboard group, click Copy.

    The Clipboard group on the Home tab

  3. Switch to the worksheet that you want to place the information in, and then click where you want the information to appear.

  4. On the Home tab, in the Clipboard group, click the arrow below Paste, and then click Paste Special.

  5. Do one of the following:

    • To paste the information as a linked object, click Paste link.

    • To paste the information as an embedded object, click Paste. In the As box, click the entry with the word «object» in its name. For example, if you copied the information from a Word document, click Microsoft Word Document Object.

Change the way that an OLE object is displayed

  1. Right-click the icon or object, point to object typeObject (for example, Document Object), and then click Convert.

  2. Do one of the following:

    • To display the content, clear the Display as icon check box.

    • To display an icon, select the Display as icon check box. Optionally, you can change the default icon image or label. To do that, click Change Icon, and then click the icon that you want from the Icon list, or type a label in the Caption box.

Control updates to linked objects

You can set links to other programs to be updated in the following ways: automatically, when you open the destination file; manually, when you want to see the previous data before updating with the new data from the source file; or when you specifically request the update, regardless of whether automatic or manual updating is turned on.

Set a link to another program to be updated manually

  1. On the Data tab, in the Connections group, click Edit Links.

    Excel Ribbon Image

    Note: The Edit Links command is unavailable if your file does not contain links to other files.

  2. In the Source list, click the linked object that you want to update. An A in the Update column means that the link is automatic, and an M in the Update column means that the link is set to Manual update.

    Tip: To select multiple linked objects, hold down CTRL and click each linked object. To select all linked objects, press CTRL+A.

  3. To update a linked object only when you click Update Values, click Manual.

Set a link to another program to be updated automatically

  1. On the Data tab, in the Connections group, click Edit Links.

    Excel Ribbon Image

    Note: The Edit Links command is unavailable if your file does not contain links to other files.

  2. In the Source list, click the linked object that you want to update. An A in the Update column means that the link will update automatically, and an M in the Update column means that the link must be updated manually.

    Tip: To select multiple linked objects, hold down CTRL and click each linked object. To select all linked objects, press CTRL+A.

  3. Click OK.

Issue: I can’t update the automatic links on my worksheet

The Automatic option can be overridden by the Update links to other documents Excel option.

To ensure that automatic links to OLE objects can be automatically updated:

  1. Click the Microsoft Office Button Office button image, click Excel Options, and then click the Advanced category.

  2. Under When calculating this workbook, make sure that the Update links to other documents check box is selected.

Update a link to another program now

  1. On the Data tab, in the Connections group, click Edit Links.

    Excel Ribbon Image

    Note: The Edit Links command is unavailable if your file does not contain linked information.

  2. In the Source list, click the linked object that you want to update.

    Tip: To select multiple linked objects, hold down CTRL and click each linked object. To select all linked objects, press CTRL+A.

  3. Click Update Values.

Edit content from an OLE program

While you are in Excel, you can change the content linked or embedded from another program.

Edit a linked object in the source program

  1. On the Data tab, in the Connections group, click Edit Links.

    Excel Ribbon Image

    Note: The Edit Links command is unavailable if your file does not contain linked information.

  2. In the Source file list, click the source for the linked object, and then click Open Source.

  3. Make the changes that you want to the linked object.

  4. Exit the source program to return to the destination file.

Edit an embedded object in the source program

  1. Double-click the embedded object to open it.

  2. Make the changes that you want to the object.

  3. If you are editing the object in place within the open program, click anywhere outside of the object to return to the destination file.

    If you edit the embedded object in the source program in a separate window, exit the source program to return to the destination file.

Note: Double-clicking certain embedded objects, such as video and sound clips, plays the object instead of opening a program. To edit one of these embedded objects, right-click the icon or object, point to object typeObject (for example, Media Clip Object), and then click Edit.

Edit an embedded object in a program other than the source program

  1. Select the embedded object that you want to edit.

  2. Right-click the icon or object, point to object typeObject (for example, Document Object), and then click Convert.

  3. Do one of the following:

    • To convert the embedded object to the type that you specify in the list, click Convert to.

    • To open the embedded object as the type that you specify in the list without changing the embedded object type, click Activate.

Select an OLE object by using the keyboard

  1. Press CTRL+G to display the Go To dialog box.

  2. Click Special, select Objects, and then click OK.

  3. Press TAB until the object that you want is selected.

  4. Press SHIFT+F10.

  5. Point to Object or Chart Object, and then click Edit.

Issue: When I double-click a linked or embedded object, a «cannot edit» message appears

This message appears when the source file or source program can’t be opened.

Make sure that the source program is available     If the source program is not installed on your computer, convert the object to the file format of a program that you do have installed.

Ensure that memory is adequate     Make sure that you have enough memory to run the source program. Close other programs to free up memory, if necessary.

Close all dialog boxes     If the source program is running, make sure that it doesn’t have any open dialog boxes. Switch to the source program, and close any open dialog boxes.

Close the source file     If the source file is a linked object, make sure that another user doesn’t have it open.

Ensure that the source file name has not changed     If the source file that you want to edit is a linked object, make sure that it has the same name as it did when you created the link and that it has not been moved. Select the linked object, and then click the Edit Links command in the Connections group on the Data tab to see the name of the source file. If the source file has been renamed or moved, use the Change Source button in the Edit Links dialog box to locate the source file and reconnect the link.

Need more help?

You can always ask an expert in the Excel Tech Community or get support in the Answers community.

Home/ Power Query/ Import Multiple Excel Files from a Folder into a Single Excel File

In this tutorial, we look at how to import multiple Excel files into Excel from a folder using Power Query.

It is amazing how simple this process can be with Power Query and modern day Excel. However, it can still come with the odd hiccup.

In this example, I want to import all the Excel files from the folder shown below.

Folder from which to import multiple Excel files with Power Query

Ideally this folder would contain Excel files ONLY. But when you have teams of people working on a shared area, things happen.

Somehow a PDF file and a JPG (picture of me) have appeared in the folder.

This can create complications with the query we will create to import the Excel files. So we will build in some protection so that the process works seamlessly. Both for now, but also for future imports.

In the future, it will be a case of clicking the Refresh button. And any changed files, or additional files will all be imported again without complication.

Watch the Video

We begin the process by clicking the Data tab > Get Data button > From File and then From Folder.

Import files from a folder in Excel

With the regular changes made to Excel, your screen may look slightly different to above.

We will then be asked to browse for the folder to import from. I have selected the reports folder on my Desktop.

Browse for the folder of files

A preview window appears showing the files from that folder.

Preview of the files in the folder

Click Edit to go to the Power Query Editor. We can then make some changes in here to ensure that only the Excel files are imported.

The files are listed in the editor.

Files to import listed in the Power Query Editor

A column with the Extension is provided. This is useful for us to exclude the non Excel files.

Click the filter arrow for the Extension column, select Text Filters and Begins With.

We can then enter .xls to ensure only Excel files are used. This folder also includes a .xlsm (macro enabled worksheet). So by using .xls instead of .xlsx we are catering for .xls, .xlsm and .xlsx files.

Filter to import multiple Excel files only

The PDF and JPG files are filtered out of the list. We can now combine the files and append the data from each one into one Excel file.

Click the Combine Files button (double arrow on the Content column header).

Combine all files into one table

The Combine Files window appears. You will see the sheets of the workbooks here and can preview them. Sheet1 is selected as the sheet that includes the data that we want.

The Skip files with errors box has been checked also. This is important to avoid possible errors affecting the import process.

Combine files window in Power Query

A common error occurs when you try and refresh a connection, but someone has one of the Excel files from that folder open. If you do not check the box to skip errors, your query is interrupted with an error message.

The data from the files is stacked on top of each other into a single list.

Combined files stacked into one table

A few more transformations can be done here to make your data look correct.

In the video I split the Source.Name column to separate the extension from the file name. The extension column is then removed. The names converted to capitalise each word. And the column renamed Location to end up with the below.

Transformed list of Excel files to import into Excel

You can then Close and Load the data into a new or existing workbook.

When new files are added, or data changed in those files. You just need to click the Data tab and Refresh to update the connection to that folder, and everything updates.

Superb!

Skip to content

VBA to append a text file Excel

Home » VBA » VBA to append a text file Excel

VBA code to VBA to append a text file Excel Macros Examples for writing to text files using VBA in MS Office Word, PowerPoint, Access, Excel 2003, 2007, 2010, 2013 and VBScript. This Example VBA Program and function will help you to know using VBA to append a text file using Excel VBA.

Appending a text file Using VBA

Here is the Procedure, Example VBA Syntax and Example VBA Macro code for appending a text file in Excel VBA. This will help you to know how to append a text file and add data using VBA.

VBA to append a text file: Procedure

We will first open the text file as Append file with a file number. Then we will add the data to the file using File Number.

VBA to append a text file: Syntax

Here is the VBA code and syntax for Appending a text file Using VBA. Use the Append keyword to open text file in appending mode.

strFile_Path = "Your file Path"
Open strFile_Path For Append As #1
Write #1, “You can write your required text here”

VBA to Append a text file: Example Macro Code

Following is the sample Excel Macro to write to a text file by appending it using Excel VBA.

Sub VBA_to_append_text_file()
    Dim strFile_Path As String
    strFile_Path = "C:temptest.txt" ‘Change as per your test folder and exiting file path to append it.
    Open strFile_Path For Append As #1
    Write #1, "This is my sample text"
    Close #1
End Sub 

Instructions to run the VBA Macro code to append a text file

Please follow the below steps to execute the VBA code to Append a text file using Excel VBA Editor.
1. Step 1: Open any Excel workbook [ To Open MS Excel: Go to Start menu, All programs and select Excel from MS Office OR You can simply type excel in the run command (Press Windows+ r key to open run dialog)]
2. Step 2: Press Alt+F11 to open the VBA Editor [You can also open the VBE from the Developer Tab in the Excel ribbon]
3. Step 3: Insert a code module [Go to insert menu in the VBE and then press Module OR Simply press the Alt+i then m to insert code module]
4. Step 4: Copy the above Example Macro code and paste in the code module which have inserted in the above step
5. Step 5: Change the folder path as per your testing folder structure and existing file path.
6. Step 6: Now press the F5 to Run and Execute the Macro.
7. You can press the F8 to debug the macro line by line and see the result immediately.

Once you are done with the macro execution, now you can observe that a text file in the test folder. And the text file in the folder is appended with the data which you have mentioned in the code.

VBA to Append a text file – Macro Explained

Here is the detailed explanation of the Excel macro to append a text file using VBA.

  • Starting the program and sub procedure to write VBA code to append a text file and adding the data.
  • Declaring the strFile_Path variable as String Data Type to store the text file path.
  • Assigning the File path to the variable strFile_Path.
  • Opening the text file for Append with FileNumber as 1.
  • Writing to the sample text to the File using FileNumber and Write Command.
  • Closing the File using FileNumber.
  • Ending the Sub procedure to write VBA Code to append a text file and updating the data.

Here is the commented macro code for writing to text file and append it using VBA.

‘Starting the program and sub procedure to write VBA code to write the data to a text file Append.
Sub VBA_to_append_text_file()

‘Declaring the strFile_Path variable as String Data Type to store the text file path.
Dim strFile_Path As String

‘Assigning the File path to the variable strFile_Path.
strFile_Path = “C:temptest.txt”

‘Opening the text file for Append with FileNumber as 1.
Open strFile_Path For Append As #1

‘Writing to the sample text to the File using FileNumber and Write Command.
Write #1, “This is my sample text”

‘Closing the File using FileNumber.
Close #1
End Sub’Ending the Sub procedure to write VBA Code to append the data in text file.

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Excel VBA Project Management Templates
All-in-One Pack
120+ Project Management Templates
Essential Pack
50+ Project Management Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project Management Template

Ultimate Resource Management Template

Project Portfolio Management Templates

Related Posts

  • Appending a text file Using VBA
    • VBA to append a text file: Procedure
    • VBA to append a text file: Syntax
    • VBA to Append a text file: Example Macro Code
      • Instructions to run the VBA Macro code to append a text file
    • VBA to Append a text file – Macro Explained

VBA Reference

Effortlessly
Manage Your Projects

120+ Project Management Templates

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

120+ PM Templates Includes:

Effectively Manage Your
Projects and  Resources

With Our Professional and Premium Project Management Templates!

ANALYSISTABS.COM provides free and premium project management tools, templates and dashboards for effectively managing the projects and analyzing the data.

We’re a crew of professionals expertise in Excel VBA, Business Analysis, Project Management. We’re Sharing our map to Project success with innovative tools, templates, tutorials and tips.

Project Management
Excel VBA

Download Free Excel 2007, 2010, 2013 Add-in for Creating Innovative Dashboards, Tools for Data Mining, Analysis, Visualization. Learn VBA for MS Excel, Word, PowerPoint, Access, Outlook to develop applications for retail, insurance, banking, finance, telecom, healthcare domains.

Analysistabs Logo

Page load link

Go to Top

You have several Excel workbooks and you want to merge them into one file? This could be a troublesome and long process. But there are 6 different methods of how to merge existing workbooks and worksheets into one file. Depending on the size and number of workbooks, at least one of these methods should be helpful for you. Let’s take a look at them.

Summary

If you want to merge just a small amount of files, go with methods 1 or method 2 below. For anything else, please take a look at the methods 4 to 6: Either use a VBA macro, conveniently use an Excel-add-in or use PowerQuery (PowerQuery only possible if the sheets to merge have exactly the same structure).

Method 1: Copy the cell ranges

copy, paste, source, main, merge, join
Copy and paste the source content into your main workbook.

The obvious method: Select the source cell range, copy and paste them into your main workbook. The disadvantage: This method is very troublesome if you have to deal with several worksheets or cell ranges. On the other hand: For just a few ranges it’s probably the fastest way.

Method 2: Manually copy worksheets

copy, move, worksheet, sheet, excel
Copy worksheets separately to the “master” workbook.

The next method is to copy or move one or several Excel sheets manually to another file. Therefore, open both Excel workbooks: The file containing the worksheets which you want to merge (the source workbook) and the new one, which should comprise all the worksheets from the separate files.

  1. Select the worksheets in your source workbooks which you want to copy. If there are several sheets within one file, hold the Ctrl key computer_key_Ctrland click on each sheet tab. Alternatively, go to the first worksheet you want to copy, hold the Shift key computer_key_Shift and click on the last worksheet. That way, all worksheets in between will be selected as well.
  2. Once all worksheets are selected, right click on any of the selected worksheets.
  3. Click on “Move or Copy”.
  4. Select the target workbook.
  5. Set the tick at “Create a copy”. That way, the original worksheets remain in the original workbook and a copy will be created.
  6. Confirm with OK.

One small tip at this point: You can just drag and drop worksheets from one to another Excel file. Even better: If you press and hold the Ctrl-Key when you drag and drop the worksheets, you create copies.


Do you want to boost your productivity in Excel?

Get the Professor Excel ribbon!

Add more than 120 great features to Excel!


Method 3: Use the INDIRECT formula

The next method comes with some disadvantages and is a little bit more complicated. It works, if your files are in a systematic file order and just want to import some certain values. You build your file and cell reference with the INDIRECT formula. That way, the original files remain and the INDIRECT formula only looks up the values within these files. If you delete the files, you’ll receive #REF! errors.

indirect, link, other, file, excel
With the INDIRECT formula you can link to other files. Only condition: the source file must be open in the background.

Let’s take a closer look at how to build the formula. The INDIRECT formula has only one argument: The link to another cell which can also be located within another workbook.

  1. Copy the first source cell.
  2. Paste it into your main file using paste special (Ctrl computer_key_Ctrl+ Alt computer_key_Alt+ v computer_key_V). Instead of pasting it normally, click on “Link” in the bottom left corner of the Paste Special window. That way, you extract the complete path. In our case, we have the following link:
    =[160615_Examples.xlsm]Thousands!$C$4
  3. Now we wrap the INDIRECT formula around this path. Furthermore, we separate it into file name, sheet name and cell reference. That way, we can later on just change one of these references, for instance for different versions of the same file. The complete formula looks like this (please also see the image above):
    =INDIRECT(“‘”&$A3&$B3&”‘!”&D$2&$C3)

Important – please note: This function only works if the source workbooks are open.

Method 4: Merge files with a simple VBA macro

You are not afraid of using a simple VBA macro? Then let’s insert a new VBA module:

  1. Go to the Developer ribbon. If you can’t see the Developer ribbon, right click on any ribbon and then click on “Customize the Ribbon…”. On the right hand side, set the tick at “Developer”.
  2. Click on Visual Basic on the left side of the Developer ribbon.
  3. Right click on your workbook name and click on Insert –> Module.
  4. Copy and paste the following code into the new VBA module. Position the cursor within the code and click start (the green triangle) on the top. That’s it!
Sub mergeFiles()
    'Merges all files in a folder to a main file.
    
    'Define variables:
    Dim numberOfFilesChosen, i As Integer
    Dim tempFileDialog As fileDialog
    Dim mainWorkbook, sourceWorkbook As Workbook
    Dim tempWorkSheet As Worksheet
    
    Set mainWorkbook = Application.ActiveWorkbook
    Set tempFileDialog = Application.fileDialog(msoFileDialogFilePicker)
    
    'Allow the user to select multiple workbooks
    tempFileDialog.AllowMultiSelect = True
    
    numberOfFilesChosen = tempFileDialog.Show
    
    'Loop through all selected workbooks
    For i = 1 To tempFileDialog.SelectedItems.Count
        
        'Open each workbook
        Workbooks.Open tempFileDialog.SelectedItems(i)
        
        Set sourceWorkbook = ActiveWorkbook
        
        'Copy each worksheet to the end of the main workbook
        For Each tempWorkSheet In sourceWorkbook.Worksheets
            tempWorkSheet.Copy after:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
        Next tempWorkSheet
        
        'Close the source workbook
        sourceWorkbook.Close
    Next i
    
End Sub

Method 5: Automatically merge workbooks

The fifth way is probably most convenient:

Click on “Merge Files” on the Professor Excel ribbon.

Now select all the files and worksheets you want to merge and start with “OK”.

This procedure works well also for many files at the same time and is self-explanatory. Even better: Besides XLSX files, you can also combine XLS, XLSB, XLSM, CSV, TXT and ODS files.

To do that you need a third party add-in, for example our popular “Professor Excel Tools” (click here to start the download).

Here is the whole process in detail:

Just click on "Merge Files" on the Professor Excel ribbon, select your files and click on OK.

Just click on “Merge Files” on the Professor Excel ribbon, select your files and click on OK.

Method 6: Use the Get & Transform tools (PowerQuery)

merge, workbooks, get, transform, complete, folder
merge, workbooks, get, transform, complete, folder
merge, workbooks, get, transform, complete, folder

The current version of Excel 365 offers the “Get & Transform” tools to import data. These functions are very powerful and are supposed to replace the old “Text Import Wizard”. However, they have one useful feature: Import a complete folder of documents.

The requirements: The workbooks and worksheets you want to import have to be in the same format.

Please follow these steps for importing a complete folder of Excel files.

  1. Create a folder with all the documents you want to import.
  2. Usually it’s the fastest to just copy the folder path directly from the Windows Explorer. You still have the change to later-on select the folder, though.
  3. Within Excel, go to the Data ribbon and click on “Get Data”, “From File” and then on “From Folder”.
  4. Paste the previously copied path or select it via the “Browse” function. Continue with “OK”.
  5. If all files are shown in the following window, either click on “Combine” (and then on “Combine & Load To”) or on “Edit”. If you click on “Edit”, you can still filter the list and only import a selection of the files in the list. Recommendation: Put only the necessary files into your import folder from the beginning so that you don’t have to navigate through the complex “Edit” process.
  6. Next, Excel shows an example of the data based on the first file. If everything seems fine, click on OK. If your files have several sheets, just select the one you want to import, in this example “Sheet1”. Click on “OK”.
  7. That’s it, Excel now imports the data and inserts a new column containing the file name.

For more information about the Get & Transform tools please refer to this article.

Next step: Merge multiple worksheets to one combined sheet

After you have combined many Excel workbooks into one file, usually the next step is this: Merge all the imported sheets into one worksheet.

Because this is a whole different topic by itself, please refer to this article.

Image by MartinHolzer from Pixabay

Append All Files in a Folder — Application

A demonstration of the process for appending all files in a folder into a single query.

When to use

Use to combine binaries after setting up a FilesList query.

Instructions

Getting Started

  • If you have not already created a FilesList query, DO THAT FIRST!
  • Expose the Queries pane —> right click the FilesList query —> Reference

Master Query – Initial modifications

  • Rename the Master query immediately to a more logical name (still referred to as Master here)
  • Click the Combine Binaries button and click OK
  • Wait for the four queries to be created on the left side of the window
  • Delete the Changed Type step that was added to the Master query

Transform Sample modifications

  • Select the Transform Sample in the queries pane on the left side of the window
  • Perform any required transformations to the Transform Sample query

Master Query – Final modifications

  • Select the Master query
  • Modify the Removed Other Columns1 step to keep any desired file properties
  • Add any additional transformation steps required to the Master query
  • Set the Data Types
  • Finalize the query by loading it to the desired location

Hints & tips

  • Using the FilesList query avoids the file path being hard coded into the solution twice
  • Using the FilesList query also makes it much easier to sort and change the file used by the Transform Sample
  • To change the file used by the sample transform, re-sort the FilesList query, go to Home —> Refresh Preview.  The new file will now be the one used in the Transform Sample

Login to download

I have one folder with multiple text files that I add one text file to every day. All text files are in the same format and are pipe delimited.

Is it possible to create code for excel that will automatically import the data from the multiple text files into one worksheet?

I found some code that would import all the text files from the folder, but only if I changed it all to comma delimited first. Also, I could not get it to update if I added files to the folder.

Any help would be greatly appreciated!

Community's user avatar

asked Feb 8, 2011 at 19:54

Kim's user avatar

1

A good way to handle files in general is the ‘FileSystemObject’. To make this available in VBA you need to add a reference to it:

(select the ToolsReferences menu. In the References dialog, select ‘Microsoft Scripting Runtime’)

The following code example will read all files in a folder, reads their content one line at a time, split each line into | separated bits, and writes theses bits to the active sheet starting at cell A1, one row per line.

Sub ReadFilesIntoActiveSheet()
    Dim fso As FileSystemObject
    Dim folder As folder
    Dim file As file
    Dim FileText As TextStream
    Dim TextLine As String
    Dim Items() As String
    Dim i As Long
    Dim cl As Range

    ' Get a FileSystem object
    Set fso = New FileSystemObject

    ' get the directory you want
    Set folder = fso.GetFolder("D:YourDirectory")  

    ' set the starting point to write the data to
    Set cl = ActiveSheet.Cells(1, 1)

    ' Loop thru all files in the folder
    For Each file In folder.Files
        ' Open the file
        Set FileText = file.OpenAsTextStream(ForReading)

        ' Read the file one line at a time
        Do While Not FileText.AtEndOfStream
            TextLine = FileText.ReadLine

            ' Parse the line into | delimited pieces
            Items = Split(TextLine, "|")

            ' Put data on one row in active sheet
            For i = 0 To UBound(Items)
                cl.Offset(0, i).Value = Items(i)
            Next

            ' Move to next row
            Set cl = cl.Offset(1, 0)
        Loop

        ' Clean up
        FileText.Close
    Next file

    Set FileText = Nothing
    Set file = Nothing
    Set folder = Nothing
    Set fso = Nothing

End Sub

the sub is deliberately simplified to remain clear (i hope) and will need work to be made robust (eg add error handling)

answered Feb 9, 2011 at 5:23

chris neilsen's user avatar

chris neilsenchris neilsen

52.2k10 gold badges84 silver badges122 bronze badges

1

Sounds like it’d be easiser to run a script to cycle thru all the files in the directory, create a new file composed of all the files’ contents as new lines, and save that as a csv. something like:

import os

basedir='c://your_root_dir'
dest_csv="<path to wherever you want to save final csv>.csv"
dest_list=[]


for root, subs, files in  os.walk(basedir):
    for f in files:
        thisfile=open(basedir+f)
        contents=thisfile.readlines()
        dest_list.append(contents)

#all that would create a list containing the contents of all the files in the directory
#now we'll write it as a csv

f_csv=open(dest_csv,'w')
for i in range(len(dest_list)):
     f_csv.write(dest_list[i])
f_csv.close()

You could save a script like that somewhere and run it each day, then open the resulting csv in excel. This assumes that you want to get the data from each file in a particular directory, and that all the files you need are in one directory.

answered Feb 8, 2011 at 20:24

John Conroy's user avatar

Понравилась статья? Поделить с друзьями:
  • Append dataframe to excel
  • Append data in excel
  • Appdata microsoft word startup
  • Appdata local microsoft word
  • Appcrash что это за проблема excel