Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Selectively Importing Records.

Selectively Importing Records

by Allen Wyatt
(last updated February 1, 2016)

5

Ole ran into a problem importing information into an Excel workbook. It seems that the files he needs to import typically have thousands and thousands of records in them. Ole doesn't need most of the input rows, however, and normally gets rid of them once the records are imported into Excel. Ole is looking for a way to get rid of the unwanted records during the import process, so that he has less work to do when the data is in his workbook.

There are a couple of different ways that a solution to this problem can be approached. One solution is to use Access as your first importing step. Access will easily handle the thousands of records you want to import—even if there are more records than what you can import into Excel. You could import the file into Access, filter out the unwanted records, and then export the resulting table as an Excel workbook.

The best solution, however, may be to bypass Excel's import filters entirely. You can easily write an import routine in VBA, and allow it to process the import file. For instance, consider the following macro:

Sub Import()
    Dim sFile As String
    Dim sUnwanted As String
    Dim sDelim As String
    Dim iRow As Integer
    Dim iCol As Integer
    Dim bBadRecord As Boolean
    Dim iTemp As Integer

    sFile = "d:\data.txt"
    sUnwanted = "bad text"
    sDelim = ","

    Open sFile For Input As #1

    iRow = 1
    While Not EOF(1) 'Scan file line by line
        iCol = 1
        Line Input #1, sBuffer

        ' Check to see if should ignore record
        bBadRecord = Instr(sBuffer, sUnwanted)

        If Not bBadRecord Then
            iTemp = Instr(sBuffer, sDelim)
            While iTemp > 0
                With Application.Cells(iRow, iCol)
                    .NumberFormat = "@" 'Text formatting
                    .Value = Left(sBuffer, iTemp-1)
                End With
                iCol = iCol + 1
                sBuffer = Mid(sBuffer, iTemp+1, Len(sBuffer))
                iTemp = Instr(sBuffer, sDelim)
            Wend
            If Len(sBuffer) > 0 Then
                With Application.Cells(iRow, iCol)
                    .NumberFormat = "@" 'Text formatting
                    .Value = sBuffer
                End With
            End If
            iRow = iRow + 1
        End If
    Wend
    Close #1
End Sub

This macro opens a data file and reads each record in the file. It checks the record to make sure it is OK to import, and then pulls the record apart, based on a delimiter, and stuffs the information into the current worksheet. You can change the name of the data file (the sFile variable), the text that indicates a bad record (sUnwanted variable) and the delimiter (sDelim variable).

As an example, let's assume that you have a data file named Customers.txt. This file contains all your customer records, but you don't want to import the records for customers with addresses inside the United States. Further, the records in the data file use a tab character between each field. In this case, you would only need to make the following changes to the variables at the beginning of the macro:

    sFile = "d:\Customers.txt"
    sUnwanted = "United States"
    sDelim = Chr(9)

Once you run the macro, the current worksheet contains just the desired data.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8491) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Selectively Importing Records.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Getting Rid of the Paste Options Box

Paste something in a Word document, and you may notice a dynamic little set of options appear right next to what you pasted. ...

Discover More

Always Opening a Workbook that is Editable

When you send a workbook to a coworker, it can be bothersome if that person has problems using what you created. There is a ...

Discover More

Word Counts for a Group of Documents

Getting a word count for a single document is easy. Getting an aggregate word count for a large number of documents can be a ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Using InputBox to Get Data

Need your macro to get some input from a user? The standard way to do this is with the InputBox function, described in this ...

Discover More

Saving Changes when Closing

If your macro closes workbooks, you'll want to make sure that it will save any changes you made to the workbook. Here's how ...

Discover More

Transferring Data between Worksheets Using a Macro

Macros can be used for all sorts of data processing needs. One need that is fairly common is the need to move data from one ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 4?

2016-02-01 21:54:42

Thomson

Power Query also good in this kind of work with minimum programming. Saving Excel Macro file in corporate environment might be hard sometime..


2016-02-01 10:48:06

Shandor

This is a workable solution, but in the big picture this workflow belongs in a database, with a reporting front end like Tableau or Birst. Old Access needs to be retired, along with Excel methods beyond prototyping and ad hoc data manipulation. AWS is commoditizing big data, and convenient tools will soon make even small businesses crunch numbers like Fortune 500's! Just thought I'd share a glimpse of the future that's here already.


2013-05-22 06:16:24

Barry Fitzpatrick

I don't think this will work if the import file has text fields (typically delimited by quotation marks) if the enclosed text contains the delimiting character (commonly a comma in CSV formatted files).

Excels own import filters ignore delimiter characters if enclosed with quotations marks.


2013-05-21 10:41:11

Américo Fernandes

Hi,
how I can, within this macro, convert the source informtion in UTF-8 to Unicode before doing the split into cells?


2013-05-20 13:22:27

Tony Moore

Similar topic to "Selectively Importing Records". MS Query under importing external data could be employed. The problem I've had with MS Query is that is does not release the access file after running and showing the result on the worksheet, until the excel file is closed. During that time, the access file can't be opened by users. I need to figure out how to set up MS Query so people can open an excel file, see current data being queried, yet still allow the Access Db to be used. If you have tips on MS Query, that would be very useful. (as are a lot of the weekly tips) Thanks, Tony


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.