求一個txt檔案中某一列資料的最小值。比如,txt檔案為a.txt,資料如下:
1,2,3,4
5,6,7,8
2,3,4,5
。。。。
學習初級水平,求指導。
比如求第三列的最小值,求寫出代碼。非常感謝。
uj5u.com熱心網友回復:
簡單的示例,檔案路徑,你在試運行時最好按“實際的完整路徑”寫。沒有進行例外處理,因此你的檔案,每行資料至少要有3個、并且第3個數不能是“不合法的數”。
如果有必要你自己加上(但估計你也不知道如何加
)。Private Sub Command1_Click()
Dim strText As String
Dim w As Long, m As Long
Open "a.txt" For Input As #1
Line Input #1, strText
w = Split(strText, ",")(2)
Do While Not EOF(1)
Line Input #1, strText
If (strText = "") Then Exit Do
m = Split(strText, ",")(2)
If (m < w) Then w = m
Loop
Close
MsgBox "最小值是:" & w, 64
End Sub
uj5u.com熱心網友回復:
Dim strLine As String, strTmp() As String, strMin As String
Open "a.txt" For Input As #1
Line Input #1, strLine
strTmp = Split(strLine, ",")
strMin = strTmp(2)
Do Until EOF(1)
Line Input #1, strLine
strTmp = Split(strLine, ",")
If strMin > strTmp(2) Then strMin = strTmp(2)
Loop
Close #1
MsgBox strMin
uj5u.com熱心網友回復:
If Val(strMin) > Val(strTmp(2)) Then strMin = strTmp(2) 可支持多位數字uj5u.com熱心網友回復:
一行行讀,按逗號拆分成陣列。然后取出指定列的數字,存到一個變數里,每次跟變數值比較大小,小就更新變數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/68554.html
標籤:VB基礎類
上一篇:Excel ping網址的問題。
