Import data

rollingzep

Board Regular
Joined
Nov 18, 2013
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hi,

In my Excel application, I import a .CSV file using VBA with the file dialog.
The file contains more than 100,000 rows. I need to import data which are >= 9 characters

The below should be imported

1677601850122.png


these should not be imported

1677601937883.png



TIA
 

Attachments

  • 1677601989857.png
    1677601989857.png
    3.7 KB · Views: 2

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
With out data which I can manipulate (can't manipulate data in pictures), I will give you the steps I would use.
1. Load your CSV file to Power Query (Get and Transform Data found on the Data Tab)
2. Add a column and test for length.
Power Query:
if Text.Length([Code]) <= 9 then [Code] else null
where Code is the name of the field
3. Highlight the new column and filter out Nulls
4. Close and Load to your Native Excel Spreadsheet.

Note. Based upon your note and visual, it appears that you want to keep those that are less than or equal to 9 digits while you indicate the opposite in your statement.
 
Upvote 0
With out data which I can manipulate (can't manipulate data in pictures), I will give you the steps I would use.
1. Load your CSV file to Power Query (Get and Transform Data found on the Data Tab)
2. Add a column and test for length.
Power Query:
if Text.Length([Code]) <= 9 then [Code] else null
where Code is the name of the field
3. Highlight the new column and filter out Nulls
4. Close and Load to your Native Excel Spreadsheet.

Note. Based upon your note and visual, it appears that you want to keep those that are less than or equal to 9 digits while you indicate the opposite in your statement.
Is it possible to do it using VBA?
 
Upvote 0
Is it possible to do it using VBA?
Have a look here. You should be able to adapt the solution to do what you want:
 
Upvote 0
This is what I came up when trying to adapt the code in the thread I referenced:
VBA Code:
Sub ImportData()
    Dim R As Long
    Dim C As Integer
    Dim sDelim As String
    Dim sRaw As String
    Dim ReadArray() As String
    Dim vFileName As Variant

    sDelim = ","
    
    vFileName = Application.GetOpenFilename("Comma Separated Files (*.csv),*.csv")
    
    If vFileName <> False Then
    
        Worksheets.Add
        
        Open vFileName For Input As #1
        R = 1
        Do While Not EOF(1)
            Line Input #1, sRaw
            
            ReadArray = Split(sRaw, sDelim, 35, vbTextCompare)
            
            If Len(ReadArray(0)) = 9 Then
                Cells(R, 1).Value = ReadArray(0)
                R = R + 1
            End If
            
        Loop
        Close #1
        
    End If
    
    MsgBox "Import complete!"
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,589
Messages
6,120,415
Members
448,960
Latest member
AKSMITH

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