매크로/모듈(Module)

Import the file to the sheet

고추탄 2022. 10. 23. 07:46

Private Sub ImportTheFiletoTheSheet()
    
  On Error Resume Next
  
  Dim Files As Variant
  Dim fileX As Variant
  Dim sht As Worksheet
  Dim WB As Workbook
  Dim rngTarget As Range
   
 '화면 업데이트 일시 중지
  Application.ScreenUpdating = False
   
 '시트삽입 후 시트이름 지정
  Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Importthefiletothesheet_"
   
 '여러개 파일이 선택가능하도록 multiselect속성을 '참'으로 한다
  Files = Application.GetOpenFilename(filefilter:="Excel Files(*.csv),*.csv", Title:="파일선택", MultiSelect:=True)
 
  Set sht = ActiveSheet
 
  For Each fileX In Files
 
     'Open Target file
      Set WB = Workbooks.Open(fileX)
      
     'Specify the target file to the array variable
      Set rngTarget = sht.Range("a65536").End(xlUp).Offset(0, 0)
        
     '시트에 파일명
     'rngTarget = Split(fileX, "\")(UBound(Split(fileX, "\")))
      
     '선택한 개별파일들의 usedrange를 복사해서 추가 시트에 붙인다
      WB.Worksheets(1).UsedRange.Copy rngTarget.Cells(1, 1)
 
      Application.CutCopyMode = False
      
      WB.Close savechanges:=False
               
  Next fileX
    
 '화면 경고창 비활성화
  Application.DisplayAlerts = False
 
 '가져오기 자료시트 삭제
 'Sheets("Importthefiletothesheet_").Delete
  
 '화면 경고창 복원
  Application.DisplayAlerts = True
  
  MsgBox "Import the file to the sheet...Complete", 48
    
 '화면 업데이트
  Application.ScreenUpdating = True

End Sub

LIST

'매크로 > 모듈(Module)' 카테고리의 다른 글

클립보드의 사진을 시트에 붙이기  (1) 2023.06.10
TreeView Control(Ⅲ)  (0) 2022.10.23
Export Range to File...  (0) 2022.10.23
Get color picker RGB value  (0) 2022.10.22
Free VBA Addin ImageMSO Gallery  (0) 2022.10.22