2012-04-23 161 views
0

我從txt文件中讀取數據,然後將其保存到整數數組中。現在我想將該數組導出到Excel表格中,但我不知道結果爲什麼不正確。請幫幫我。如何在Excel中將數字數組寫入Excel中Excel

Input #1, n 

'Now construct the (n x n) matrix that will hold the problem and read in the problem from the input file 
ReDim ProbMatrix(1 To n, 1 To n) As Integer 

Dim someNumber As Integer 
Dim startPosition As Long 
Dim endPosition As Long 
Dim temp As String 

Do While Not (EOF(1)) 
    startPosition = Seek(1) '// capture the current file position' 
    Line Input #1, temp  '// read an entire line' 
    endPosition = Seek(1) '// determine the end-of-line file position' 
    Seek 1, startPosition '// jump back to the beginning of the line' 

    '// read numbers from the file until the end of the current line' 
    For i = 1 To n 
     For j = 1 To n 
      Do While Not (EOF(1)) And (Seek(1) < endPosition) 
       Input #1, someNumber 
       ProbMatrix(i, j) = someNumber 
       MsgBox ProbMatrix(i, j) 
      Loop 
     Next j 
    Next i   
Loop  
Close #1   

Dim NewProbMatrix() As Integer 
Dim act As Action 
NewProbMatrix = makeInitialSolution(ProbMatrix, n) 
Cells(1, 1) = n 
For i = 1 To n 
    For j = 1 To n 
     Cells(i + 1, j) = ProbMatrix(i, j) 
    Next 
Next 

Application.Save 
Workbooks.Close 
MsgBox ("Finished") 
+4

你是什麼意思「的結果是不正確的'? – markblandford 2012-04-23 10:52:16

回答

0

試試這個

With Thisworkbook.Activesheet 
.Cells(1, 1).value = n 
For i = 1 To n 
    For j = 1 To n 
     .Cells(i + 1, j).value = ProbMatrix(i, j) 
    Next 
Next 
End With 

希望這是有幫助的,乾杯