|
以下は、
Dir関数でファイルを取得、ひとつづつOpenして
一行づつ読み込み、検索文字列が含まれていたら、
それをセルに出力するサンプルです。
Sub Try_Find2()
ActiveSheet.UsedRange.ClearContents
SearchText "D:\(Data)", "*.txt", "あいう", ActiveSheet
End Sub
'A列にファイル名、B列に 一致行を出力
Private Sub SearchText(LookIn$, Filename$, What$, _
ws As Worksheet)
Dim f As String
Dim i As Long, Lno As Long
Dim io As Integer, flg As Boolean
Dim ss As String
If Right$(LookIn, 1) <> "\" Then LookIn = LookIn & "\"
f = Dir$(LookIn & Filename)
io = FreeFile()
Do While Len(f) > 0
Open LookIn & f For Input As io
flg = True
Lno = 0
Do Until EOF(io)
Lno = Lno + 1
Line Input #io, ss
If InStr(1, ss, What, vbTextCompare) > 0 Then
If flg Then
i = i + 1
ws.Cells(i, 1).Value = f
flg = False
End If
i = i + 1
ws.Cells(i, 2).Value = "[" & Lno & "] " & ss
End If
Loop
Close io
f = Dir$()
Loop
End Sub
|
|