Results 1 to 2 of 2

Thread: Read values from an array into Excell or a .txt file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2000
    Posts
    31
    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.

  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658

    Thumbs up Give this a whirl

    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
    Iain, thats with an i by the way!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width