+ Reply to Thread
Results 1 to 4 of 4

Counting rows of data in a closed csv or txt file

  1. #1
    Registered User
    Join Date
    04-27-2005
    Posts
    2

    Smile Counting rows of data in a closed csv or txt file

    Is it possible to have VBA determine the number of rows of data in a csv or text file without opening the file first? Here is why ...

    If Rows > 65,536 Then
    Run Macro_A
    Else
    Run Macro_B
    End If

    Thanks.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Pavel,

    Your reason for not opening the file first must mean that you are using the file Wizard. VBA allows you to Open, Read, Write, and Close files. These is all done in a buffer area separate from Excel. The VBA code gives complete control of what happens with the file's data. Here is a macro to count the Lines in either a CSV or TXT file.

    Please Login or Register  to view this content.
    Copy and Paste this code into a VBA Module.

    To Use the Macro:
    LinesOfText = CountDataLines "C:\Test File.txt"

    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    04-27-2005
    Posts
    2

    Post

    Thanks Leith.

    I'm a little confused by:

    To Use the Macro:
    LinesOfText = CountDataLines "C:\Test File.txt"

    Can you explain how I make this function a macro?

    Thanks.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Pavel,

    The function is the macro. A macro is simply a collection of simple commands design to perform a specific task.

    Variable(Long) = CountDataLines "Name of file to Open"

    The function or macro CountDataLines opens the file and counts the lines of data in the file. The function assigns this count to the Variable.

    Hope this helps,
    Leith Ross

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1