Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-10-15
3
Medium Priority
?
163 Views
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

Best,
Blueoris
0
Comment
Question by:Juan Villanueva
  • 2
3 Comments
 
LVL 49

Accepted Solution

by:
Martin Liss earned 2000 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) & ","
    Else
        strValues = strValues & arr1(lngIndex)
    End If
Next

FF = FreeFile

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

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
Close

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

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

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

End Sub

Open in new window

0
 

Author Closing Comment

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

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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

926 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