我如何在两个不同的工作簿之间合并数据?

我需要合并两个学生在不同的工作簿(控股和子公司)之间的注册。 所以当我点击button“生成graphics”时,数据被合并到“总计”表中,然后统计课程的数量(Excel,Word,Access …)并在图表“graphics”中生成graphics。 第一个testing是可以的,但是当我再次点击时,列表正在增加与子公司的工作簿相同的数据。 我需要更改代码中的某些内容,但是我不知道该怎么做。 你可以帮帮我吗? 我的代码是:

Sub GerarGrafico() Dim k As Long 'copying data of the “Course Booking” Sheet Sheets("Course Booking").Select Range("A1").Select linini = 2 'Select the last row Selection.End(xlDown).Select linfin = Selection.Row 'Select the last column Selection.End(xlToRight).Select colfin = Selection.Column Range(Cells(linini, 1), Cells(linfin, colfin)).Select Selection.Copy 

“在表单中复制数据”Total“

 Sheets("Total").Select Cells(linini, 1).Select ActiveSheet.Paste Application.CutCopyMode = False 'Copying data of the “reserva filial.xlsm” caminho = ThisWorkbook.Path Workbooks.Open caminho & "\" & "reserva filial.xlsm" 'copying data Range("A1").Select linini2 = 2 'Select the last row Selection.End(xlDown).Select linfin2 = Selection.Row 'Select the last column Selection.End(xlToRight).Select colfin2 = Selection.Column Range(Cells(linini2, 1), Cells(linfin2, colfin2)).Select Selection.Copy Windows("Trabalho_Felipe Granado_v8.xlsm").Activate Sheets("Total").Select 'Select the last row with value Selection.End(xlDown).Select k = ActiveCell.Row + 1 Cells(k, 1).Activate Application.Windows("reserva filial.xlsm").Visible = False 'pasting data "reserva filial.xlsm" in the sheet "Total" ActiveSheet.Paste Application.CutCopyMode = False Columns.HorizontalAlignment = xlCenter 

代码的这一部分导航到工作表中数据的末尾

 'Select the last row with value Selection.End(xlDown).Select k = ActiveCell.Row + 1 Cells(k, 1).Activate 

然后粘贴"reserva filial.xlsm"数据。

它在第一遍中运行正常,但是第二次运行代码时,您(正确)粘贴第一个工作簿数据,导航到数据的末尾,并在第二个工作簿中重新join数据。

根据您的Excel项目如何组合,您可能希望使用.ClearContents方法清除Sheets("Total")的全部内容Sheets("Total")或其子集。

它在Excel中作为Ctrl + Shift +向下(箭头)和Ctrl + Shift +向右(箭头)。 现在没关系,它只会作为一个列表使用。 我真的很感激你的build议。 我是一个新的VBA和堆栈溢出用户,这个社区是非常有用的,因为你所有的贡献。