Selectively Import Rows into Excel from a CSV file based on criteria

brawnystaff

Board Regular
Joined
Aug 9, 2012
Messages
104
Office Version
  1. 365
I am looking to import certain information from a CSV file that is 4GB in size (first row is the header row). Rather than import the entire CSV file, I only want to import certain rows that meet a certain criteria. In this case, all rows that contain the value "AZ" in a column labeled "State Name". I am working in Excel 2010. Any ideas how to create a macro to do it? Thanks.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
open the file for input and read every line, but only save the ones you want.

Code:
    Open MyDataFileName for input as #1
    while not eof(1)
        input #1, my list of fields, etc,...   , MyState, etc
        if MyState = "AZ" then
            CurrentDataRow= CurrentDataRow + 1
            'save the relevant data to CurrentDataRow
        end if
    Wend
    close 1
 
Upvote 0
Could give more detail on the macro?

I am looking to import all fields (columns) in the rows that contain the text I want.
 
Last edited:
Upvote 0
i need to see a sample of the textfile. at least the header row and first row of data before i can give any further advice.
 
Upvote 0
Trying to import selectively import values from a CSV file. A smaller version of the CSV can be found here:

http://nppes.viva-it.com/NPPES_Data_Dissemination_100812_101412_Weekly.zip


I am been working on this for a while. I have some code below that works. The only problem is that the first column (column A) has the values in quotes. Any ideas why? Thanks.

Code:
Sub csvimport()
Dim FileName1 As Variant
Dim MyState As String
Dim J As Integer
Dim X As Integer
Dim arow As Integer
Dim r As Integer
Dim arr As Variant
Dim S As String
Dim State As String
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
    FileName1 = Application.GetOpenFilename("<wbr>Csv Files (*.csv), *.csv")
    MyState = Application.InputBox("Enter Keywords")
    On Error GoTo err:
    X = 1
    arow = 1
     Open FileName1 For Input As #1
    Do While Not EOF(1)
      r = 0
      S = ""
      For J = 1 To 329
         Input #1, State
         S = S & State & "|"
         If State = MyState Then r = J
      Next
      If r > 0 Or arow = 1 Then
        arr = Split(S, "|")
        Range(Cells(X, 1), Cells(X, 330)).Value = arr
        X = X + 1
      End If
      arow = arow + 1
    Loop
 
err:
    Close 1
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,157
Messages
6,123,340
Members
449,097
Latest member
thnirmitha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top