Sunday, June 17, 2012

Membaca File Dan Memasukannya Ke Dalam Array

Option Explicit

Private Sub Command1_Click()

Dim L As Long
Dim MyArray() As String

' Load file into string array
FileToArray "C:\TEST.txt", MyArray

' Reverse array contents
ReverseStrArray MyArray

' show result in immediate window
For L = 0 To UBound(MyArray)
Debug.Print MyArray(L)
Next L

End Sub

Private Sub FileToArray(ByVal sPath As String, ByRef sArray() As String)
Dim ff As Integer
ff = FreeFile
On Error GoTo Fini
Open sPath For Input As #ff
sArray = Split(Input(LOF(ff), ff), vbCrLf)
Fini:
Close #ff
End Sub

Private Sub ReverseStrArray(ByRef sArray() As String)
Dim ubnd As Long, lbnd As Long, x As Long
Dim sTmp As String
ubnd = UBound(sArray)
lbnd = LBound(sArray)
For x = lbnd To ((ubnd - lbnd - 1) \ 2)
sTmp = sArray(lbnd + x)
sArray(lbnd + x) = sArray(ubnd - x)
sArray(ubnd - x) = sTmp
Next x
End Sub