2016-02-12 85 views

回答

1

此外,我們可以做到這一點沒有循環:

Sub Test() 

Dim FSO As Object, MyFile As Object 
Dim FileName As String, Arr As Variant 

FileName = "C:\Test\Test.txt" ' change this to your text file full name 
Set FSO = CreateObject("Scripting.FileSystemObject") 
Set MyFile = FSO.OpenTextFile(FileName, 1) 
Arr = Split(MyFile.ReadAll, vbNewLine) ' Arr is zero-based array 


'For test 
'Fill column A from this Array Arr 

Range("A1").Resize(UBound(Arr) + 1, 1).Value = Application.Transpose(Arr) 

End Sub 
2

只需將文本文件的路徑保存到名爲FilePath的變量中並運行此代碼塊即可。

Dim arInt(1 to 1000) as Integer 
Dim intCount as Integer 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
With = objFSO.OpenTextFile(FilePath, ForReading) 
intCount = 1 
Do While .EOF = False AND intCount < 1001 
    arInt(intCount) = Val(.readline) 
    intCount = intCount + 1 
Loop 

Val函數將字符串轉換爲數字值,然後vba將它轉換爲一個整數給你。 之後,數組中有100個int值。 一旦文件競爭或者數組中有1000個值,代碼就會停止。