Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7082505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:03:45+00:00 2026-05-28T07:03:45+00:00

I am looking for a VBA user-defined function that will read in the contents

  • 0

I am looking for a VBA user-defined function that will read in the contents of a .csv file and return the results as an array to populate the cells of a worksheet, i.e something like this:

   =CSV_Read("path/to/file.csv")

I have found this but looks proprietary/shareware. Does anyone know of any alternatives? Or what the best approach to rolling my own in VBA would be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:03:46+00:00Added an answer on May 28, 2026 at 7:03 am

    In your VBA code, add reference to “Microsoft Scripting Runtime”.
    Add a module, paste the following code.

    Option Explicit
    Public Function CSV_Read(ByVal path As String) As Variant
    Dim fso As Scripting.FileSystemObject
    Dim tsm As Scripting.TextStream
    
    Dim data
    
    Dim text As String
    Dim allRowsInArray As Variant
    Dim currentLineToArray As Variant
    
    Dim columnCount As Integer
    Dim columnCounter As Integer
    
    Dim rowCounter As Integer
    Dim rowCount As Integer
    
    Set fso = New Scripting.FileSystemObject
    Set tsm = fso.OpenTextFile(path, ForReading)
    
    rowCounter = 0
    If Not tsm.AtEndOfStream Then
        text = tsm.ReadAll
    
        tsm.Close
        Set tsm = Nothing
        Set fso = Nothing
    
        allRowsInArray = Split(text, vbCrLf)
        rowCount = UBound(allRowsInArray)
    
        Do While rowCounter < (rowCount + 1)
            currentLineToArray = Split(allRowsInArray(rowCounter), ",")
    
            If Not IsArray(data) Then
                columnCount = UBound(currentLineToArray)
                ReDim data(rowCount, columnCount)
            End If
    
            For columnCounter = 0 To columnCount
                data(rowCounter, columnCounter) = currentLineToArray(columnCounter)
            Next
    
            rowCounter = rowCounter + 1
        Loop
    End If
    
    CSV_Read = data
    End Function
    

    Sample CSV file data

    a,b,c,d,e,f,g
    1,2,3,4,5,6,7
    7,6,5,4,3,2,1
    q,w,e,r,t,y,u
    

    Looking at the data, it has 7 columns and 4 rows.

    Now, goto cell A1 and select the range from cell A1 to G4.
    Type in the formula (=CSV_Read("the full path to your csv file"), in cell A1 (keeping the selection as it is).

    Important:
    Press CTRL + SHIFT + ENTER, once you have finished typing in the formula. This is array formula.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for a Linux application (or Firefox extension) that will allow me to scrape
I am looking to run SQL queries using VBA code in an Excel file.
I am looking for ways to read in a PDF file with SAS. Apparently
Using VBA. My script moves a file into a directory. If that filename already
I am looking to implement a VBA trie -building algorithm that is able to
I'm looking for a decent sort implementation for arrays in VBA. A Quicksort would
I'm looking for the best way of loading formatted data in VBA. I’ve spent
Looking for an example that: Launches an EXE Waits for the EXE to finish.
Looking for C# class which wraps calls to do the following: read and write
Looking at the C# project templates in VS2008 and the offerings are WPF User

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.