2016-09-12 20 views
1

ファイルが存在するかどうかをチェックする関数があります。それが真であれば、それは与えられたセルから値を引き出します。excel:FileExistsの場合、指定された値がファイル内のxである場合、指定されたセルからデータを取り出します。

問題は、このコードを実行するとダイヤルボックスが表示され続けることです。私はFileExists関数に

Application.DisplayAlerts = False 

を挿入しましたが、まだダイアログボックスが表示されています。

私の機能:Imは式ボックスに挿入何

Function FileExists(FilePath As String) As Boolean 

    Application.DisplayAlerts = False 

'Step 1: Declare your variables. 
    Dim FileName As String 

'Step 2: Use the Dir function to get the file name 
    FileName = Dir(FilePath) 

'Step 3: If file exists, return True else False 
    If FileName <> "" Then FileExists = True _ 
    Else: FileExists = False 

End Function 

Sub FileExits() 

End Sub 

=IF(FileExists("path\filename.xls"),IF('path\[filename.xls]Sheet'!D$13="16m8", 'path\[filename.xls]Sheet'!D40, ""),"false") 

はここから続けhttps://stackoverflow.com/questions/39452159/excel-pull-data-from-one-excel-file-to-another-if-the-file-exists

+0

どのようなダイアログボックスが表示されますか? –

+0

ディレクトリ/ファイルへのパス。しかし、ファイルが存在するので、それがなぜそれをスローするのかはわかりません。 –

+1

...これは、これらが存在するデフォルトオプション(例えば、「閉じる」には保存しない)に依存しますが、「使用するように指定したファイルが見つかりません」というデフォルトオプションはありません。 – pnuts

答えて

1

を私はOPの私のコメントで少し間違っていました。はい、あなたはこのような方法で値を取得できますが、Excelのブール論理が短絡しないため、ファイルが存在しない場合に表示されるダイアログが表示されます。

名前付きファイルが存在しない場合、Excelは完全な式を評価する必要があり、は指定されたファイルが存在しない場合はと評価できません。 @pnutsとして

IF('path\[filename.xls]Sheet'!D$13="16m8", 'path\[filename.xls]Sheet'!D40, "") 

Application.DisplayAlerts = False

[Excelが](閉じるために保存しないなど)これらが存在するデフォルトのオプションに頼るだろうが、私ができる」のデフォルトのオプションがない場合、上記の言及します指定されたシート名が存在しない場合という名前のファイルが存在しない場合

NOTEを「Tは、あなたが使用することを私に言ったファイルを見つける」、あなたはまだ、ダイアログボックスを取得する可能性があります。

代替ソリューション:

すでにちょうど閉じたブックから値を取得するには、この(シドの答えhereから少し変更された)のようなものを使用し、カスタム関数FileExistsを使用しているので:

Function GetVal(path$, shtName$, cellRef$) 

Dim exists As Boolean, ret 
Dim fileName$, directory$, sht$, addr$ 

    exists = Dir(Trim(path)) <> vbNullString 

    If Not exists Then GoTo EarlyExit 

    'Else, if the file exists, get the values 
    path = Replace(path, "/", "\") 
    'Get the query substrings: 
    fileName = Dir(path) 
    directory = Left$(path, Len(path) - Len(fileName)) 

    'Get the address of the cell, R1C1 style 
    addr = Range(cellRef).Address(True, True, -4150) 
    'Build the query string in the ExecuteExcel4Macro function 
    ret = ExecuteExcel4Macro("'" & directory & "[" & fileName & "]" & shtName & "'!" & addr) 

    GetVal = ret 

EarlyExit: 
    ret = "File or sheetname doesn't exist!" '## Modify as needed 
End Function 

その後

:あなたはこれを行う

=IF(FileExists("path\filename.xls"),IF('path\[filename.xls]Sheet'!D$13="16m8", 'path\[filename.xls]Sheet'!D40, ""),"false") 

:よう代わりに、あなたのロジックを変更

=IF(GetVal("path\filename.xls", "Sheet1", "D$13")="16m8", GetVal("path\filename.xls", "Sheet1", "D40")) 

上記の機能は、お使いのFileExistsと同じロジックを使用し、ファイルがExecuteExcel4Macro組み込み関数にクエリを渡す前に存在していることを確認ありません。

関連する問題