下面這段代碼是我自己定義的一個函式,還不能用,不知道哪里出錯了,我想在程序中呼叫的時候直接寫Pipedat2D(1, 2)就能讀到對應陣列的值
Public Function Pipedat2D(ByVal i%, j%)
Open App.Path & "\def\管系配置.txt" For Input As #1
i = 1
Do Until EOF(1)
Input #1, a
t = Split(a, " ")
For j = 0 To 4
Pipedat2D(i, j) = t(j)
Next
i = i + 1
Loop
Close #1
End Function
下面是我的文本檔案的內容:
BG 艙底水管系 橡膠墊片 鍍鋅 0.3
CL 冷卻水管系 橡膠墊片 鍍鋅 0.45
CA 壓縮空氣管 橡膠墊片 磷化 1.0
LO 滑油輸送管 無石棉纖維 酸洗 0.3
FF 消防管系 橡膠墊片 鍍鋅 0.6
FT 燃油輸送管 無石棉纖維 酸洗 0.4
uj5u.com熱心網友回復:
跪求高手幫忙解答一下啊,非常感謝!uj5u.com熱心網友回復:
如果你僅僅是要直接讀出文本對應行列的值,如下(似乎你的行是基于 1 計數,列基于 0 計數的):Public Function Pipedat2D(ByVal i As String, ByVal j As String)
Dim strRow As String, strCol() As String, n As Long
Open App.Path & "\def\管系配置.txt" For Input As #1
n = 1
Do Until EOF(1)
Line Input #1, strRow
If n = i Then
strCol = Split(strRow, " ")
If j <= UBound(strCol) Then
Pipedat2D = strCol(j)
End If
Close #1
Exit Function
End If
n = n + 1
Loop
Close #1
End Function
uj5u.com熱心網友回復:
如果 txt 文本較長,每次讀取效率較低,可以首次查詢填充到真實陣列中:Public strPipedata2D() As String同樣,行的基數是 1 為起點。
Public Populated As Boolean
Public Function Get_Pipedat2D(ByVal i As Long, ByVal j As Long) As String
If Not Populated Then
Init_Pipedata_Array
End If
If i <= UBound(strPipedata2D, 1) And j <= UBound(strPipedata2D, 2) Then
Get_Pipedat2D = strPipedata2D(i, j)
End If
End Function
Public Sub Init_Pipedata_Array()
Dim r As Long, c As Long, i As Long, j As Long
Dim strRow As String, strCol() As String
Dim strTmp() As String, OK As Boolean
Open "c:\def\1ü?μ????.txt" For Input As #1
Do Until EOF(1)
Line Input #1, strRow
strCol = Split(strRow, " ")
If r = 0 Then
c = UBound(strCol)
ElseIf c > UBound(strCol) Then
MsgBox "Bad file!"
Exit Sub
End If
ReDim Preserve strTmp(c, r)
For j = 0 To c
strTmp(j, r) = strCol(j)
Next j
r = r + 1
Loop
OK = True
Close #1
If Not OK Then Exit Sub
ReDim strPipedata2D(1 To r, c)
For i = 1 To r
For j = 0 To c
strPipedata2D(i, j) = strTmp(j, i - 1)
Debug.Print strPipedata2D(i, j) & " ";
Next j
Debug.Print ""
Next i
Populated = True
End Sub
uj5u.com熱心網友回復:
Option Explicit
Dim iData()
Private Sub Form_Load()
Static n As Integer
Open "C:\1.txt" For Input As #1
Do Until EOF(1)
ReDim Preserve iData(n)
Line Input #1, iData(n)
n = n + 1
Loop
Close #1
End Sub
Public Function Pipedat2D(ByVal nRow As Long, ByVal nCol As Long) As String
Pipedat2D = Split(iData(nRow - 1), " ")(nCol - 1)
End Function
Private Sub Command1_Click()
MsgBox Pipedat2D(3, 1)
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/92466.html
標籤:VB基礎類
上一篇:請教最簡單的程式有條件跳轉
