2

まず第一に、私は VBA の知識があまりないため、私の問題を提示する際の私の欠点をお許しください。どうぞよろしくお願いいたします。

私は、3 つの異なるサブフォルダーから 3 つの異なる Excel ファイルのコンテンツを 1 つの Excel ファイルに配置し、そこに含まれるデータを処理するためにいくつかのマクロを実行することを意味するプロジェクトに取り組んでいます。処理マクロを既に設定しているため、問題はコンテンツを正しくインポートすることにかかっています。

私が直面している問題は、開きたいファイルの正確な名前がなく、毎月変更されることです。したがって、正確な名前が必要な「WorkBooks.Open」コマンドは使用できません。ただし、ファイルには予測可能な名前形式があります。たとえば、サブフォルダーの 1 つは「XXX-jan2013.xls」という名前のファイル、別のサブフォルダーは「january2013-XXX」、最後のフォルダーは「XXX-01/2013」という名前のファイルで構成されます。

私の目標は、たとえば「01/2013」のように月と年を手動で入力し、名前に「January」、「jan」、または「01」を含むすべてのファイルを開くことです。

ここに私がこれまでに持っているものとコメントがあります:

Sub ChosenDate()
‘It aims at opening a box in which the desired month would be written manually
Dim InputDate As String
‘These are the indications the user will get
    InputDate = InputBox(Prompt:="Please choose a month.", _
            Title:="Date", Default:="MM/YYYY")
‘In case the person forgets to write what he’s asked to
          If InputDate = "MM/YYYY" Or _
          InputDate = vbNullString Then
          Exit Sub
‘If he does it correctly, I call the second Sub
         Else: Call FilesOpening
        End If
End Sub
‘So far, everything works fine

Public Sub FilesOpening()
‘This one aims at opening the chosen files
Dim ThisFile               As String
Dim Files                    As String
 ‘Defining the folder in which the file is, as it can change from a computer to another
ThisFile = ThisWorkbook.Path
‘Here’s where I start struggling and where the macro doesn’t work anymore
‘If I wanted to open all the files of the folder, I would just write that:
Files = Dir(ThisFile & "\*.xls")
‘You never know…
On Error Resume Next
‘Creating the Loop
Do While Files <> vbNullString
Files = Dir
Set wbBook = Workbooks.Open(ThisWorkbook.Path & "\" & Files)
Loop
End Sub
‘But it doesn’t look inside of sub-folders, neither does it consider the date
Sub DataProcess()
‘This one is fine, except I can’t find a way to name the files correctly. Here’s the beginning:
Windows("I don’t know the name.xls").Activate
Sheets("Rapport 1").Select
Cells.Select
Selection.Copy
Windows("The File I Want To Put Data In.xlsm").Activate
Sheets("Where I Want To Put It").Select
Range("A1").Select
ActiveSheet.Paste
Windows("I don’t know the name.xls").Close
‘How can I get the name?

私の発言が理解できることを願っています。

事前にどうもありがとうございました!

良い1日を、

E.

4

1 に答える 1

0

パスと予想されるファイル マスクのリストを作成する必要があります。次に、一致する各ファイルをループして、自分のことを行うことができます。

Sub foo()
Dim request As String:  request = "01/2013"
'//make a date
Dim asDate   As Date:   asDate = "01/" & request
Dim dirs(2) As String, masks(2) As String

dirs(0) = "c:\xxx\dir1\"
masks(0) = "*" & Format$(asDate, "mmmmyyyy") & "*.xls"

dirs(1) = "c:\xxx\dir2\"
masks(1) = "*" & Format$(asDate, "mmmyyyy") & "*.xls"

dirs(2) = "c:\xxx\dir3\"
masks(2) = "*" & Format$(asDate, "mmyyyy") & "*.xls"

Dim i As Long
For i = 0 To UBound(dirs)
    GetFiles dirs(i), masks(i)
Next
End Sub

Private Function GetFiles(path As String, mask As String)
Dim file As String
'//loop matching files
file = Dir$(path & mask)
Do Until Len(file) = 0
    '//process match
    process path & file
    file = Dir$()
Loop
End Function

Sub process(filePath As String)
    MsgBox "processing " & filePath
    'workbook.open
End Sub

"XXX-01/2013"私が想定したファイル名ではありません"XXX-012013"。別のサブディレクトリの場合:

dirs(x) = "c:\xxx\dir3\" & Format$(asDate, "mm") & "\"
masks(x) = "*" & year(asDate) & "*.xls"
于 2013-07-11T17:02:44.513 に答える