【问题标题】:How to aggregate/compile multiple Excel spreasheets (.csv) into separate sheets in one workbook?如何在一个工作簿中将多个 Excel 电子表格 (.csv) 聚合/组合成单独的工作表?
【发布时间】:2017-04-08 07:44:56
【问题描述】:

我不是程序员 - 但我需要/想要在 Excel 中编写一个命令来将多个 .csv 文件聚合到一个工作簿中的单独工作表中...它运行一次,并复制/粘贴一个 .csv 的内容文件,但随后出现此错误:

Runtime error '438':

Object does not support this property or method.

我把它缩小到这一行:

'paste it
ThisWorkbook.Worksheets(Sheets.Count).Range("A1").Paste

但是,由于我对这一切都不熟悉,所以我不确定该怎么做。到目前为止,我刚刚从网络上抓取了似乎适用的代码 sn-ps。

Private Sub CommandButton1_Click()

Dim strFile As String, strPath As String
Dim wkb As Workbook

'Change this path for your own file location:
strPath = "C:[FILE PATH HERE]"

'this returns an empty string "" if the file cannot be found and will error 
if the folder is incorrect
strFile = Dir(strPath & "*.csv")

Do While strFile <> ""
 'open the csv file and assign it to a variable so that we can easily 
 reference it later
Set wkb = Workbooks.Open(strPath & strFile)

 'add a new worksheet at the end of the macro workbook to paste into
ThisWorkbook.Sheets.Add After:=Sheets(Sheets.Count)

 'get the range and copy it
wkb.Sheets(1).UsedRange.Copy
Debug.Print (Sheets.Count)
 'paste it
ThisWorkbook.Worksheets(Sheets.Count).Range("A1").Paste

 'close the csv file
wkb.Close

 'find the next file - Dir without parameters will look for the next file in the folder that matches the first Dir call
strFile = Dir
Loop



End Sub

【问题讨论】:

    标签: vba excel csv


    【解决方案1】:
     Sub Dougsloop()
    
         Dim wbk As Workbook
         Dim Filename As String
         Dim path As String
         Dim wsO As Workbook
         Dim StartTime As Double
         Dim SecondsElapsed As Double
         Dim aRR As Variant
         Dim rowC As Long
         Dim colC As Long
    
         Application.ScreenUpdating = False
         Application.DisplayAlerts = False
         Application.Calculation = xlCalculationManual
    
         StartTime = Timer
    
         path = "path to folder of files" & "\"
         Filename = Dir(path & "*.csv??")
         Set wsO = ThisWorkbook
         wsO.Sheets(1).Select
    
         Do While Len(Filename) > 0
             DoEvents
             Set wbk = Workbooks.Open(path & Filename, True, True)
             aRR = wbk.Sheets(1).UsedRange
             rowC = wbk.Sheets(1).UsedRange.Rows.Count
             colC = wbk.Sheets(1).UsedRange.Columns.Count
             wsO.ActiveSheet.Range(wsO.ActiveSheet.Cells(1, 1), wsO.ActiveSheet.Cells(rowC, colC)).Value2 = aRR
             wbk.Close False
             Filename = Dir
             wsO.Sheets.Add After:=Worksheets(Worksheets.Count)
         Loop
    
         Application.ScreenUpdating = True
         Application.DisplayAlerts = True
         Application.Calculation = xlCalculationAutomatic
    
         SecondsElapsed = Round(Timer - StartTime, 2)
         MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
    
     End Sub
    

    【讨论】:

    • 只是好奇 - 为什么不在path = 中包含最后的“\”?为什么要分开使用&amp; "\"
    • @BruceWayne 我之前曾让编译器对此大惊小怪,所以我只是出于习惯这样做。
    最近更新 更多