Write array of Long to a file, and read it back to array Long VBA

Posted on 2014-10-15
Last Modified: 2014-10-16
Hi experts,

A VBA problem here:

I have declared and initialised the array below:

Dim arr1() As Long

I'd like to do two things:
1. Write it to a file
2. Initialise the array below with the file contents.

Dim arr2() As Long

I will accept solution / provide max points if:
1. Code can be copy/paste to any VBA macro and will work
2. arr2 is an array of Long
3. arr1 and arr2 have exactly the same data

Question by:Juan Villanueva
  • 2
LVL 47

Accepted Solution

Martin Liss earned 500 total points
ID: 40383393
Here are two macros. The first which writes the data and the second that reads it.

Sub WriteArrayToDisk()
Dim FF As Integer
Dim arr1() As Long
Dim lngIndex As Long
Dim strValues As String

ReDim arr1(4) As Long

arr1(0) = 12345
arr1(1) = 54321
arr1(2) = 999999
arr1(3) = 88888
arr1(4) = 7777

For lngIndex = LBound(arr1) To UBound(arr1)
    If lngIndex < UBound(arr1) Then
        strValues = strValues & arr1(lngIndex) & ","
        strValues = strValues & arr1(lngIndex)
    End If

FF = FreeFile

Open "C:\temp\MyFile.txt" For Output As #FF
Print #FF, strValues

End Sub
Sub ReadArrayFromDisk()
Dim FF As Integer
Dim arr2() As Long
Dim strParts() As String
Dim strValues As String
Dim lngIndex As Long

FF = FreeFile

Open "C:\temp\MyFile.txt" For Input As #FF
Line Input #1, strValues

strParts = Split(strValues, ",")
ReDim arr2(UBound(strParts))

For lngIndex = 0 To UBound(strParts)
    arr2(lngIndex) = strParts(lngIndex)

' Show the results
For lngIndex = 0 To UBound(arr2)
    Debug.Print arr2(lngIndex)

End Sub

Open in new window


Author Closing Comment

by:Juan Villanueva
ID: 40383620
You are a legend. Thanks!
LVL 47

Expert Comment

by:Martin Liss
ID: 40384306
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question