Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
160 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 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

670 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