0
Sub a()

    Dim copy_range As Variant
  
    Application.Workbooks.Open Filename:=Dir(ThisWorkbook.Path & "/* (2).csv")
    copy_range = Sheets(1).Range("A1:C288").Value
    ActiveWorkbook.Close
    
    Sheets(2).Range("A1:C288") = copy_range
    
   
      
End Sub

In this part Dir(ThisWorkbook.Path & "/* (2).csv")

It says the file doesn't exist.

The current folder contains (2) to (19).csv . The full name of the file name is 20222022(2).csv 20222022(3).csv ~ 20222022(19).csv no see.

The folder path is like this. C:\Users\fauus\Desktop\람세스 제어반

김동진
  • 1
  • 1
  • `Dir(ThisWorkbook.Path & "/* (2).csv")` is missing an ampersand, has a `*` instead of a `"`. And the path separator is wrong if on Windows. – Lundt Sep 23 '22 at 01:04
  • I'm a Korean. Can you upload the finished version? The full name of the file name is 20222022(2).csv 20222022(3).csv ~ 20222022(19).csv no see. – 김동진 Sep 23 '22 at 01:15
  • `Dir(ThisWorkbook.Path & "\*" & "(2).csv")` for Windows. I don't use macs see help if you do https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dir-function – Lundt Sep 23 '22 at 01:28
  • I applied as you suggested, but the result is the same. – 김동진 Sep 23 '22 at 02:03
  • Dir() only returns the *filename*, so if the current directory isn't `ThisWorkbook.Path` it won't find the file. – Tim Williams Sep 23 '22 at 03:07
  • Application.Workbooks.Open Filename:=ThisWorkbook.Path & "\" & Dir(ThisWorkbook.Path & "\* (2).csv") – Tim Williams Sep 23 '22 at 03:08
  • I used all methods. But it still doesn't work. – 김동진 Oct 11 '22 at 06:49

2 Answers2

0

You don't need the DIR and you slash is the wrong way round, the following should work:

Application.Workbooks.Open Filename:=ActiveWorkbook.Path & "\*(2).csv"
Stuart
  • 144
  • 5
0

Dir() only returns the file name, not the full path, so if the current directory isn't ThisWorkbook.Path it won't find the file.

Sub a()

    Dim copy_range As Variant, f, fldr As String, wb As Workbook

    fldr = ThisWorkbook.Path & "\"
    f = Dir(fldr & "* (2).csv", vbNormal)

    If Len(f) > 0 Then
        Set wb = Workbooks.Open(fldr & f)
        copy_range = wb.Sheets(1).Range("A1:C288").Value
        wb.Close
        ThisWorkbook.Sheets(2).Range("A1:C288") = copy_range
    Else
        MsgBox "No file found!"
    End If

End Sub
Tim Williams
  • 154,628
  • 8
  • 97
  • 125