Nightlytic
Member
Hi,
Can someone review my macro? Working on Excel 2010 here. I have multiple workbooks, one for analysis (here called x) and one for data (here called y) and I am working on a code to transfer data from workbooks y (based on file path listed in workbook x, column L, starting at "L17" named MyPath) to an output tab, immediately below whatever code is already there. Here is the code so far:
It 'works', my issues though:
1. the part on pasting data, I only pastes the value from top left corner. I've no idea why
2. Could you suggest a way for me to loop this, it's a bit beyond my ability. I need the macro to then declare the next cell, L18, L19 and so on, as the workbook and copy data over from those as well. Move on to the next L value if it can't find the file, and stop if the cell in L is a "" value (the hyperlinks are formulae that list files within a folder, and give a "" if there are none left.
3. Minor, but there are sometimes totals in the data that you can see me pitifuly trying to get rid of:
x.Sheets("Sheet1").Range("A1").End(xlDown).Offset(1, 0).EntireRow.Delete
Is there a way perhaps to modify this so that it looks at the last A1 value there
x.Sheets("Sheet1").Range("A1").End(xlDown).Offset(0, 0).EntireRow.Delete
I presume that? And then deletes it if and only if the A value there is "" or a space? There are no totals on A it's a reference point so I just want to delete anything that is empty there, but without running a macro to delete empty values across the entire sheet, just at the last 3 rows, for performance sake.
If you see any optimisation opportunities, please let me know, I have no idea how smoothly this will run, technically I envision it opening c.5-10 workbooks at a time, transferring c.10-20k rows of data
Thank you for any help
Can someone review my macro? Working on Excel 2010 here. I have multiple workbooks, one for analysis (here called x) and one for data (here called y) and I am working on a code to transfer data from workbooks y (based on file path listed in workbook x, column L, starting at "L17" named MyPath) to an output tab, immediately below whatever code is already there. Here is the code so far:
Code:
Sub CopyDataOver()
'Optimise settings
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
' Declare workbooks:
Dim x As Workbook
Dim y As Workbook
MyPath = Range("L17")
Set y = ThisWorkbook
Set x = Workbooks.Open(MyPath)
'remove extra data in sheet x (data)
x.Sheets("Sheet1").Range("A1").Copy
x.Sheets("Sheet1").Range("A1").EntireRow.Delete
x.Sheets("Sheet1").Range("A1").End(xlDown).Offset(1, 0).EntireRow.Delete
x.Sheets("Sheet1").Range("A1").End(xlDown).Offset(1, 0).EntireRow.Delete
x.Sheets("Sheet1").Range("A1").End(xlDown).Offset(1, 0).EntireRow.Delete
'move data to y (analysis)
Set CopyRange = x.Sheets("Sheet1").Range("A1:Z5000").CurrentRegion
Set PasteRange = y.Sheets("Output").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
PasteRange.Value = CopyRange.Value
'Close x:
Application.DisplayAlerts = False
x.Close False
Application.DisplayAlerts = True
'Reset settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
It 'works', my issues though:
1. the part on pasting data, I only pastes the value from top left corner. I've no idea why
2. Could you suggest a way for me to loop this, it's a bit beyond my ability. I need the macro to then declare the next cell, L18, L19 and so on, as the workbook and copy data over from those as well. Move on to the next L value if it can't find the file, and stop if the cell in L is a "" value (the hyperlinks are formulae that list files within a folder, and give a "" if there are none left.
3. Minor, but there are sometimes totals in the data that you can see me pitifuly trying to get rid of:
x.Sheets("Sheet1").Range("A1").End(xlDown).Offset(1, 0).EntireRow.Delete
Is there a way perhaps to modify this so that it looks at the last A1 value there
x.Sheets("Sheet1").Range("A1").End(xlDown).Offset(0, 0).EntireRow.Delete
I presume that? And then deletes it if and only if the A value there is "" or a space? There are no totals on A it's a reference point so I just want to delete anything that is empty there, but without running a macro to delete empty values across the entire sheet, just at the last 3 rows, for performance sake.
If you see any optimisation opportunities, please let me know, I have no idea how smoothly this will run, technically I envision it opening c.5-10 workbooks at a time, transferring c.10-20k rows of data
Thank you for any help