如何用vba實作查詢檔案里的字串并把查詢到的字串所在檔案的行數,在哪句列出出來,查詢條件不寫在代碼里
uj5u.com熱心網友回復:
Input讀取檔案Instr查找字串
具體用法google下。
uj5u.com熱心網友回復:
因為win10裝不了vb6,所以用vbs寫了個腳本,你可以參考一下:Set fso = createobject("scripting.filesystemobject")
curdir = fso.getparentfoldername(wscript.scriptfullname)
path = fso.buildpath(curdir,"test.txt")
target = inputbox("請輸入要查找的內容:")
Set stream = fso.opentextfile(path,1,False)
content = stream.readall
stream.close
arrLines = Split(content,vbcrlf)
For i = lbound(arrlines) To ubound(arrlines)
If InStr(arrlines(i),target) > 0 Then
msgbox "第" & (i+1) & "行: " & arrlines(i)
End If
Next
msgbox "查找完畢!"
uj5u.com熱心網友回復:
Function TT(strb)
Dim aa, bb, cc
Dim strFile As String
Dim intFile As Integer
Dim strData As String
strFile = "d:\1.txt"
intFile = FreeFile
Open strFile For Input As intFile
stra = StrConv(InputB(FileLen(strFile), intFile), vbUnicode)
Debug.Print stra
Close intFile
'--------------
aa = InStr(1, stra, strb)
bb = Mid(stra, 1, aa)
cc = Len(Replace(bb, vbCrLf, ""))
Debug.Print (aa - cc) \ 2 + 1
TT = (aa - cc) \ 2 + 1
End Function
uj5u.com熱心網友回復:
Function TT(strb, path)
Dim aa, bb, cc, intFile, strData
intFile = FreeFile
Open path For Input As intFile
stra = StrConv(InputB(FileLen(path), intFile), vbUnicode)
Close intFile
aa = InStr(1, stra, strb)
bb = Mid(stra, 1, aa)
cc = Len(Replace(bb, vbCrLf, ""))
TT = (aa - cc) \ 2 + 1
End Function
uj5u.com熱心網友回復:
vb6,錯了uj5u.com熱心網友回復:
'Search 函式 FilePath 檔案的絕對路徑 SearchStr 要查找的字串
'如果找到字串,回傳字串所在位置,否則回傳空字串
'至于檔案絕對路徑的正確與否,是否為空,自己去檢查
Public Function Search(FilePath As String, SearchStr As String) As String
Dim StrData As String '定義臨時變數
Dim Row As Integer '定義行
Dim Col As Integer '定義列
Open FilePath For Input As #1 '打開檔案
Do Until (EOF(1)) '直到回圈(直到檔案末尾)
Row = Row + 1 '行計數+1
Line Input #1, StrData '一行一行讀入臨時變數
If InStr(StrData, SearchStr) <> 0 Then '如果找到字串
Col = InStr(StrData, SearchStr) '將字串所在位置賦值給Col
Exit Do '退出回圈
End If
Loop
If Col <> 0 Then '如果找到字串
Search = CStr(Row) & "," & CStr(Col) '以“行,列”的格式回傳字串所在位置,便于后期加工
End If
Close #1 '關閉檔案
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/38701.html
標籤:VBA
上一篇:齒輪自動化建模編程
