Hi
I have a 2d array and wish to read its values into
1. an MS Excell file and
2. a .txt file
How can I do this?
Thanks.
Printable View
Hi
I have a 2d array and wish to read its values into
1. an MS Excell file and
2. a .txt file
How can I do this?
Thanks.
Code:Option Explicit
Dim myArray(100, 10) As String
Private Sub writeExcel()
Dim xlApp As New Excel.Application
Dim xlwb As Excel.Workbook
Dim xlws As Excel.Worksheet
Dim i As Integer, j As Integer
'create a new excel workbook
Set xlwb = xlApp.Workbooks.Add
'get the first sheet
Set xlws = xlwb.Sheets(1)
'loop to fill the sheet
For i = 1 To 100
For j = 1 To 10
xlws.Cells(i, j) = myArray(i, j)
Next j
Next i
xlApp.Visible = True
End Sub
Private Sub Command1_Click()
writeExcel
End Sub
Private Sub writeFile(strFile As String)
Dim i As Integer, j As Integer
Dim strTemp As String
'open the file for output
Open strFile For Output As #1
'loop to fill the file
For i = 1 To 100
For j = 1 To 10
'build a string
strTemp = strTemp & "," & myArray(i, j)
Next j
'remove the leading comma
strTemp = Right$(strTemp, Len(strTemp) - 1)
'print the line to the file
Print #1, strTemp
strTemp = ""
Next i
Close #1
End Sub
Private Sub Command2_Click()
writeFile "d:\test.txt"
MsgBox "Done!"
End Sub
Private Sub Form_Load()
Dim i As Integer, j As Integer
For i = 1 To 100
For j = 1 To 10
myArray(i, j) = i & ":" & j
Next j
Next i
End Sub