简体   繁体   English

VBA将多个工作簿中的粘贴复制到同一文件夹中的1个中

[英]VBA copy Paste from multiple workbooks into 1 on same folder

I have multiple Excel files in a same folder which contains data in worksheet "Case Tracker". 我在同一文件夹中有多个Excel文件,其中包含工作表“ Case Tracker”中的数据。 I wanted to copy and paste data to all Excel files from one Excel file " Macro.xlsx ". 我想将数据从一个Excel文件“ Macro.xlsx ”复制并粘贴到所有Excel文件中。 The code is like it copies data from Rahul.xlsx to Macro.xlsx and then from Rohit.xlsx to Macro.xlsx and so on. 代码就像将数据从Rahul.xlsx复制到Macro.xlsx ,然后从Rohit.xlsxMacro.xlsx ,依此类推。 The problem is that while pasting data from Rohit.xlsx it is overlapping. 问题在于,从Rohit.xlsx粘贴数据时,它们是重叠的。 The code is not finding the next available blank row to paste data and this is due to code Sheets("Sheet1").Range("A1").Select . 该代码找不到要粘贴数据的下一个可用空白行,这是由于代码Sheets("Sheet1").Range("A1").Select导致的。 Can someone help me edit the code 有人可以帮我编辑代码吗

Sub OpenCopyPaste()

' open the source workbook and select the source sheet
Workbooks.Open Filename:="C:\Users\Rahul\Desktop\Test\Rahul.xlsx"

Sheets("Case Tracker").Select

' copy the source range
Sheets("Case Tracker").Range("A:G").Select

Selection.Copy

' select current workbook and paste the values starting at A1
Windows("Macro.xlsx").Activate

Sheets("Sheet1").Select
Sheets("Sheet1").Range("A1").Select

ActiveSheet.Paste
Application.CutCopyMode =False

ActiveWorkbook.Save

Workbooks.Open Filename:="C:\Users\Rahul\Desktop\Test\Rohit.xlsx"
Sheets("Case Tracker").Select

' copy the source range
Sheets("Case Tracker").Range("A:G").Select
Selection.Copy

' select current workbook and paste the values starting at A1
Windows("Macro.xlsx").Activate
Sheets("Sheet1").Select
Sheets("Sheet1").Range("A1").Select

ActiveSheet.Paste
Application.CutCopyMode =False

ActiveWorkbook.Save

EndSub

Replace 更换

Sheets("Sheet1").Range("A1").Select

With

Dim oCell As Range 'Only insert this line once!

With Sheets("Case Tracker")
    Set oCell = .Cells(.Rows.Count, 1).End(xlUp)
End With

If oCell.Row > 1 Then
    Set oCell = oCell.Offset(1, 0)
End If

oCell.Select

You should only declare oCell once, so only put this line once at the top: 您只应声明一次oCell,因此只需在该行的顶部放置一次:

Dim oCell As Range

Complete: 完成:

Sub OpenCopyPaste()
Dim oCell As Range
Dim rowCount As Integer
' open the source workbook and select the source sheet

Workbooks.Open Filename:="C:\Users\Rahul\Desktop\Test\Rahul.xlsx"

Sheets("Case Tracker").Select

' copy the source range

With Sheets("Case Tracker")
rowCount = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range(.Cells(1, 1), .Cells(rowCount, 7)).Select
End With

Selection.Copy

' select current workbook and paste the values starting at A1

Windows("Macro.xlsx").Activate

Sheets("Sheet1").Select

'------------------------------------------------
With Sheets("Sheet1")
    Set oCell = .Cells(.Rows.Count, 1).End(xlUp)
End With

oCell.Select
'------------------------------------------------

ActiveSheet.Paste

Application.CutCopyMode = False

ActiveWorkbook.Save

Workbooks.Open Filename:="C:\Users\Rahul\Desktop\Test\Rohit.xlsx"

Sheets("Case Tracker").Select

' copy the source range

With Sheets("Case Tracker")
rowCount = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range(.Cells(1, 1), .Cells(rowCount, 7)).Select
End With

Selection.Copy

' select current workbook and paste the values starting at A1

Windows("Macro.xlsx").Activate

Sheets("Sheet1").Select

'------------------------------------------------
With Sheets("Sheet1")
Set oCell = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0)
End With

oCell.Select
'------------------------------------------------

ActiveSheet.Paste

Application.CutCopyMode = False

ActiveWorkbook.Save
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM