1

列Aにタイトルのリストがあり、列Dにファイル名のリストがあるExcelスプレッドシートを使用しています。ファイル名は、列Aのタイトルに基づく特定の命名規則で作成されます。 A1のタイトルが「Taco_123」の場合、D1のファイル名には正確なフレーズ「Taco_123」が含まれます。

現時点では、ファイルの作成後にファイル名を手動でD1に入力する必要があります。しかし、私が欲しいのは、それを自動的に行うVBAスクリプトです。私の考えでは、各行について、列Aのタイトルを読み取り、ファイルがあるディレクトリでその正確なフレーズを含むファイルを検索してから、ファイル名(拡張子を除く)を列Dにコピーします。

そのようなことさえ可能ですか?VBAについてzilchを知っているので、オンラインで解決策を検索しようとしましたが、あまり運がありませんでした。同じ問題を抱えている人を見つけることすらできません。どんな助けでもいただければ幸いです。

編集:これが違いを生むかどうかはわかりませんが、一致するファイル名を検索する必要があるファイルタイプはPSDです。

4

1 に答える 1

1

そのようなことは間違いなく可能です。Excel 2003 以前と同様に、Application.FileSearch を使用して古いバージョンの Excel でこれを行う簡単な方法がありました。2007 以降では、P. Havdra のソリューションを使用または変更する必要があります。

http://social.msdn.microsoft.com/Forums/en-US/isvvba/thread/a450830d-4fc3-4f4e-aee2-03f7994369d6/

このスクリプトは、検索パラメーターに一致するファイルのリストを出力し、迷惑なメッセージ ボックスのポップアップも表示します。

作業を開始できるように、彼の FileSearch サブルーチンにいくつかの変更を加えました。これにより、指定したディレクトリに一致する PSD ファイル名の配列が作成され、配列を操作してワークシート データを操作できます。

Sub FileSearch()
'
' Example of FileSearchByHavrda procedure calling as replacement of missing FileSearch     function in the newest MS Office VBA
' 01.06.2009, Author: P. Havrda, Czech Republic

'As modified Feb-13-2013, David Zemens, for _
http://stackoverflow.com/questions/14865258/populate-cell-with-a-filename-based-on-path-excerpt-in-previous-cell
'
Dim sDir As String  '< this is the search directory you will use

Dim FileNameWithPath As Variant
Dim ListOfFilenamesWithParh As New Collection ' create a collection of filenames
Dim rCount As Long 'row counter

Dim myArray() As Variant
Dim a As Long 'array iterator
a = 0

'Set the search directory:
sDir = "C:\DESKTOP\"  '<-- MODIFY TO SUIT YOUR NEEDS

' Filling a collection of filenames (search Excel files including subdirectories)
Call FileSearchByHavrda(ListOfFilenamesWithParh, sDir, "*.psd", False)

' Print list to immediate debug window and as a message window
For Each FileNameWithPath In ListOfFilenamesWithParh ' cycle for list(collection) processing
    'Create an array of matching filenames
    ReDim Preserve myArray(a)
    myArray(a) = FileNameWithPath
    a = a + 1
Next FileNameWithPath

' If no file was found:
If ListOfFilenamesWithParh.Count = 0 Then
    'Debug.Print "No file was found !"
    MsgBox "No file was found !"
Else:
    'some files were found, so do something with myArray
    '########################################################
    '
    '  <-- Code to manipulate your worksheet will go here -->
    '
    '
    '########################################################
End If
End Sub
于 2013-02-14T02:37:44.877 に答える