[英]Excel VBA - How do copy and transpose paste another sheet
I try to copy data from Workbooks with Sheets("daily shift report") to another Workbooks Sheets ("Sheet1") by transpose according to the code below.我尝试根据下面的代码通过转置将数据从带有工作表的工作簿(“每日班次报告”)复制到另一个工作簿工作表(“Sheet1”)。
Sub copyDatafrommultipleworkbookintomaster()
Dim FolderPath As String, Filepath As String, Filename As String, Erow As Range
FolderPath = "C:\Users\YIT\Documents\test\April57\"
Filepath = FolderPath & "*.xls*"
Filename = Dir(Filepath)
Do While Filename <> ""
Workbooks.Open (FolderPath & Filename)
ActiveWorkbook.Sheets("daily shift report").Range("B71:G77").Copy
Application.DisplayAlerts = False
ActiveWorkbook.Close
Worksheets("Sheet1").Range("A1").PasteSpecial Transpose:=True
Filename = Dir
Loop
End Sub
But found Run time error '1004' Application-defined or object-defined error.I guess error in line 14. Worksheets("Sheet1").Range("A1").PasteSpecial Transpose:=True Could you please suggest a solution to this problem?.但发现运行时错误 '1004' 应用程序定义或对象定义错误。我猜第 14 行有错误。 Worksheets("Sheet1").Range("A1").PasteSpecial Transpose:=True您能否提出解决方案这个问题?。
Try the next code, please.请尝试下一个代码。 It will Paste, from each existing.xls workbook, in the next empty column of "Sheet1":
它将从每个现有的.xls 工作簿中粘贴到“Sheet1”的下一个空列中:
Sub copyDatafrommultipleworkbookintomaster()
Dim FolderPath As String, Filepath As String, Filename As String
Dim wb As Workbook, ws As Worksheet, Col As Long
Col = 1
Set ws = ActiveWorkbook.Sheets("Sheet1")
FolderPath = "C:\Users\YIT\Documents\test\April57\"
Filename = Dir(FolderPath & "*.xls*")
Do While Filename <> ""
Set wb = Workbooks.Open(FolderPath & Filename)
wb.Sheets("daily shift report").Range("B71:G77").Copy
ws.cells(1, Col).PasteSpecial Paste:=xlPasteAll, Transpose:=True
Col = Col + 7 'increment the next col where to paste
wb.Close False
Filename = Dir
Loop
End Sub
Please see if either of following two set of codes is useful to you.请查看以下两组代码中的任何一个是否对您有用。
Sub GetData()
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetSheet As Worksheet, sourceSheet As Worksheet
'targetSheet is Activeworkbook wherein you would want to fetch the data
Set targetSheet = ActiveWorkbook.Worksheets("Sheet1")
'Mention Source-file path & file name between double quotes below
customerFilename = "C:\Users\YIT\Documents\test\April57\Your_File_Name_Here.xls"
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
Set sourceSheet = customerWorkbook.Worksheets("daily shift report")
sourceSheet.Range("B71:G77").Copy
'select in which cell you want to paste data
targetSheet.Range("A1").PasteSpecial Transpose:=True
customerWorkbook.Close
End Sub
Sub GetData2()
Dim filter As String, caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetSheet As Worksheet, sourceSheet As Worksheet
'targetSheet is Activeworkbook wherein you would want to fetch the data
Set targetSheet = ActiveWorkbook.Worksheets("Sheet1")
'Prompt to get the customerWorkbook i.e. Source Workbook
filter = "Excel and CSV Files (*.xls;*.xlsx;*.csv),*.xls;*.xlsx;*.csv"
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
Set sourceSheet = customerWorkbook.Worksheets("daily shift report")
sourceSheet.Range("B71:G77").Copy
'select in which cell you want to paste data
targetSheet.Range("A1").PasteSpecial Transpose:=True
customerWorkbook.Close
End Sub
Hope these codes are useful to you.希望这些代码对你有用。 Regards.
问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.