0

Sub ReadEntireFileAndPlaceOnWorksheet()
  Dim X As Long, FileNum As Long, TotalFile As String, FileName As String, Result As Variant, Lines() As String
  FileName = "C:\Users\Mohamed samatar.DSSE-EMEA\Documents\EQVL\Test\WHVP113_140910_TTinsug_TT_299Data_PUoff_WOT-TakeOff_NotKickDown_gearD_FelLambda.dat"
  FileNum = FreeFile
  Open FileName For Binary As #FileNum
    TotalFile = Space(LOF(FileNum))
    Get #FileNum, , TotalFile
  Close #FileNum
  Lines = Split(TotalFile, vbNewLine)
  ReDim Result(1 To UBound(Lines) + 1, 1 To 1)
  For X = 1 To UBound(Result)
    Result(X, 1) = Lines(X - 1)
  Next
  Range("A1").Resize(UBound(Result)) = Result
End Sub​

I have some files in the .dat format,these files contain some valuable information however they can be quite big, trying to open each file in notepad and extracting the information I need is not efficient at all, as it takes notepad a long time to open each file. I have come across this Binary Access Read function which apparently opens large files and allows you to read them very quickly. I was wondering how to find/ get specific lines of information, can you use a similar function to say the find function or is there another way to get information this is what I have so far. All it does is tell me the file type, essentially I want to be able to search for specific string value, or If I could just dump all the text in EXCEL and sort from there any guidance is useful.

3
  • Check HERE for basic I/O functions, you can read the file line by line and then use basic string functions or RegEx to find the data you're looking for, or read the entire file into an array and iterate that, etc. Commented Feb 6, 2015 at 2:38
  • Thanks for the swift response, will have a look! Commented Feb 6, 2015 at 2:43
  • Hi David, any chance you can point me in the right direction, I have just gone through the document and maybe an example of the reading the file into an array? If I can get it to read into an array I can possible dump it in excel and do the search there where I have a little more experience. Little meaning very little. Thanks? Commented Feb 6, 2015 at 3:11

1 Answer 1

2

Here are two examples. The first one reads each line in the file and prints to a worksheet beginning in cell A1, and then down to A2, A3, etc., one for each line:

Option Explicit
Sub testReadLine()
    Dim filename As String
    Dim FF As Integer
    Dim line As String
    Dim i as Long

    filename = "C:\yourfilename.txt" '### MODIFY AS NEEDED

    FF = FreeFile
    Open filename For Input As FF
    Do While Not EOF(FF)
        Line Input #FF, line

        Range("A1").Offset(i).Value = i
        i = i + 1
    Loop
    Close FF

End Sub

The second example reads the entire file into a string variable, and then use the Split function to convert that string to an array of string, based on the line delimiter (assumed to be the vbCRLF).

This one should be faster, and of course once you have the data in Excel you can use your normal excel functionality like the "Find", etc.

Option Explicit
Option Base 0
Sub testReadAll()

    Dim filename As String
    Dim FF As Integer
    Dim text As String
    Dim buffer As Long
    Dim txtArray() As String

    filename = "C:\yourfilename.txt"  '### MODIFY AS NEEDED

    FF = FreeFile
    Open filename For Input As FF
    buffer = LOF(FF)
    text = Input(buffer, FF)
    Close FF

    'Put the text into an array, split by the LineFeed character:
    txtArray = Split(text, vbCrLf)

    'Print to the worksheet:
    Range("A1").Resize(Ubound(txtArray)).Value = Application.Transpose(txtArray)

End Sub
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you very much for the response, its currently just outputting numbers I should have explained the file format is .dat file and I have seen some places where I potentially need to do a conversion from binary to normal text? is this the case, how does it read it out as it just seems to be outputting numbers 0 to 4 although there is about 17MB of data in the file?
That sounds like a new question, but there are String Conversion functions which can convert a byte array to a string, etc. I'm sorry, but it is midnight here and I don't intend to entertain follow-up questions at the present time. Please ask a new question so that others may be able to assist you.
in hindsight it does seem to work there was a typo in the first code only issue its getting some errors and not finishing running the script, I will try to fix it from here anyways thanks.
Cheers. I'll be online tomorrow, if you do run in to trouble I can try to help, I just wont' be able to assist immediately today.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.