0

我从 txt 文件中读取数据,然后将其保存到整数数组中。现在我想将该数组导出到 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

2 回答 2

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

希望这有帮助,干杯

于 2013-05-23T00:13:31.540 回答
0

你可能需要收藏

参考:自定义集合类

于 2013-04-27T09:18:53.300 回答