Consulting

Results 1 to 8 of 8

Thread: Reading 1st row of a CSV file into Array without opening it

  1. #1

    Reading 1st row of a CSV file into Array without opening it

    Hi again,

    Let say I have a CSV file saved in disk. Now I want to read the 1st row of that file without opening it and import that data in some fixed Array. And I want to do that programmatically using VBA.

    Could you please provide some pointer on how to achieve that?

    Thanks for your time.

  2. #2
    VBAX Regular
    Joined
    Jul 2013
    Posts
    56
    Location
    Here's a start:

    Private Sub CommandButton1_Click()
        Dim z, YourFile As String
        YourFile = "C:\yourchoice.csv"
            With GetObject(YourFile).Sheets(1)
                z = .Range(.Cells(1, 1), .Cells(1, .Cells(1, .Columns.Count).End(xlToLeft).Column))
                .Parent.Close
            End With
     End Sub

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Quote Originally Posted by volabos View Post
    Hi again,

    Let say I have a CSV file saved in disk. Now I want to read the 1st row of that file without opening it and import that data in some fixed Array. And I want to do that programmatically using VBA.

    Could you please provide some pointer on how to achieve that?

    Thanks for your time.

    You'll have to open it somehow

    You can open it as file without importing it into Excel as a worksheet
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub Test()
    Dim fso, f, txt, t
    
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.opentextfile("C:\VBAX\Data1.csv")
    txt = f.readline
    'To check
    t = Split(txt, ",")
    Cells(1, 1).Resize(, UBound(t) + 1) = t
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    You can't drive a car without entering it.

  6. #6
    VBAX Newbie
    Joined
    Jul 2018
    Posts
    1
    Location
    Your response made my day - just registered to reply to you snb

  7. #7
    Quote Originally Posted by volabos View Post
    Hi again,

    Let say I have a CSV file saved in disk. Now I want to read the 1st row of that file without opening it and import that data in some fixed Array. And I want to do that programmatically using VBA.

    Could you please provide some pointer on how to achieve that?

    Thanks for your time.
    I don't know whether this will help, but you can use start recording macro then go to Data tab and use get data from Text/CSV, this will generate a code which can be used to read from a csv file without opening. However it will paste the data in your worksheet rather than in an array.

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Welcome to the forum and thanks for the reply

    However, since the question was more than 2 years old, I don't think the information will be helpful to the original requester

    It's always a good idea to check the date to make sure that your response is still relevant before you take the time and effort to respond
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

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