2016-11-05 7 views
0

テキストファイルで大きな文字列を検索するためにvbaマクロを使用しています。 私はテキストファイルを読んで、比較するテキスト(セルの1つに保存されています)を読んでください。その後、CRLFをCRに置き換えます(保存されたテキストにはCRLFが含まれていないため)。次に比較する。ファイルサイズが小さい場合は正常に動作します。しかし、ファイルサイズが大きいときにエラーを投げます(約50 KBは問題ありません)。 ファイルの最大サイズを推測しますか?VBA Excelマクロエラー:1004は、WorkbooFunctionクラスの代替プロパティを取得できません。

コードの下の部分がエラー投げている

Open LogFilePath For Input As #iFile 
    strFileContent = Input(LOF(iFile), iFile) 
    Close #iFile 
    strFileContent = Application.WorksheetFunction.Substitute(strFileContent, vbCrLf, "") 
    strSearch = Application.WorksheetFunction.Substitute(strSearch, vbLf, "") 
    strFileContent = Application.WorksheetFunction.Substitute(strFileContent, vbTab, " ") 
    If InStr(1, strFileContent, strSearch, vbBinaryCompare) > 0 Then 
    SearchTextFile = "success" 
    Else 
    SearchTextFile = "failed" 
    End If 

enter image description here

任意の推測や提案してください。

+0

私はちょうどテストしたところ、最初のパラメータ(とおそらく他のパラメータ)のサイズには32,767バイトの制限があります。私はあなたが50Kbのファイルを稼働させることに成功したことに驚いています。 @AndyWは答えで示唆しているように、 'Application.WorksheetFunction.Substitute'ではなくVBAの' Replace'関数を使います。 – YowE3K

+0

セルに含めることができる合計文字数:32,767文字。だから私は「代理人」も限られていると思う。 [Excelの仕様と制限]をご覧ください(https://support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3) –

+0

ありがとう返信のために。サイズが200KBを超えるファイルを試していました。今私は32767バイトと32768バイトでチェックしました。 32768はエラーを示しましたが、32767はエラーを示さなかった – user3710272

答えて

2

エラーは、置換プロパティを参照し、Application.WorksheetFunction.Substituteを使用していることに注意してください。

私は常にVBAで同じ方法で動作するREPLACE機能を使用する傾向があります。

大規模な.txtファイル(20,000行/ 30MB)を処理しても問題はありません。

+0

ありがとうございます。あなたが言いましたように私はReplaceを試しました、そして今はうまくいきます – user3710272

1

Substituteは、例えば、セル式で必要に応じてReplaceを使用してコードスニペットに従ってください。

Private Sub CommandButton1_Click() 

Dim LogFilePath As String 
Dim ifile As Integer 

ifile = 1 

LogFilePath = "D:/_working/application-log-file-small.txt" 
strSearch = "Temp Path  :" 

Open LogFilePath For Input As #ifile 
    strFileContent = Input(LOF(ifile), ifile) 
    Close #ifile 
    '--- Show len of file content string ----- 
    MsgBox (Len(strFileContent)) 

    strFileContent = Replace(strFileContent, vbCrLf, "") 
    strSearch = Replace(strSearch, vbLf, "") 
    strFileContent = Replace(strFileContent, vbTab, " ") 

    If InStr(1, strFileContent, strSearch, vbBinaryCompare) > 0 Then 
    SearchTextFile = "success" 
    MsgBox ("success") 
    Else 
    SearchTextFile = "failed" 
    MsgBox ("failed") 
    End If 
End Sub 
+0

ありがとうございました。 replace関数がうまくいき、結果を得ました。 – user3710272

関連する問題