Creating an array

E

Eric

Hi,

I am trying to create a macro that store my text file data
into an array and open it in excel file.

My text file data looks like this:

Column1 Column2
Row1 Name1 TextLength1
Row2 Name2 TextLength2
Row3 Name3 TextLength3
. . .
. . .
. . .

Assuming rows is unlimited, How could I store this data
into an array?

I would appreciate anyone's input, tips or suggestions.

Regards
Eric
 
M

mct

But why in an Array? I think it's easier to read the data from the text
file, and pump it straight into the worksheet. Like this.

Option Explicit
Public Sub LoadTextFileData()
Dim wb As Workbook
Dim ws As Worksheet
Dim txtFile As String
Dim fileLineTxt As String
Dim fileLineLen As Integer
Dim rowNum As Integer
Dim colNum As Integer
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
txtFile = "C:\textfile.txt"
rowNum = 1
colNum = 1
With ws
Open txtFile For Input As #1
Do While Not EOF(1)
Line Input #1, fileLineTxt
fileLineLen = Len(fileLineTxt)
If fileLineTxt <> "" Then
ws.Cells(rowNum, colNum).Value = fileLineTxt
ws.Cells(rowNum, (colNum + 1)).Value = fileLineLen
End If
rowNum = rowNum + 1
Loop
Close #1
End With
End Sub

*Filling an array can be done in the IF-statement.

Hans
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top