我想从多个工作簿复制到一个工作表中的值

我正在尝试使用macros将多个单元格从一个工作簿中的一个工作表复制到主工作表。 我使用logging工具创build了一个macros,但是当我尝试在不同的工作簿中运行它时,代码的第一部分适用于不同的工作簿,但是其他部分则返回到原始工作表。 我看到macros保持激活特定的窗口(“文森特…”)我想知道如何可以定义选定的工作表作为variables,从而执行其余的使用该variables激活?

Range("F4:F14").Select Selection.Copy Windows("Combined Spreadsheet.xlsx").Activate Range("B" & Rows.Count).End(xlUp).Offset(1, 0).Select Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _ , Transpose:=True Windows("VincentCAIN107_Intra1_VD1_Rudd.xlsx").Activate Range("H4:H14").Select Application.CutCopyMode = False Selection.Copy Windows("Combined Spreadsheet.xlsx").Activate Range("L" & Rows.Count).End(xlUp).Offset(1, 0).Select Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _ , Transpose:=True Windows("VincentCAIN107_Intra1_VD1_Rudd.xlsx").Activate Range("N4:N14").Select Application.CutCopyMode = False Selection.Copy Windows("Combined Spreadsheet.xlsx").Activate Range("V" & Rows.Count).End(xlUp).Offset(1, 0).Select Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _ , Transpose:=True Windows("VincentCAIN107_Intra1_VD1_Rudd.xlsx").Activate Range("R4:R14").Select Application.CutCopyMode = False Selection.Copy Windows("Combined Spreadsheet.xlsx").Activate Range("AF" & Rows.Count).End(xlUp).Offset(1, 0).Select Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False _ , Transpose:=True Windows("VincentCAIN107_Intra1_VD1_Rudd.xlsx").Activate Range("S4:S14").Select Application.CutCopyMode = False Selection.Copy Windows("Combined Spreadsheet.xlsx").Activate Range("AP" & Rows.Count).End(xlUp).Offset(1, 0).Select Selection.PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=True _ , Transpose:=True 

我想你可以清理一下你的代码。 这里是一个例子:

 Sub JoinArray() Dim master As Worksheet, source As Worksheet, copyCols() As Variant, pasteCols() As Variant, i As Integer Set master = Workbooks("Combined Spreadsheet").Worksheets(1) Set source = Workbooks("VincentCAIN107_Intra1_VD1_Rudd").Worksheets(1) copyCols = Array("F", "H", "N", "R", "S") pasteCols = Array("B", "L", "V", "AF", "AP") For i = 0 To UBound(copyCols) source.Range(copyCols(i) & "4:" & copyCols(i) & 14).Copy master.Range(pasteCols(i) & master.Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True Next i End Sub 

笔记:

  1. 预先设置对工作表的引用
  2. 如果您知道要从中复制并粘贴的列,可以将其定义在数组中以供参考

在macros的顶部声明一个string

 DIM BookName As String 

然后分配它的书名

 BookName = "VincentCAIN107_Intra1_VD1_Rudd.xlsx" 

然后你可以使用Windows方法

 Windows(BookName).Activate 

为了简化您的代码,添加一个函数来处理给定正确参数的复制

 Sub CopyCells(Book1 As String, Book2 As String, RngSrc As String, ColumnDest As String) Windows(Book1).Activate Range(RngSrc).Select Selection.Copy Windows(Book2).Activate Range(ColumnDest & Rows.Count).End(xlUp).Offset(1, 0).Select Selection.PasteSpecial _ Paste:=xlAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True End Sub 

使用这个函数会减less你的源代码

 Sub CopyWorkbooks() Dim Book1 As String Dim Book2 As String Book1 = "VincentCAIN107_Intra1_VD1_Rudd.xlsx" Book2 = "Combined Spreadsheet.xlsx" Call CopyCells(Book1, Book2, "F4:F14", "B") Call CopyCells(Book1, Book2, "H4:H14", "L") Call CopyCells(Book1, Book2, "N4:N14", "V") Call CopyCells(Book1, Book2, "R4:R14", "AF") Call CopyCells(Book1, Book2, "S4:S14", "AP") End Sub 

将variables设置为工作簿:(示例)

 Option Explicit 'this forces to declare variables (, always good) Sub MySub Dim Wb as Workbook , MainWB as workbook Set MainWB = Workbooks("Combined Spreadsheet.xlsx") 'assuming it is opened Err.clear On Error resume Next 'if the workbook is not opened yet, it will throw an error Set Wb= Workbooks ("WhateverName.xls") ' <<<<<<<<<< this is the way of setting the workbook to read from If err<>0 then 'can also be written: if Wb is nothing then Err.clear Set Wb= Workbooks.Open (Thisworkbook.path & "\" & "WhateverName.xls") 'If Wb not open yet, we do it now, assuming the file is in the same path as this main workbook end if On Error Goto 0 'reset error handling 'work with Wb : With Wb .Sheets("AnySheetYouWant").Range("AnyRange").Copy 'by the way, no need to select or activate, "selection" can be replaced by a range MainWB.range("V" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlAll, Operation:=xlNone, _ SkipBlanks:=False , Transpose:=True .Save ' just for example .Close ' just for example End With Set Wb = Nothing ' i like freeing memory at the end of Subs... Set MainWB= Noting End Sub 

macroslogging器是不是太糟糕的初学者,但很快它真的很糟糕的编程…阅读的东西,使用谷歌,提出问题…