Dim cnt As Long Const c列 = 3 Const d列 = 4 Const h列 = 8 Const i列 = 9 Sub open_dir_and_run() Dim Folder As Object Dim strPath As String Dim MaxRow As Long Dim Dir_path As String If Cells(6, c列).Value <> "" Then MaxRow = Cells(Rows.Count, c列).End(xlUp).Row cnt = MaxRow Else cnt = 5 End If 'パスを変更して下さい。 'strPath = "C:\tools\base\ログファイルサンプル 1 file" Dir_path = ThisWorkbook.Path strPath = Dir_path Call re_call(strPath) End Sub Sub re_call(ByVal strPath) Dim buf As String Dim textbuf As String Dim text_cnt As Long Dim flg1 As Integer Dim re As Object Dim mc As Object Dim m As Object Dim i As Long 'ライブラリ参照は止血 Dim f As Object Dim ws As Worksheet Set ws = Sheets(1) Set ws = Worksheets(1) Set re = CreateObject("VBScript.RegExp") '書式設定を標準から文字列に設定する。肝 デバッグのなごり。 ws.Columns(c列).NumberFormatLocal = "@" ws.Columns(d列).NumberFormatLocal = "@" ws.Columns(h列).NumberFormatLocal = "@" ws.Columns(i列).NumberFormatLocal = "@" buf = Dir(strPath & "\allLogAnalyse.log") 'ターゲットのファイル名 tgt_file = strPath & "\" & buf Do While buf <> "" Open tgt_file For Input As #1 Do Until EOF(1) Line Input #1, textbuf If textbuf = "================================== 個別判定 ==================================" Then flg1 = 1 End If If flg1 = 1 Then text_cnt = text_cnt + 1 End If If text_cnt > 3 Then '正規表現 cnt = cnt + 1 re.Pattern = "(\S+)\(\S+)\s*(\S*)\s*(\S*)" re.Global = True Set mc = re.Execute(textbuf) For Each m In mc ws.Cells(cnt, c列).Value = m.SubMatches(0) ws.Cells(cnt, d列).Value = m.SubMatches(1) ws.Cells(cnt, h列).Value = m.SubMatches(2) ws.Cells(cnt, i列).Value = m.SubMatches(3) Next End If Loop Close #1 'ファイルクローズした、時点でフラグとtext_cntを初期化。 flg1 = 0 text_cnt = 0 buf = Dir() Loop With CreateObject("Scripting.FileSystemObject") For Each f In .GetFolder(strPath).SubFolders Call re_call(f.Path) Next f End With End Sub
このコーディングは VBA ツール->参照設定で正規表現のライブラリの
レ点を外しても動作するコードです。
つい最近、そういう事がVBAではできると知りました。
VBAにお詳しい方にご質問させて下さい。
一般的に、参照設定はさせないコーディングを
するほうがスタンダードなのでしょうか?
つい最近までは、ツールの参照設定でライブラリの参照設定を
するコーディングをしていました。
参照設定をしなくても良いコードなら
他のユーザにマクロを公開する際、ライブラリにレ点をつけてね
なんて言わなくてもよくなり、これはいい事だらけだな。
と感じたのですが、何かデメリットとなる事など
ございましたら、リプライして頂けると
助かります。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/17 08:41