| 
    
     |  | 皆さん、こんばんは。 
 沢山アドバイス投稿があったでお任せしようかなあ
 と思いましたが、正規表現の箇所を少し汎用的にしたので
 投稿します。
 
 新規ブックの標準モジュールに
 
 '============================================================================
 Sub main()
 Const infile = "infile.txt"
 Const otfile = "infile.tmp"
 Dim s As String
 Dim intxt As Object
 Dim outtxt As Object
 Dim fso As Object
 Set fso = CreateObject("scripting.filesystemobject")
 '**************************************************************************
 
 Call mk_sample_txt(ThisWorkbook.Path & "\" & infile, fso)
 MsgBox infile & "をサンプルファイルとして、作成しました"
 Set intxt = fso.OpenTextFile(ThisWorkbook.Path & "\" & infile)
 MsgBox "ファイルの中身は" & vbCrLf & vbCrLf & intxt.ReadAll & _
 vbCrLf & vbCrLf & "です"
 intxt.Close
 
 '****************************************************************************
 MsgBox infile & "の編集を開始します"
 
 Set intxt = fso.OpenTextFile(ThisWorkbook.Path & "\" & infile)
 Set outtxt = fso.CreateTextFile(ThisWorkbook.Path & "\" & otfile, True)
 Do Until intxt.AtEndOfLine = True
 outtxt.WriteLine myreplace(intxt.ReadLine, "^CREATE TABLE ""[^""]+", "P", 1)
 Loop
 intxt.Close
 outtxt.Close
 fso.DeleteFile ThisWorkbook.Path & "\" & infile
 fso.GetFile(ThisWorkbook.Path & "\" & otfile).Name = infile
 '**************************************************************************
 
 MsgBox "新しい" & infile & "ファイルを作成しました"
 Set intxt = fso.OpenTextFile(ThisWorkbook.Path & "\" & infile)
 MsgBox "ファイルの中身は" & vbCrLf & vbCrLf & intxt.ReadAll & _
 vbCrLf & vbCrLf & "です" & vbCrLf & _
 "○○○○の後に「P」を追加されていますね!!"
 
 intxt.Close
 Set intxt = Nothing
 Set outtxt = Nothing
 Set fso = Nothing
 End Sub
 '=================================================================
 Function myreplace(mystr As Variant, pat As Variant, rstr As Variant, Optional rtype As Long = 0) As Variant
 '  機能 : 文字列mstrから、patで指定された文字列を見つけ出し、rtypeの指示により、
 '      文字列rstrで置換したり、追加した文字列を返す
 '  input : mstr  探索対象文字列、pat メタ文字列、 rstr 置換または、追加文字列
 '      rtype 0--patで取得した文字列をrstrで置換する
 '         1--patで取得した文字列にrstrを追加する
 '  output:myreplace-mstrの編集後の文字列
 Dim matches As Object
 Dim mm As Object
 myreplace = mystr
 With CreateObject("VBScript.RegExp")
 .Pattern = pat
 .IgnoreCase = True
 .Global = True
 Set matches = .Execute(mystr)
 For Each mm In matches
 .Pattern = mm.Value
 If rtype = 0 Then
 myreplace = .Replace(mystr, rstr)
 Else
 myreplace = .Replace(mystr, mm.Value & rstr)
 End If
 Next
 End With
 End Function
 '============================================================================
 Sub mk_sample_txt(ByVal flnm As Variant, ByVal fso As Object)
 Dim txtstrm As Object
 Set txtstrm = fso.CreateTextFile(flnm, True)
 txtstrm.WriteLine String(16, "あ")
 txtstrm.WriteLine String(29, "A")
 txtstrm.WriteLine "CREATE TABLE ""○○○○"" ("
 txtstrm.WriteLine "CREATE TABLE ""BBBBBB"" ("
 txtstrm.WriteLine String(17, "い")
 txtstrm.Close
 Set txtstrm = Nothing
 End Sub
 
 
 として、一度保存した後に
 
 mainを実行してみてください。
 
 サンプルテキストファイルとして 上記のマクロを含むブックと同じフォルダに
 
 infile.txtとして、
 
 ああああああああああああああああ
 AAAAAAAAAAAAAAAAAAAAAAAAAAAAA
 CREATE TABLE "○○○○" (
 CREATE TABLE "BBBBBB" (
 いいいいいいいいいいいいいいいいい
 
 というデータを作成します。
 
 このデータを
 
 ああああああああああああああああ
 AAAAAAAAAAAAAAAAAAAAAAAAAAAAA
 CREATE TABLE "○○○○P" (
 CREATE TABLE "BBBBBBP" (
 いいいいいいいいいいいいいいいいい
 
 に変更しています。
 
 前にもどこかで記述しましたが、
 文字列の編集は楽しみながらやりましょう!!
 色んなやりかたがありますから・・・。
 
 
 試してみてください。
 
 
 |  |