将Excel支持的文件从同一文件夹复制粘贴到单个Excel

时间:2017-10-11 13:40:35

标签: excel vba excel-vba

我有两种不同类型的文件。 1是" .tsv" 2是" .xlsx"。

我希望我的宏(.xlsm)文件位于同一文件夹中,其中放置了两个不同的文件(.tsv和.xlsx)。我通常从一些工具下载它们,通常它们都是我的默认设置"下载"夹。

问题1:我希望我的宏文件读取格式并将粘贴数据复制到单个excel文件中。我完成了这个问题。需要进行代码优化。

问题2:我当前正在为该文件夹分配手动路径,并希望宏选择该文件夹路径,以便它可以复制2个扩展文件并继续。 (尝试:ActiveWorkbook.Path,没有工作)。

Dim FPath As String, filename As String, FileExt1 As String, FileExt2 As 
String

Sub GetSheets()
FPath = "C:\Users\dinekuma\Desktop\Dinesh KT\Macro New UI\"
FileExt1 = "*.tsv"
FileExt2 = "*.xlsx"

'ActiveWorkbook.Path
'"C:\Users\dinekuma\Desktop\Dinesh KT\Macro New UI\"

filename = Dir(FPath & FileExt1)

Application.ScreenUpdating = False
Application.DisplayAlerts = False

  Do While filename <> ""
  Workbooks.Open filename:=FPath & filename, ReadOnly:=True
     For Each sheet In ActiveWorkbook.Sheets
     sheet.Copy After:=ThisWorkbook.Sheets(1)
    ActiveSheet.Name = Split(filename, ".")(0)
       Next sheet
     Workbooks(filename).Close
     filename = Dir()

  Loop

  filename = Dir(FPath & FileExt2)

Application.ScreenUpdating = False
Application.DisplayAlerts = False

  Do While filename <> ""
  Workbooks.Open filename:=FPath & filename, ReadOnly:=True
     For Each sheet In ActiveWorkbook.Sheets
 sheet.Copy After:=ThisWorkbook.Sheets(1)
ActiveSheet.Name = Split(filename, ".")(0)
   Next sheet
 Workbooks(filename).Close
 filename = Dir()

  Loop
  MsgBox ("Import Successful!")

End Sub

通过活动宏文件帮助进行代码优化和自动路径选择。

0 个答案:

没有答案