[英]VBA pasting into different workbook, different worksheet
I have a tricky copy and paste problem. 我有一个棘手的复制和粘贴问题。 I have an excel 2007 workbook, called Summary, with two sheets in it (sheet 1 and sheet 2). 我有一本名为Summary的excel 2007工作簿,其中有两页(第一页和第二页)。 I have a list of the names of excel workbooks that reside given folder on my hard drive typed into Column A on Sheet 1. I am trying to open each of those workbooks, copy specific cells in each of those workbooks, and paste them into my Summary workbook, in sheet TWO. 我有一个列出的excel工作簿的名称,这些工作簿位于硬盘驱动器上给定的文件夹中,并键入到工作表1的A列中。我试图打开每个工作簿,将每个工作簿中的特定单元格复制并粘贴到我的工作簿中摘要工作簿,第2页。 I've got them going perfectly onto Sheet 1, but can't seem to copy them to Sheet 2. Any help would be greatly appreciated! 我已经将它们完美地放在了Sheet 1上,但是似乎无法将它们复制到Sheet 2上。任何帮助将不胜感激!
Thank you, 谢谢,
Jonathan 乔纳森
Here is my code: 这是我的代码:
Sub CopyRoutine()
Const SrcDir As String = "C:\filepath\"
Dim SrcRg As Range
Dim FileNameCell As Range
Dim Counter As Integer
Application.ScreenUpdating = False
'Selecting the list of workbook names
Set SrcRg = Range(Range("A2"), Range("A3").End(xlDown))
On Error GoTo SomethingWrong
For Each FileNameCell In SrcRg
Counter = Counter + 1
Application.StatusBar = "Doing workbook " & Counter & " of " & SrcRg.Cells.Count
'Copying the selected cells
Workbooks.Open SrcDir & FileNameCell.Value
Sheets("Sheet1").Visible = True
Sheets("Sheet1").Select
Range("'Sheet1'!J4:K4").Copy
Sheets("Sheet2").Select
'Pasting the selected cells - but i cannot seem to move to sheet 2!
FileNameCell.Offset(0, 5).PasteSpecial xlPasteValuesAndNumberFormats
Application.CutCopyMode = False 'Clear Clipboard
ActiveWorkbook.Close False
Next
Application.StatusBar = False
Exit Sub
SomethingWrong:
MsgBox "Could not process " & FileNameCell.Value
End Sub
Keep track of your workbooks. 跟踪您的工作簿。
Sub CopyRoutine()
Const SrcDir As String = "C:\filepath\"
Dim SrcRg As Range
Dim FileNameCell As Range
Dim Counter As Integer
Dim SummaryWorkbook As Workbook 'added
Dim SourceDataWorkbook As Workbook 'added
Set SummaryWorkbook = ActiveWorkbook 'added
Application.ScreenUpdating = False
'Selecting the list of workbook names
Set SrcRg = Range(Range("A2"), Range("A3").End(xlDown))
On Error GoTo SomethingWrong
For Each FileNameCell In SrcRg
Counter = Counter + 1
Application.StatusBar = "Doing workbook " & Counter & " of " & SrcRg.Cells.Count
'Copying the selected cells
Set SourceDataWorkbook = Workbooks.Open SrcDir & FileNameCell.Value
Sheets("Sheet1").Visible = True
Sheets("Sheet1").Select
Range("'Sheet1'!J4:K4").Copy
SummaryWorkbook.Sheets("Sheet2").Select 'goto correct workbook!
'Pasting the selected cells - but i cannot seem to move to sheet 2!
FileNameCell.Offset(0, 5).PasteSpecial xlPasteValuesAndNumberFormats
Application.CutCopyMode = False 'Clear Clipboard
SourceDataWorkbook.Close False
Next
Application.StatusBar = False
Exit Sub
SomethingWrong:
MsgBox "Could not process " & FileNameCell.Value
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.