1
我不是程序員 - 但我需要/想要在Excel中編寫命令以將多個.csv文件聚合到一個工作簿中的單獨工作表中......它運行一次,並且複製/粘貼一個.csv文件的內容但隨後出現了錯誤,此錯誤:如何在一個工作簿中將多個Excel Spreasheets(.csv)聚合/編譯爲單獨的工作表?
Runtime error '438':
Object does not support this property or method.
我已經將範圍縮小到這一行:
'paste it
ThisWorkbook.Worksheets(Sheets.Count).Range("A1").Paste
但是,是,我是新來的最重要的是這個,我不能確定什麼做。到目前爲止,我一直在抓取來自網絡的代碼片段。
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
只是好奇 - 爲什麼不包括在'路徑='最後的 「\」?爲什麼要分開它並使用'&「\」'? – BruceWayne
@BruceWayne我已經讓編譯器在之前給我做過一些小題大做,所以我只是出於習慣而做。 –