neodjandre
Well-known Member
- Joined
- Nov 29, 2006
- Messages
- 950
- Office Version
- 2019
- Platform
- Windows
I am trying to paste data from old listobject to a new listobject and have come with this sub:
However, there is a problem with the last line, as the new listobject remains empty. i.e. the old data is not being pasted successfully even if I do not get any errors in vba. Any ideas on how to fix this?
many thanks,
Andrew
Code:
Sub san_import(owb As Workbook, nwb As Workbook, sha As Worksheet, lis As String, col As Long, res As Long)
Dim orng As ListObject
Dim nrng As ListObject
Dim r_fix As Long
Dim c_fix As Long
With owb
Set orng = sha.ListObjects(lis)
End With
With nwb
Set nrng = sha.ListObjects(lis)
End With
r_fix = orng.ListRows.Count - nrng.ListRows.Count
c_fix = orng.ListColumns.Count - nrng.ListColumns.Count
If r_fix > 0 Then
nrng.DataBodyRange.EntireRow.Resize(r_fix).Insert
End If
If c_fix > 0 Then
nrng.DataBodyRange.EntireColumn.Resize(c_fix).Insert
End If
nrng.DataBodyRange.Columns(col).Resize(, res).ClearContents
nrng.Range.Offset(1).Resize(orng.ListRows.Count).Columns(col).Resize(, res).Value2 = orng.DataBodyRange.Columns(col).Resize(, res).Value2
End Sub
However, there is a problem with the last line, as the new listobject remains empty. i.e. the old data is not being pasted successfully even if I do not get any errors in vba. Any ideas on how to fix this?
many thanks,
Andrew