1

我需要在不同的工作簿(控股和子公司)之间合并 2 个学生的注册。因此,当我单击“生成图形”按钮时,数据将合并到“总计”表中,然后计算课程数量(Excel、Word、Access ......)并在“图形”表中生成图形。第一次测试没问题,但是当我再次点击时,列表随着子公司工作簿的相同数据而增加。我需要更改代码中的某些内容,但我不知道是什么。你可以帮帮我吗?我的代码是:

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

'复制工作表“总计”中的数据

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
4

2 回答 2

0

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

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

Cells(k, 1).Activate

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

它在第一次通过时运行良好,但是在第二次运行代码时,您(正确地)粘贴了第一个工作簿数据,将第二个工作簿数据留在它下面,导航到数据的末尾,然后在第二个工作簿中重新粘贴数据。

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

于 2017-02-15T14:14:24.270 回答
0

它在 Excel 中用作 Ctrl + Shift + Down(箭头)和 Ctrl + Shift + Right(箭头)。现在没关系,它只会用作列表。我真的很感谢你的建议。我是 VBA 和 Stack Overflow 的新用户,这个社区非常有用,因为您的所有贡献。

于 2017-02-17T17:08:55.750 回答