Solved

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

Posted on 2014-10-15
3
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 47

Accepted Solution

by:
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) & ","
    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 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
0

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

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.
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.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Viewers will learn the different options available in the Backstage view in Excel 2013.

752 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