Share via

Excel2010 Macro-あるフォルダからファイル名の新しいものを選択し、別のファイルにコピーする方法を教えてください

Anonymous
2015-08-30T16:38:04+00:00

最近Excel2010でマクロVBAを独学で始めた初心者です。本を購入して学習を進めていますが、本当に基本的なことしか書いていなかったり、ピンポイントで私の使いたいマクロがみつからなかったり、で、結局インターネットで検索してコピーしたり、必要な個所のみ変更したりしてどうにかこうにか学習し、実務(営業事務)に使用しています。

教えていただきたいことは、タイトルにも書きましたが、

1)あるフォルダに格納されている複数のExcelファイルからファイル名の新しいものを選び出し(例:SalesData_20150801, SalesData_20150802.......であれば選び出すのはSalesData_20150802 )

2)別のExcel fileのシートにコピーしたいのです。

以下で詳しく述べますが、1)と2)の作業を切り離して別々に作業したところ、それぞれは動作がきちんとできました。ですが一連の動作をどのように行うのかがわかりません。できるだけ簡単なコードを教えていただけると幸いです。よろしくお願いいたします。

まず、2)のほうのコードから考えてみたところ、ファイル名を指定した上でですが、きちんと作動しました。

Sub DataCopy()

Dim readBook As Workbook

Set readBook = Workbooks.Open("C:\xxx\xxx\xxx.xlsx")

Dim readSheet As Worksheet

Set readSheet = readBook.Worksheets("xxx")

Workbooks("xxx.xlsx").Worksheets("xxx").Cells.Copy ThisWorkbook.Worksheets("xxx“).Range("A1")

readBook.Close False

Set readSheet = Nothing

Set readBook = Nothing

End Sub

次に1)を別個に考えてみました。ネットでみつけたコードをそのままコピーし必要な変更を加えただけなのですが、こちらも、正しいファイル(最新の日付のファイル)がきちんと検索されました。 

Sub 最新フォルダを取得()

 Dim sDir As String

 Dim sTitleHead As String

 Dim sTemp As String

 Dim sReturn As String

    ' ' 「指定フォルダ」へのパスをドライブ名から指定

     sDir = "C:\xxx\xxx\xxx"

    ' ' ファイル名を前方一致で篩に掛ける「タイトル」を指定

     sTitleHead = "xxx"

    ' ' Dir関数でファイル名を取得

     sTemp = Dir(sDir & "" & sTitleHead & "*")

    ' ' Dir関数をループ

    Do While sTemp <> ""  ' Dir関数でファイル名を取得し尽したらループ終了

     ' ' ファイル名が「タイトル」で始まるものだけ篩に掛ける

        If sTemp Like sTitleHead & "*" Then

            If sTemp > sReturn Then sReturn = sTemp

        End If

        sTemp = Dir()  ' ' Dir関数で次のファイル名を取得

     Loop

    MsgBox "フォルダ:" & sDir & vbLf & "タイトル:" & sTitleHead & vbLf & "最新:" & sReturn

 End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2015-09-01T11:35:12+00:00

Minako,

Macros can be extremely useful depending on what you are doing.  I think it is time well invested.  Wish you the best of luck and this site is certainly a great resource to ask questions when they arise (a lot of talented coders in Excel VBA answering here)

  • probably asking in English would garner more answers <g>.   

--

Regards,

Tom Ogilvy

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Anonymous
2015-08-30T17:39:34+00:00

Possibly:

Sub Abc()

 Dim sDir As String

  Dim sTitleHead As String

  Dim sTemp As String

  Dim sReturn As String

  Dim readBook As Workbook

  Dim readSheet As Worksheet

  sDir = "C:\xxx\xxx\xxx"

  sTitleHead = "xxx"

  sTemp = Dir(sDir & "" & sTitleHead & "*")

  Do While sTemp <> ""

     If sTemp Like sTitleHead & "*" Then

        If sTemp > sReturn Then sReturn = sTemp

     End If

     sTemp = Dir()

  Loop

  Set readBook = Workbooks.Open(sDir & "" & sReturn)

  Set readSheet = readBook.Worksheets("xxx")

  readSheet.Cells.Copy ThisWorkbook.Worksheets("xxx").Range("A1")

  readBook.Close False

  Set readSheet = Nothing

  Set readBook = Nothing

End Sub

--

Regards,

Tom Ogilvy

Was this answer helpful?

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2015-09-01T05:41:46+00:00

    Dear Tom,

    That's amazing!

    I just stared doing/learning macro, and sometimes difficult. I don't know how much I can develop my skill but I will try.

    i know macro is very useful and helpful.

    thanks again for your help!

    regards,

    Minako

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-08-31T21:06:30+00:00

    Minako,

    I don't speak or read Japanese.   I just took a guess at what you might be asking based on the code that I could read.  Glad it worked for you!

    --

    Regards,

    Tom Ogilvy

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-08-31T19:36:35+00:00

    Dear Tom,

    Thank you so much for your prompt advice.

    Your macro code worked properly.

    Did you understand what I wrote in Japanese? or did you understand what I would want to know from my seperate macro codes?

    anyway it worked and thank you so much for your help!

    Best regards,

    Minako

    Was this answer helpful?

    0 comments No comments