Written by Allen Wyatt (last updated March 19, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
Gordon wonders how he can import a subset of a text file into Excel, depending upon the value of a particular field. As an example, he may want to only import records that contain a "y" in column 5 of each record in the text file.
There are several ways you can approach this task. One is that you could simply import the whole text file, sort the records, and delete those you don't want. This is, perhaps, the simplest option if you only need to process a single file and the entire file can fit within a single worksheet.
Another approach is to use a macro. (This is the one that I find the fastest and easiest, particularly if you need to import the same type of file quite a bit.) The macro can open the text file, read each line, and then make a determination as to whether the information in that line should be added to the worksheet or not. Here's an example that will open a file named "MyCSVFile.txt" and then stick the data into a new worksheet starting at the first row.
Sub ReadMyFile() Dim R As Integer Dim C As Integer Dim sDelim As String Dim sRaw As String Dim ReadArray() As String sDelim = "," ' Set to vbTab if tab-delimited file Worksheets.Add Open "myCSVFile.txt" For Input As #1 R = 1 Do While Not EOF(1) Line Input #1, sRaw ReadArray() = Split(sRaw, sDelim, 20, vbTextCompare) If ReadArray(4) = "y" Then For C = 0 To UBound(ReadArray) Cells(R, C + 1).Value = ReadArray(C) Next C R = R + 1 End If Loop Close #1 End Sub
To use the macro, simply change the name of the file to match the file you want to process. You'll also want to modify the sDelim variable to make sure it matches whatever is being used as a delimiter in your records. As written, it assumes the delimiter is a comma (which it would be in a CSV file), but you could change it to vbTab if you are actually working with a tab-delimted file. After the macro is complete, only those records with a single, lowercase "y" character are in the new worksheet.
Another approach is to use the Power Query feature of Excel. This is a free add-in, from Microsoft, that is available for some variations of Excel 2010 and Excel 2013. You can download (and find out which variations are supported) at this location:
http://www.microsoft.com/en-us/download/details.aspx?id=39379
If you are using Excel 2016, then Power Query is built into the program. If you have Power Query installed or available in your version of Excel and that version of Excel happens to be Excel 2010 or Excel 2013, then follow these steps:
If you are using Excel 2016 or a later version, the steps are a bit different:
At this point—regardless of the version of Excel you are using—you can use the controls to specify a query (meaning, setting up a definition of which records should be imported). When you click Close and Load, the records are retrieved from the file, and the query can be saved for future use.
A fourth approach is to use Microsoft Query. To do so, you'll need to follow this very lengthly series of steps. (Nobody ever said that Microsoft wanted to make Microsoft Query easy to use, and you'll agree after you go through these steps.)
Figure 1. The Choose Data Source dialog box.
Figure 2. The Create New Data Source dialog box.
Figure 3. The Add Criteria dialog box.
Figure 4. The Import Data dialog box.
(Told you the steps were lengthly.) You can now work with the data in Excel and, if desired, use the tools on the Design tab of the ribbon to refresh the data from the CSV file.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10384) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Limiting what can be entered in a cell can be an important part of developing a worksheet that other people use. Here are ...
Discover MoreWhen setting up a worksheet for others to use, you might want to make some limitations on what can be entered in certain ...
Discover MoreWhen entering many negative values in a worksheet, you could save time if you didn't need to enter the minus sign for ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments