简体   繁体   English

VBA - 如何在excel工作簿(工作簿名称更改)之间复制单元格?

[英]VBA - How to copy cells between excel workbooks (where workbook names change)?

I am looking for advice on how to write a macro that does the below. 我正在寻找有关如何编写下面的宏的建议。 I imagine its easy to do, but I can't figure it out. 我想它很容易做到,但我无法弄明白。 Thanks in advance! 提前致谢!

START 开始

  1. In the active sheet (in the workbook I am running this macro in [Title changes but same formatting each time]), copy cell B9. 在活动工作表中(在工作簿中我在[标题更改但每次格式相同]中运行此宏),复制单元格B9。 Paste in column A on the next blank row of the other workbook I am using [Can have the same title every time I run this process, or just be the only other workbook open] 粘贴在我正在使用的其他工作簿的下一个空行上的A列中[每次运行此过程时都可以使用相同的标题,或者只是打开其他工作簿]
  2. In the active sheet (in the workbook I am running this macro in), copy cell B8. 在活动工作表中(在我正在运行此宏的工作簿中),复制单元格B8。 Paste in column B of the row identified above. 粘贴在上面标识的行的B列中。
  3. In the active sheet (in the workbook I am running this macro in), copy cell B12. 在活动工作表中(在我正在运行此宏的工作簿中),复制单元格B12。 Paste in column C of the row identified above. 粘贴在上面标识的行的C列中。
  4. In the active sheet (in the workbook I am running this macro in), copy cells A17:E17. 在活动工作表中(在我正在运行此宏的工作簿中),复制单元格A17:E17。 Paste in D:H of the row identified above. 粘贴在上面标识的行的D:H中。
  5. In the active sheet (in the workbook I am running this macro in), copy cells A17:E17. 在活动工作表中(在我正在运行此宏的工作簿中),复制单元格A17:E17。 Paste in D:H of the row identified above. 粘贴在上面标识的行的D:H中。
  6. In the active sheet (in the workbook I am running this macro in), copy cells G17:N17. 在活动工作表中(在我正在运行此宏的工作簿中),复制单元格G17:N17。 Paste in I:P of the row identified above. 粘贴在上面标识的行的I:P中。

END 结束

Given my lack of vba coding ability I'm trying to record a macro and then adjust. 鉴于我缺乏vba编码能力,我正在尝试录制一个宏然后进行调整。 I've tried as many options as I can find on google. 我已经尝试了尽可能多的选项,我可以在谷歌上找到。 The below seems to be the best, but doesn't work. 以下似乎是最好的,但不起作用。 (NB: I start with B9 from point 1 above selected). (注意:我从上面选择的第1点开始B9)。

Sub Copy_Timesheet()
'
' Copy_Timesheet Macro
'

'
Selection.Copy
Windows("WorkbookB").Activate
Find_Blank_Row()
Dim BlankRow As Long
BlankRow = Range("A65536").End(xlUp).Row + 1
Cells(BlankRow, 1).Select
ActiveCell.Offset(1, 0).Range("A1").Select 
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ThisWorkbook.Activate
ActiveCell.Offset(3, 0).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("WorkbookB").Activate
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ThisWorkbook.Activate
ActiveCell.Offset(-4, 0).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("WorkbookB").Activate
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ThisWorkbook.Activate
ActiveCell.Offset(9, -1).Range("A1:E1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("WorkbookB").Activate
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ThisWorkbook.Activate
ActiveCell.Offset(0, 6).Range("A1:H1").Select
Application.CutCopyMode = False
Selection.Copy
Windows("WorkbookB").Activate
ActiveCell.Offset(0, 5).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub

Now that you have shown some effort in generating the code, here is a refactored version of what you said you were after. 既然您已经在生成代码方面付出了一些努力,那么这里是您所说过的重构版本。 (I didn't check to see whether that matched what you actually recorded, but the fact that you went to the trouble of recording something indicated that you weren't just too lazy to do this yourself.) (我没有检查是否与您实际录制的内容相符,但事实上您遇到了录制内容的麻烦,这表明您自己并不是太懒了。)

Sub Copy_Timesheet()
    'Set up some objects to make life easier in the rest of the code
    ' "the active sheet (in the workbook I am running this macro in)"
    Dim wsSrc As Worksheet
    Set wsSrc = ThisWorkbook.ActiveSheet
    'the sheet in the other workbook
    Dim wsDst As Worksheet
    Set wsDst = Workbooks("WorkbookB").Worksheets("destination_sheet_name") 'change sheet name to whatever you need

    Dim BlankRow As Long
    'Fully qualify ranges so that we ensure we are working with the sheet we expect to be
    'Use Rows.Count rather than 65536 just in case we are working in a recent workbook that allows 1048576 rows
    BlankRow = wsDst.Range("A" & wsDst.Rows.Count).End(xlUp).Row + 1

    'In the active sheet (in the workbook I am running this macro in [Title changes but same formatting each time]), copy cell B9. Paste in column A on the next blank row of the other workbook I am using [Can have the same title every time I run this process, or just be the only other workbook open]
    wsDst.Range("A" & BlankRow).Value = wsSrc.Range("B9").Value    

    'In the active sheet (in the workbook I am running this macro in), copy cell B8. Paste in column B of the row identified above.
    wsDst.Range("B" & BlankRow).Value = wsSrc.Range("B8").Value    

    'In the active sheet (in the workbook I am running this macro in), copy cell B12. Paste in column C of the row identified above.
    wsDst.Range("C" & BlankRow).Value = wsSrc.Range("B12").Value    

    'In the active sheet (in the workbook I am running this macro in), copy cells A17:E17. Paste in D:H of the row identified above.
    wsDst.Range("D" & BlankRow & ":H" & BlankRow).Value = wsSrc.Range("A17:E17").Value    

    'In the active sheet (in the workbook I am running this macro in), copy cells A17:E17. Paste in D:H of the row identified above.
    'No need to do this - we just did it

    'In the active sheet (in the workbook I am running this macro in), copy cells G17:N17. Paste in I:P of the row identified above.
    wsDst.Range("I" & BlankRow & ":P" & BlankRow).Value = wsSrc.Range("G17:N17").Value    

End Sub
Sub copysheet()
Dim wb  As Workbook
Dim wb1 As Workbook

application.screenupdating=False
application.DisplayAlerts=False
On error goto resetsettings

MyPath = "C:\Users\foo\" 'The folder containing the files you want to use
MyExtension = "*.xlsx" 'The extension of the file you want  to use

Myfile = Dir(MyPath & MyExtension)
Set wb = ThisWorkbook
While Myfile <> ""
Set wb1 = Workbooks.Open(MyPath & Myfile)
lr = wb1.Sheets(1).Range("A1:A" & Rows.Count).End(xlUp).Row + 1
wb.Sheets(1).Range("B9").Copy Destination:=wb1.Sheets(1).Range("A" & lr)
wb.Sheets(1).Range("B8").Copy Destination:=wb1.Sheets(1).Range("B" & lr)
wb.Sheets(1).Range("B12").Copy Destination:=wb1.Sheets(1).Range("C" & lr)
wb.Sheets(1).Range("A17:E17").Copy Destination:=wb1.Sheets(1).Range("D" & lr & ":H" & lr)
wb.Sheets(1).Range("G17:N17").Copy Destination:=wb1.Sheets(1).Range("I" & lr & ":P" & lr)
wb1.close Savechanges:=True
Myfile = Dir
Wend
ResetSettings:
application.screenupdating=True
application.DisplayAlerts=True
End Sub

This Macro will loop through all Xlsx Files in a folder and make the above changes in the files and closes them. 此宏将遍历文件夹中的所有Xlsx文件,并在文件中进行上述更改并关闭它们。

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

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