Hi All,
I think I've gone and killed the code again.
This is for the 'NCD Data' part of the code, what it has gone and done is that it is now not only deleting the header row (which I want deleted) but also the first row of data of the second table I want to append to the first.
Sub CopyDeliveryData()
Dim ReportWbk As Workbook 'workbook with report data
Dim Report As String 'name of file with report data
Dim lastRow As Long 'last row of first table
Dim WS As Worksheet
Dim listObj As ListObject
'Opens source file
Application.DisplayAlerts = False
Application.FileDialog(msoFileDialogFilePicker).Show
Report = Application.FileDialog(msoFileDialogFilePicker).SelectedItems(1)
'/Opens source file
'Source file setup
Set ReportWbk = Workbooks.Open(Report)
For Each WS In ReportWbk.Worksheets
For Each listObj In WS.ListObjects
listObj.AutoFilter.ShowAllData
Next listObj
Next WS
'/Source file setup
'***Delivery data***
ReportWbk.Sheets(1).Cells.Copy
ThisWorkbook.Sheets("Delivery").Activate
Cells(1, 1).Select: ActiveSheet.Paste
' ***Delivery data***
' ***NCD data***
ReportWbk.Sheets(2).Cells.Copy
ThisWorkbook.Sheets("AMO NCD").Activate
Cells(1, 1).Select: ActiveSheet.Paste
With ReportWbk.Sheets(3)
.Range(.Cells(2, "A"), .Cells(.UsedRange.Rows.Count, .UsedRange.Columns.Count)).Copy
End With
'Deleting the header row for sheet 3
ThisWorkbook.Sheets("AMO NCD").Activate
With ActiveSheet
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
.Cells(lastRow, 1).Select
.Paste
.Rows(lastRow).Select
.Rows(lastRow).Delete
End With
Also just for my understanding:
With ReportWbk.Sheets(3)
.Range(.Cells(2, "A"), .Cells(.UsedRange.Rows.Count, .UsedRange.Columns.Count)).Copy
End With
The code above copies only the 'used' rows and columns of a sheet starting from cell A2 right? Does this mean that for sheets(3) in the original code at the top, it does not copy the header row during the copy process thus eliminating the range for additional coding?
AND
'Deleting the header row for sheet 3
ThisWorkbook.Sheets("AMO NCD").Activate
With ActiveSheet
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
.Cells(lastRow, 1).Select
.Paste
.Rows(lastRow).Select
.Rows(lastRow).Delete
End With
'/Deleting the header row for sheet 3
The code above, for the current activesheet defines the lastrow and adds 1 row to it (row below the last used row of the first table), then pastes the second table on that row. It then deletes that row (essentially removing the header).
If my logic serves me right, this double elimination has deleted more rows than needed or is my understanding flawed?
I tried changing
.Range(.Cells(2, "A"), .Cells(.UsedRange.Rows.Count, .UsedRange.Columns.Count)).Copy
With
.Range(.Cells(1, "A"), .Cells(.UsedRange.Rows.Count, .UsedRange.Columns.Count)).Copy
But it gave me a range class error.
Bookmarks