Relavent data from text file

Npsays

Board Regular
Joined
Dec 17, 2004
Messages
70
Hi
I need help on importing data into excel from a text file. But, before the import i need to ignore the blank lines. is it possible ? It is a huge file which has lot o f blank spaces in between which I don't need before importing
into excel .

Thanks..Np
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
better to sort after import the blank rows will go down.

Thanks .. but I need it before the upload as the file size is huge and I need only the required data. Is it possible to sort the text file in ascending order before it uploads the data on excel ?
 
Upvote 0
Hi,

try this
Code:
Option Explicit

Sub import_textfile_without_blanks()
'Erik Van Geit
'060912
'requirement: exclude blanks before import

    Dim FilePath As String
    Dim FileNumber As Integer
    Dim I As Long
    Dim Data As String
    Dim CalcSetting As Integer

FilePath = "C:\windows\desktop\temp.txt"
FileNumber = FreeFile

Open FilePath For Input As #FileNumber
I = 0

    With Application
    .ScreenUpdating = False
    CalcSetting = .Calculation
    .Calculation = xlCalculationManual
    End With

With Sheets(1)
    If FileLen(FilePath) > 0 Then
        While Not EOF(FileNumber)
            Line Input #FileNumber, Data
            If Data <> "" Then
            I = I + 1
            Cells(I, 1) = Data
            End If
        Wend
        Close FileNumber
    Else
      MsgBox "No Data found in the textfile", 48, "TITLE"
      Exit Sub
    End If
    
End With
    
    With Application
    .ScreenUpdating = False
    .Calculation = CalcSetting
    End With

End Sub
storing everything into an array before import, will be faster, but you would be limited to about 1800 characters per line
kind regards,
Erik
 
Upvote 0
Thanks Eric ! Just Perfect!
last , one small addition ...... there are some special characters which I need to ignore . They appear in the begining of the string.. any ways around that too ? .. great help Thanks !
 
Upvote 0
always same amount of characters ?
do you know which characters ?

check it out: report the numbers you get
after I = I + 1 insert this
Code:
Dim ii

Stop
'continue with function key F8 till you know enough
'then press stop-button
For ii = 1 To Len(Data)
MsgBox Asc(Mid(Data, i, 1))
Next ii
best regards,
Erik
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,249
Members
448,556
Latest member
peterhess2002

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