2016-11-29 4 views
0

現在、画像名をExcelフォルダから画像フォルダに一致させる機能を使用していますが、画像を保存してその名前を追加するのを忘れてしまったら、名前を追加するのを忘れてしまうはずです。例えば 私は、画像フォルダExcel VBAユーザー定義関数はフォルダ内の画像を検索します(画像のフォルダ名にExcel名を一致させます)

16095_1.jpg,16095_2.jpg,16095_3.jpg 

で3枚の画像を保存し、私はそれが私がExcelのセルに一つの画像の名前を忘れてしまったことを私に警告しなければならない

16095_1.jpg,16095_2.jpg 

としてExcelシートに画像名を追加した場合。

私のイメージ名の形式がある - 16095_1.jpg、私が使用しています16095_2.jpg

機能は...

Function findimage(Path As String, ImageList As String) 
Dim results 
Dim x As Long 
Dim dc 'double comma 
results = Split(ImageList, ",") 
If Not Right(Path, 1) = "\" Then Path = Path & "\" 
For x = 0 To UBound(results) 
results(x) = Len(Dir(Path & results(x))) > 0 
Next 
dc = InStr(ImageList, ",,") 
If dc = 0 Then 
findimage = Join(results, ",") 
Else 
findimage = ("Double_comma") 
End If 
End Function 
+0

あなたは指定された場所にすべてのファイルをループに 'ディレクトリを()'を使用し、あなたが渡したファイルのリストと照合して確認することができます。 –

+0

説明してください、私はで初心者ですVBA。 –

答えて

1

であるこの機能は、フォルダパスをとり、パターンの可変数は、(参照してください。 MSDN - Parameter Arrays (Visual Basic))。 MSDN - Dir Functionを使用して、フォルダパス内のファイル名を反復処理し、パターンと一致するファイルの数をカウントするために、MSDN - Like Operator (Visual Basic)のパターンと比較します。

用途:

  • getFileCount( "C:\ユーザーは、所有者\写真を\"、 ".GIF"、」 .PNG ")
  • getFileCount(" C:\ Users \ユーザー所有者\写真」、 "* GIF"
  • getFileCount。( "C:\ユーザーは、所有者\写真を\"、 "apple_ .GIF"、 "banana_ .GIF"、 "オレンジ_ ## *"。)
  • getFileCount( "C:\ Users \ Owner \ Pictures"、 "######。gif")

Function getFileCount(DirPath As String, ParamArray Patterns() As Variant) As Integer 
    Dim MyFile As String 
    Dim count As Integer, x As Long 
    If Not Right(DirPath, 1) = "\" Then DirPath = DirPath & "\" 

    MyFile = Dir(DirPath, vbDirectory) 

    Do While MyFile <> "" 
     For x = 0 To UBound(Patterns) 
      If MyFile Like Patterns(x) Then 
       count = count + 1 
       Exit For 
      End If 
     Next 

     MyFile = Dir() 
    Loop 
    getFileCount = count 
End Function 
+0

ありがとうございました....しかし、あなたはこれを別々に作成しました...しかし、私はこれを私のVBAで動作させたいと思っています... –

+0

あなたはこれらの2つをマージしてください....その結果は... True、True 、False(4).... 4は、フォルダに4つの画像があることを示しています。 –

+0

http://stackoverflow.com/questions/40931818/merging-two-vba-functions –

関連する問題