I need to consolidate 2 students’ registrations between different workbooks (holding and subsidiary). So when I click the button “Generate Graphic”, the data are consolidated in the sheet “Total” and then, the number of courses (Excel, Word, Access…) are counted and the graphic is generated in the sheet “Graphic”. The first test is ok, but when I click again, the list is increasing with the same data of the subsidiary’s workbook. There’s something in the code I need to change but I don’t know what. Could you help me? My code is:
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
‘copying data in the sheet "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
This part of the code navigates to the end of the data in the sheet
'Select the last row with value
Selection.End(xlDown).Select
k = ActiveCell.Row + 1
Cells(k, 1).Activate
Then you paste in "reserva filial.xlsm"
data.
It works fine on the first pass, but the second time you run the code, you (correctly) paste in the first workbook data, LEAVING THE SECOND WORKBOOK DATA BENEATH IT, navigate to the end of the data, and repaste in the second workbook data.
Depending on how your Excel project fits together you might wish to clear the entire contents of Sheets("Total")
or a subset of it using the .ClearContents
method.