VBA粘贴到不同的工作簿,不同的工作表
我有一个棘手的复制和粘贴问题。我有一个名为“摘要”的 Excel 2007 工作簿,其中有两张表(表 1 和表 2)。我有一个 Excel 工作簿的名称列表,该列表位于硬盘驱动器上的给定文件夹中,输入到工作表 1 的 A 列中。我尝试打开每个工作簿,复制每个工作簿中的特定单元格,然后将它们粘贴到我的工作簿中。摘要工作簿,第二张。我已将它们完美地复制到工作表 1 上,但似乎无法将它们复制到工作表 2 中。任何帮助将不胜感激!
谢谢
乔纳森
这是我的代码:
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
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). 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. 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!
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
跟踪您的工作簿。
Keep track of your workbooks.